Home > Software engineering >  Jenkins pipeline update file with build number and push to Git repo
Jenkins pipeline update file with build number and push to Git repo

Time:10-13

In a Jeninks pipeline, I want to update a file with the new build number in a different Git repo.

What's the best way to clone the repo, change the file, commit, and push the changes?

CodePudding user response:

Using a shell block is the easiest way.

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
          '''
        }
    }
}

CodePudding user response:

Thanks to @ycr I was finally able to solve it.

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
    '''
}
  • Related