场景

Docker中部署Jenkins+Pipline流水线基础语法入门:

Docker中部署Jenkins+Pipline流水线基础语法入门

上面介绍了环境搭建以及Pipeline的Jenkinsfile的常用写法。

如果需要通过Jenkins插件获取git相关的信息,比如上一次提交的SHA,分支名称等信息,然后

需要输出上一次git提交的message的相关信息,即需要执行git log等的相关sh指令,并获取指令

返回的结果并输出。

注:

博客:霸道流氓气质

实现

1、Jenkinsfile中如何执行Groovy语言脚本

参考官方文档说明:

流水线语法

需要添加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官方插件说明文档,直接可通过环境变量获取

Jenkins : Git Plugin

Docker+Jenkins+Pipline如何获取git插件环境变量(提交sha、分支等)以及Jenkinsfile中获取sh执行结果(获取git最近提交信息)_docker

插件所提供的环境变量如下

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 '构建失败'
        }
    }
}

示例运行结果

Docker+Jenkins+Pipline如何获取git插件环境变量(提交sha、分支等)以及Jenkinsfile中获取sh执行结果(获取git最近提交信息)_docker_02