Home > Software engineering >  Update file content in gitlab using jenkin
Update file content in gitlab using jenkin

Time:10-15

Is there a way, after updating the file content via Jenkinsfile, to push it back to Git Lab and replace the previous file? I fetched all the files to the working dir and changed the content with sed.

pipeline
{
    agent any
    stages {
        stage('Update deployment file') {
                steps {
                        sh 'sed -i "s/source/update/g" file.txt'
                }
        }

        stage('Push to gitlab') {
                steps {
                        ?????????
                }
        }

Thanks in advance.

CodePudding user response:

You can simply use a shell block for this. If you need credentials to push, you may have to append them to the URL or configure them in the git client.

stage(''Push to gitlab''){
  steps{
          sh '''
              git add file.txt
              git commit -m "Updates file1.txt"
              git push
          '''
    }
}

CodePudding user response:

In extension to the Answer before.

You can set the ssh Key for git with the following snipped:

sshagent(['<credentialsID>']) {
    sh("git push origin HEAD:${BRANCH}")
}
  • Related