Docker+Jenkins+Pipline如何獲取git外掛環境變數(提交sha、分支等)以及Jenkinsfile中獲取sh執行結果(獲取git最近提交資訊)

霸道流氓發表於2024-06-15

場景

Docker中部署Jenkins+Pipline流水線基礎語法入門:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/139672283

上面介紹了環境搭建以及Pipeline的Jenkinsfile的常用寫法。

如果需要透過Jenkins外掛獲取git相關的資訊,比如上一次提交的SHA,分支名稱等資訊,然後

需要輸出上一次git提交的message的相關資訊,即需要執行git log等的相關sh指令,並獲取指令

返回的結果並輸出。

注:

部落格:
https://blog.csdn.net/badao_liumang_qizhi

實現

1、Jenkinsfile中如何執行Groovy語言指令碼

參考官方文件說明:

https://www.jenkins.io/zh/doc/book/pipeline/syntax/

需要新增script塊

官方示例:

pipeline {
agent any
stages {
stage('Example') {
steps {
echo 'Hello World'

script {
def browsers = ['chrome', 'firefox']
for (int i = 0; i < browsers.size(); ++i) {
echo "Testing the ${browsers[i]} browser"
}
}
}
}
}
}

Jenkinsfile中使用Groovy的異常處理try-catch官方示例

node {
stage('Example') {
try {
sh 'exit 1'
}
catch (exc) {
echo 'Something failed, I should sound the klaxons!'
throw
}
}
}

2、Jenkinsfile中如何獲取Jenkins安裝Git外掛後相關git資訊。

需要參考Git-Plugin官方外掛說明文件,直接可透過環境變數獲取

https://wiki.jenkins.io/JENKINS/Git-Plugin.html

外掛所提供的環境變數如下

GIT_COMMIT - SHA of the current
GIT_BRANCH - Name of the remote repository (defaults to origin), followed by name of the branch currently being used, e.g. "origin/master" or "origin/foo"
GIT_LOCAL_BRANCH - Name of the branch on Jenkins. When the "checkout to specific local branch" behavior is configured, the variable is published. If the behavior is configured as null or **, the property will contain the resulting local branch name sans the remote name.
GIT_PREVIOUS_COMMIT - SHA of the previous built commit from the same branch (not set on first build on a branch)
GIT_PREVIOUS_SUCCESSFUL_COMMIT - SHA of the previous successfully built commit from the same branch (not set on first build on a branch)
GIT_URL - Repository remote URL
GIT_URL_N - Repository remote URLs when there are more than 1 remotes, e.g. GIT_URL_1, GIT_URL_2
GIT_AUTHOR_NAME and GIT_COMMITTER_NAME - The name entered if the "Custom user name/e-mail address" behaviour is enabled; falls back to the value entered in the Jenkins system config under "Global Config user.name Value" (if any)
GIT_AUTHOR_EMAIL and GIT_COMMITTER_EMAIL - The email entered if the "Custom user name/e-mail address" behaviour is enabled; falls back to the value entered in the Jenkins system config under "Global Config user.email Value" (if any)

使用示例:

stage('獲取提交資訊') {
steps {
script {
try {
def gitPreviousCommit = env.GIT_PREVIOUS_COMMIT
println("gitPreviousCommit: ${gitPreviousCommit}")
def gitCommit = env.GIT_COMMIT
println("gitCommit: ${gitCommit}")
def gitBranch = env.GIT_BRANCH
println("gitBranch: ${gitBranch}")
} catch (Exception ex) {
println("獲取提交資訊異常: ${ex}")
}
}
}
}

3、Pipeline中使用Jenkinsfile獲取script塊中執行sh的結果

首先git中獲取最近一次提交資訊的sh指令為

git log -1 --pretty=format:'%h - %an, %ar : %s'

如何在script中獲取該指令執行的結果並輸出

def commit = sh(returnStdout: true, script: "git log -1 --pretty=format:'%h - %an, %ar : %s'").trim()
println("獲取提交資訊: ${commit}")

其中script後面跟的是具體sh指令,其它格式固定,commit是自定義的變數

完整示例:

stage('獲取提交資訊') {
steps {
script {
try {
def gitPreviousCommit = env.GIT_PREVIOUS_COMMIT
println("gitPreviousCommit: ${gitPreviousCommit}")
def gitCommit = env.GIT_COMMIT
println("gitCommit: ${gitCommit}")
def gitBranch = env.GIT_BRANCH
println("gitBranch: ${gitBranch}")
def commit = sh(returnStdout: true, script: "git log -1 --pretty=format:'%h - %an, %ar : %s'").trim()
println("獲取提交資訊: ${commit}")
} catch (Exception ex) {
println("獲取提交資訊異常: ${ex}")
}
}
}
}

完整Jenkinsfile示例:

pipeline {
agent any
tools {
maven 'maven'
jdk 'jdk'
}
stages {
stage('獲取提交資訊') {
steps {
script {
try {
def gitPreviousCommit = env.GIT_PREVIOUS_COMMIT
println("gitPreviousCommit: ${gitPreviousCommit}")
def gitCommit = env.GIT_COMMIT
println("gitCommit: ${gitCommit}")
def gitBranch = env.GIT_BRANCH
println("gitBranch: ${gitBranch}")
def commit = sh(returnStdout: true, script: "git log -1 --pretty=format:'%h - %an, %ar : %s'").trim()
println("獲取提交資訊: ${commit}")
} catch (Exception ex) {
println("獲取提交資訊異常: ${ex}")
}
}
}
}
stage('編譯構建') {
steps {
echo '編譯構建'
//sh 'mvn clean package -DskipTests'
}
}
}
post {
always {
echo '構建結束,結果:'
}
success {
echo '構建成功'
}
failure {
echo '構建失敗'
}
}
}

示例執行結果

相關文章