Giter Site home page Giter Site logo

jenkins-tutorial-shared-lib's Introduction

jenkins-tutorial-shared-lib

Shared library for the Jenkins tutorial

Vars

This lib contains the following vars:

isMaster

Simple callable function that returns true if the branch is master or main:

if(isMaster()) {
    println 'We are running on master!'
}

sanitizeImageTag

Sanitizes an image tag, removing all illegal special characters and replacing them with a -:

sanitizeImageTag('feat/my-branch#123') // Returns feat-my-branch-123

kaniko

This global var cannot be called directly, instead one must use one of two functions:

  • kaniko.buildAndPush: when you want the image to be pushed to a remote repository, like ECR.
  • kaniko.buildNoPush: when you just want to build the image.

This function requires a Kaniko container to be available on the pod.

Parameters

Both functions take a Map args argument. One can pass this map directly or use Groovy's syntax sugar to help, both of these are equivalent:

// This
def myMap = [foo: "value", bar: 123]
myFunc(myMap)

// Is equivalent to just this
myFunc foo: "value", bar: 123

Both functions receive the exact same parameters, described below. Parameters without a default value are required. For reference, Full Docker image names use the following pattern: REGISTRY/IMAGE_NAME:IMAGE_TAG. Check this Stackoverflow question for a better explanation.

Parameter Default Description
imageName - The name of the image to be built, including a registry if a custom one is used, but without the tag.
imageTag - The tag(s) to be used on the Docker image. Accepts either a string or a list of strings, if you wish to push multiple tags (like the commit but also latest).
container 'kaniko' The name of the Kaniko container.
shell '/busybox/sh' Path of the shell inside the Kaniko container.
dockerfile "${env.WORKSPACE}/Dockerfile" Path of the Dockerfile, passed to Kaniko with the -f argument.
context env.WORKSPACE Path of the docker build context, passed to Kaniko with the -c argument.
extraArgs '' Extra arguments to pass to Kaniko. See the official documentation for help.
pathOverride "/kaniko:/busybox:${env.PATH}" Overrides the PATH environment variable.

When using kaniko.buildNoPush, the --no-push argument is automatically passed to Kaniko, there's no need to provide it using extraArgs.

Examples:

Simple build without push

stage('Kaniko Build') {
    when {
        expression { !isMaster() }
    }
    steps {
        script {
            kaniko.buildNoPush(
                    imageName: 'our-application',
                    imageTag: '1.2.3-alpine',
                    extraArgs: '--build-arg NPM_TOKEN=${NPM_TOKEN}'
            )
        }
    }
}

Re-utilizing code for multiple stages

pipeline {
    // -- snip --
    stage('Kaniko Build | No Push') {
        when {
            expression { !isMaster() }
        }
        steps {
            script {
                kaniko.buildNoPush kanikoArgs()
            }
        }
    }
    stage('Kaniko Build And Push') {
        when {
            expression { isMaster() }
        }
        steps {
            script {
                kaniko.buildAndPush kanikoArgs()
            }
        }
    }
    // -- snip --
}

def kanikoArgs() {
    return [
            imageName: 'our-application',
            imageTag: '1.2.3-alpine',
            dockerfile: "${env.WORKSPACE}/docker/Dockerfile",
            context: "${env.WORKSPACE}/docker",
            extraArgs: '--build-arg NPM_TOKEN=${NPM_TOKEN}'
    ]
}

Pushing multiple tags

stage('Kaniko Build') {
    steps {
        script {
            kaniko.buildAndPush(
                    imageName: 'our-application',
                    imageTag: [env.GIT_COMMIT, 'latest']
            )
        }
    }
}

jenkins-tutorial-shared-lib's People

Contributors

angelin01 avatar

Stargazers

Igor Assunção avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.