jenkins的pipeline中实现git提交

背景

一般场景,就是jenkins从git的代码库下载代码进行各种动作。
但是,我们的场景会出现在jenkins中将自动生成的文件提交到git代码库中。
典型场景是:我用解析工具将数据字典、配置文件进行解析,生成配置文件和头文件等(富含业务信息,程序使用)。当文档更新时,这些文件就会自动在代码库更新,传统的人工操作一定会遗漏或忘记的。

解决方法

jenkins的pipeline实现如下:

stage('Document') {
    //conf2bin output headers
    sh 'python ${CONF2BIN}/conf2bin.py'
    //generate international mo files from po files which are written by conf2bin.py
    sh 'cd platform/tools && bash get_mo_file.sh'
    //CI commit header files automatically
    if ("$BRANCH" != "null") {
        //powerci credentials id in power ci server
        sshagent(['2f64a97f-0358-xxx-8863-bd2e16928e1d']) {
            //you must use this way, or else you can not get the shell result
            //trim() is necessary to get rid of the \n in shell result
            def uid = sh(script: 'id -u', returnStdout: true).trim()
            // the name can be anything
            sh "useradd jenkins -u ${uid} -m -s /bin/bash"
            // withouth known_hosts you can not git push automatically
            sh "cp -r .ssh /home/jenkins/.ssh"
            //change push url, or else you will push to gerritro
            //if nothing is added, this commit will fail
            //you can not remove HEAD:refs/heads/ or else it fails
            //language includes po and mo files
            // and you can not put the above comments in following """"""
            sh """
            git add ${INCLUDE_PATH}
            git add platform/power/data/language
            git config --global user.name "powerci"
            git config --global user.email "PowerCI@zte.com.cn"
            git config --global url."ssh://powerci@gerrit".pushInsteadOf ssh://powerci@gerritro
            git clean -df
            git commit -m "header changed. ci commit automatically" || true
            git push origin HEAD:refs/heads/$BRANCH
            """
        }
    }
}

关键地方,上面都有注释。
这里用中文再详细说明一下。

0.jenkins安装必要插件sshagent
pipeline目前需要依赖其他插件来实现git的提交,因此,需要安装sshagent插件,否则,sshagent那步会出现无故障提示的退出

1.生成文件
在if (“ BRANCH!=null)pipelinewithEnvBRANCH= {env.BRANCH_NAME}”)

2.找到credentials id
在jenkins的credentials中找到具有提交git库权限的账号对应的credentials id

3.为docker的用户id建立一个名字
由于我们的gerrit是基于ssh提交的,ssh中是不允许没有用户名的操作的。pipeline的docker操作,只会分配一个用户id(uid),没有用户名,因此,需要useradd一个用户名(任意皆可),才能完成后面的ssh提交代码操作。
提示:useradd要加-m,才能生成它的home目录,下一步操作需要home目录。

4.添加known_hosts
ssh第一次连接新的主机时,为了安全,都会询问是否信任它,这是一个手动操作的过程。点击yes后,会在 HOME/.ssh/knownhostsgerrit.zte.com.cnknownhostsCI HOME/.ssh文件夹即可生效
(中兴gerrit主机真是不变的吗?)

5.git提交
git config这步。CI的docker里一般不会没有.gitconfig文件,因此,需要git config这几步。注意,一定要有pushInsteadOf那一步,否则会push到gerritro(只读库)。
git commit这步,当没有文件可以提交时(业务文档没有改变,和代码库相比输出物不变),会出错,因此,需要加上“ || true”
git push这步,需要加上“HEAD:refs/heads/”这个前缀,否则会出错。
(git branch可以发现处于HEAD detached状态,这种状态下,git push无法直接推导出 BRANCHfullnameHEAD:refs/heads/ BRANCH)

6.一个易错点
sh中 \”””和”“”“”“是不一样的,这是groovy语法
\”””里面${xxx}会被直接显示,如果需要显示xxx这个变量的值,需要使用”“”“”“,这点很易错。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值