Jenkins管道更新文件(包含内部版本号)并推送到Git存储库

gpnt7bae  于 2022-11-02  发布在  Jenkins
关注(0)|答案(2)|浏览(176)

在Jeninks管道中,我想在不同的Git存储库中用新的build号更新一个文件。
克隆存储库、更改文件、提交和推送更改的最佳方式是什么?

ibrsph3r

ibrsph3r1#

使用shell块是最简单的方法。

stage('Change File'){
  steps{
        dir("newRepo"){
          sh '''
              git clone YOUR_REPO .
              echo $BUILD_NUMBER > file1.txt
              git add file1.txt
              git commit -m "Updates file1.txt with $BUILD_NUMBER"
              git push
          '''
        }
    }
}
laik7k3q

laik7k3q2#

感谢@ycr我终于能够解决它。

withCredentials([usernamePassword(credentialsId: 'build.user', passwordVariable: 'pass', usernameVariable: 'user')]) {
    sh '''
        if cd tools-gitops; then git pull; else git clone http://git/Tools/tools-gitops; fi
        sed -i 's/"x\\/x-support:.*"/"x\\/x:'$BUILD_NUMBER'"/g' x-support.yml
        git commit -am "Updates x-support.yml with $BUILD_NUMBER"
        git push http://$user:$pass@git/Tools/tools-gitops
    '''
}

相关问题