Jenkins File Example

https://www.notion.so/Jenkins-File-Example-bb0efd5ea63e41699913260f81ad7219

Jenkinsfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
pipeline {
    agent any

    tools { 
        maven 'Maven 3.5'
    }

    environment { 
        APP_NAME     = 'wf'
        DEPLOY_TO    = '../../../../websites/demo/'
        EMAIL_RECIPIENTS = 'zb@bndy.net'
        HAS_DEPLOYMENT   = 'false'
    }

    stages {
        stage('Prepare') {
            steps {
                updateGithubStatus("PENDING", "Begin to build");
                //sh 'printenv'
                sh 'java -version'
                sh 'mvn -v'
                sh 'mkdir -p ../../email-templates/ && \\cp ./jenkins/my-groovy-html.template ../../email-templates/my-groovy-html.template'
            }
        }
        stage('Build') {
            steps {
                echo 'Building...'
                sh 'mvn -B -DskipTests clean package' 
            }
        }
        stage('Test') {
            steps {
                echo 'Skip tests'
            }
        }
        stage('Deploy') {
            when {
                // following expression is just available for multi-branch project
                branch 'master'
            }
            steps {
                echo 'Deploying....'
                sh 'rm -f ${DEPLOY_TO}/${APP_NAME}.war'
                sh 'rm -Rf $DEPLOY_TO/${APP_NAME}'
                sh 'mv ./target/wf-*.war ${DEPLOY_TO}/${APP_NAME}.war'
                script {
                    HAS_DEPLOYMENT = 'true'
                }
            }
        }
    }
    
    post {
        success {
            updateGithubStatus("SUCCESS", "Build complete");
            sendEmail("SUCCESS");
        }
        unstable {
            updateGithubStatus("UNSTABLE", "Build complete");
            sendEmail("UNSTABLE");
        }
        failure {
            updateGithubStatus("FAILURE", "Build complete");
            sendEmail("FAILURE");
        }
    }
}

// get change log to be send over the mail
@NonCPS
def getChangeString() {
    MAX_MSG_LEN = 100
    def changeString = ""

    echo "Gathering SCM changes"
    def changeLogSets = currentBuild.changeSets
    for (int i = 0; i < changeLogSets.size(); i++) {
        def entries = changeLogSets[i].items
        for (int j = 0; j < entries.length; j++) {
            def entry = entries[j]
            truncated_msg = entry.msg.take(MAX_MSG_LEN)
            changeString += "<li>${truncated_msg} [${entry.author}]</li>"
        }
    }

    if (!changeString) {
        changeString = "<li>No new changes</li>"
    }
    return changeString
}

def sendEmail(status) {
    def subject = "[" + status + "] ${currentBuild.fullDisplayName}"
    if (HAS_DEPLOYMENT == 'true') {
        subject += " - deployed"
    }

    // Default Email Notification Plugin: email(...), but below supports html
        emailext(
	mimeType: "text/html",
        to: "$EMAIL_RECIPIENTS",
        subject: "${subject}",
	body: '''${SCRIPT, template="my-groovy-html.template"}''',
        //body: "Changes:<ul>" + getChangeString() + "</ul><br />Check console output at: ${BUILD_URL}console" + "<br />",
	recipientProviders: [[$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider']]
    )
}

// set GitHub status
def getRepoURL() {
    sh "git config --get remote.origin.url > .git/remote-url"
    return readFile(".git/remote-url").trim()
}

def getCommitSha() {
    sh "git rev-parse HEAD > .git/current-commit"
    return readFile(".git/current-commit").trim()
}

def updateGithubStatus(status, message) {
    // status: pending, success, failure or error
    repoUrl = getRepoURL()
    commitSha = getCommitSha()

    step ([
        $class: 'GitHubCommitStatusSetter',
        reposSource: [$class: "ManuallyEnteredRepositorySource", url: repoUrl],
        commitShaSource: [$class: "ManuallyEnteredShaSource", sha: commitSha],
        errorHandlers: [[$class: 'ShallowAnyErrorHandler']],
        statusResultSource: [
            $class: 'ConditionalStatusResultSource',
            results: [
                [$class: 'AnyBuildResult', state: status, message: message]
            ]
        ]
    ])
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
node {
  print '✋' + params
	print params.anything_to_undefined     // output: null

  if (params.MultiLines) {
    params.MultiLines.split("\\r?\\n").each {
      if (it.trim()) {
        echo '✋-->' + it
      }
    }
  }

  // clean workspace
  cleanWs()

  try {
    docker.image('node:14').inside {
      stage("Source code") {
        //git branch: Git_Branch, credentialsId: Cred_ID, url: Git_Url
      }
      stage('Install packages') {
        sh 'npm i'
      }
      stage('Checks') {
        // TODO
      }
      stage('Test') {
        if (params.Skip_Test != true) {
          // TODO
          echo 'do test'
        } else {
          echo 'Skip test'
        }
      }
      stage('Deploy') {
        if (params.Skip_Deploy != true) {
          // TODO
          echo 'do deploy'
        } else {
          echo 'Skip deploy'
        }
      }
      stage('Deploy to production') {
        def toProdParams
        def didTimeout = false
        try {
          timeout(time: 15, unit: 'SECONDS') { // change to a convenient timeout for you
            toProdParams = input(
              id: 'deployToProd', message: 'Deploy to production?', parameters: [
                choice(choices: ['prod-a.com/api', 'prod-b.com/api'], description: 'Backend endpoint to connect', name: 'Backend_Uri'),
                booleanParam(defaultValue: false, description: 'Archive this package', name: 'Archive')
              ])
          }
        } catch (err) { // timeout reached or input false
          // def user = err.getCauses()[0].getUser() // required administrator
          //if('SYSTEM' == user.toString()) { // SYSTEM means timeout.
          didTimeout = true
          //} else {
          //toProd = false
          //echo "Aborted by: [${user}]"
          //}
        }
        echo '✋Your choice: ' + toProdParams // null if timeout
        if (toProdParams) {
          echo toProdParams.Backend_Uri
        }

        if (didTimeout) {
          // do something on timeout
          echo "no input was received before timeout"
        } else if (toProdParams) {
          // do something
          echo "Deployed to production."
        } else {
          // do something else
          echo "Skip deployment to production."
        }
      }
    }
  } catch (error) {
    echo """❗Exception thrown: 
    ${error.class}
    ${error.message}
    """

    // error.printStackTrace() // required administrator

    currentBuild.result = 'FAILURE'
  } finally {
    if (currentBuild.result == 'SUCCESS') {
      // TODO
    } else {
      // TODO
    }
    echo currentBuild.result
  }
}
This post is licensed under CC BY 4.0 by the author.