Skip to content

Instantly share code, notes, and snippets.

@nguyenthang98
Last active October 25, 2020 10:29
Show Gist options
  • Save nguyenthang98/7faaa935ddff63c7e3da0b75f87c0fc9 to your computer and use it in GitHub Desktop.
Save nguyenthang98/7faaa935ddff63c7e3da0b75f87c0fc9 to your computer and use it in GitHub Desktop.
Practical example with Jenkins scripted pipeline and shared library
@Library('gitlab_lib') _
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
final project_id = "nguyenthang98%2Fexample-project-jenkins"
node("Slave") {
checkout scm
final latest_commit = sh(script: 'git rev-parse --verify HEAD', returnStdout: true)
final sha = env.gitlabAfter ?:latest_commit;
try {
stage("Unittest") {
gitlab.build_status(project_id, sha, "Unittest", "running")
try {
sh 'echo "total:\\t\\t\\t\\t(statements)\\t$(( ( RANDOM % 50 ) + 51 )).$(( (RANDOM % 10) + 1))%" > /tmp/coverage'
sh 'cat /tmp/coverage'
def code_coverage = sh(script: ' cat /tmp/coverage | grep total: | awk \'{ sub("%",""); print $3}\'', returnStdout: true)
gitlab.build_status(project_id, sha, "Unittest", "success", code_coverage)
} catch (e) {
gitlab.build_status(project_id, sha, "Unittest", "failed")
throw e
}
}
def doBuild = false
stage("Build") {
if (doBuild) {
gitlab.build_status(project_id, sha, "Build", "running")
try {
sh "sleep 1"
gitlab.build_status(project_id, sha, "Build", "success")
} catch (e) {
gitlab.build_status(project_id, sha, "Build", "failed")
throw e
}
} else {
Utils.markStageSkippedForConditional("Build")
}
}
def deploys = [:]
def deploy_envs = ['development', 'test']
for(deploy_env in deploy_envs) {
echo deploy_env
deploys[deploy_env] = gitlab.prepareParallelStage("Deploy ${deploy_env}", project_id, sha)
}
parallel(deploys)
} catch(e) {
echo e.toString()
throw e
} finally {
stage("Clean up") {
gitlab.build_status(project_id, sha, "Clean%20up", "running")
deleteDir()
gitlab.build_status(project_id, sha, "Clean%20up", "success")
}
}
}
def build_status(project_id, commit_sha, stage, status, code_coverage=null) {
// final latest_commit = sh(script: 'git rev-parse --verify HEAD', returnStdout: true)
// def sha = env.gitlabAfter ?:latest_commit;
def sha = commit_sha;
def job = env.JOB_URL.replace("/", "%2F") + "${env.BUILD_NUMBER}%2Fconsole"
def query = "context=${stage.replace(" ","%20")}&state=${status}&target_url=${job}"
final request_url = "https://gitlab.com/api/v4"
if (code_coverage) {
query = query + "&coverage=${code_coverage}"
}
withCredentials([usernamePassword(credentialsId: 'gitlab_token', passwordVariable: 'GL_PSWD', usernameVariable: 'GL_USER')]) {
httpRequest customHeaders: [
[maskValue: true, name: 'PRIVATE-TOKEN', value: "${GL_PSWD}"]
], httpMode: 'POST',
responseHandle: 'NONE',
url: "${request_url}/projects/${project_id}/statuses/${sha}?${query}",
wrapAsMultipart: false
}
}
def prepareParallelStage(name, project_id, sha) {
return {
stage(name) {
echo name
build_status(project_id, sha, name, "running")
try {
sh "sleep 1"
build_status(project_id, sha, name, "success")
} catch (e) {
build_status(project_id, sha, name, "failed")
throw e
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment