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

场景

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

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

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

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

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

返回的结果并输出。

注:

博客:
霸道流氓气质-CSDN博客

实现

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

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

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

示例运行结果

  • 23
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
搭建项目自动化框架需要一定的技术基础,以下是从0到1使用 Docker + Jenkins + Git + Pytest + Allure 搭建项目自动化框架的步骤: 1. 安装 Docker 首先需要在本机安装 Docker,可以到 Docker 官网下载并安装。 2. 创建 Docker 镜像 使用 Dockerfile 创建 Docker 镜像,配置好 Python 3.7 环境以及所需的依赖包,例如 pytest, allure-pytest 等。 3. 创建 Jenkins 服务器 使用 Docker 创建 Jenkins 服务器,可以通过以下命令启动 Jenkins 服务器: ``` docker run -p 8080:8080 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts ``` 其-p参数是指定端口映射,-v参数是挂载目录到本地磁盘。 4. 安装必要的 Jenkins 插件Jenkins 安装必要的插件,如 Git 插件、Allure 插件等。 5. 创建 Jenkins 任务 创建一个 Jenkins 任务,配置源码管理为 Git,指定 Git 仓库地址和分支名称。在构建步骤添加执行命令的步骤,例如: ``` docker run -it --rm -v $WORKSPACE:/test -w /test <docker_image_name> pytest --alluredir ./report/allure-report ``` 其,$WORKSPACE 是 Jenkins 任务的工作目录,<docker_image_name> 是之前创建的 Docker 镜像名称,pytest 命令是执行测试用例的命令。 6. 配置 Allure 报告 在 Jenkins 配置 Allure 报告,安装 Allure 插件,配置 Allure 命令路径,并指定报告存放路径。 7. 执行 Jenkins 任务 执行 Jenkins 任务,Jenkins 将会拉取 Git 代码,构建 Docker 镜像并启动容器运行测试用例,最后生成 Allure 报告。 以上就是使用 Docker + Jenkins + Git + Pytest + Allure 搭建项目自动化框架的步骤,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值