跳过正文
  1. Posts/

通过 Jenkins 来提交修改的代码 git push by Jenkins

·439 字·1 分钟· ·

在持续集成中,你可能需要通过 Jenkins 来修改代码,并且将修改后的代码提交到Git仓库里。怎么做呢?最方便的做法还是 Jenkins 提供对应的插件,但是很遗憾我没找到合适的。另外我也觉得通过脚本的方式来实现会更加稳定,不用担心 Jenkins 以及插件升级带来潜在不好用的可能。

以下 pipeline 片段供参考使用:

// This pipeline is used for bumping build number

pipeline {

    environment {
        MYGIT = credentials("d1cbab74-823d-41aa-abb7")
    }
    stages {

        stage('Git clone repo') {
            steps {
                sh 'git clone -b develop --depth 1 https://$MYGIT_USR:"$MYGIT_PSW"@github.com/shenxianpeng/blog.git'
            }
        }

        stage('Change code stage'){
            steps {
                sh ''
            }
        }

        stage('Git push to remote repo') {
            steps {
                sh label: '', script: '''
                cd blog
                git add .
                git commit -m "Bld # 1001"
                git push https://$MYGIT_USR:"$MYGIT_PSW"@github.com/shenxianpeng/blog.git --all'''
            }
        }
    }

}

这里面我所遇到最大的坑,我之前脚本是这样写的:

stage('Git push to remote') {

    // not works script

    steps {
        sh 'cd blog'
        sh 'git add .'
        sh 'git commit -m "${JIRA_NO} Bld # ${BUILD_NO}"'
        sh 'git push https://$MYGIT_USR:"$MYGIT_PSW"@github.com/shenxianpeng/blog.git --all'
    }
}

在最后一个阶段提交代码时,shell 脚本不能使用单引号 ‘’,要使用三引号才行’’’ ‘’’。我在这里花了很多时间,一直找不到问题所在,因为我在上面的shell脚本使用的时候用单引号 ’’ 可以正常 git clone 代码,但在提交代码时不行,最后我 Jenkins 的 Pipeline Syntax 生成的脚本,提交代码成功。

沈显鹏
作者
沈显鹏
Engineer. Builder. Maintainer.

相关文章

Jenkinsfile 配置

·256 字·1 分钟
本文介绍了如何使用 Jenkinsfile 配置 Jenkins Pipeline,包括构建、测试和发布阶段的示例,以及如何处理邮件通知。

Git 管理

·324 字·1 分钟
本文介绍了 Git 的常见管理操作,包括分支管理、提交规范、代码审查等,帮助开发者更好地使用 Git 进行版本控制。

Jenkins Linux Agent 配置

·353 字·1 分钟
本文提供了 Jenkins Linux Agent 的逐步配置指南,包括 Java 运行时的准备、节点创建以及常见问题的排查方法。