Merging to main branch

stage(‘Commit & Push’) {
steps {
dir(“eks-python-demo”) {
sh “git config --global user.email ‘[email protected]’”
sh ‘git remote set-url origin https://[email protected]/ksnithya/eks-python-demo.git
sh ‘git checkout feature’
sh ‘git add -A’
sh ‘git commit -am “Updated image version for Build - $VERSION”’
sh ‘git push origin feature’
sh ‘git fetch --all’
sh ‘git merge -m “merging to main branch” origin/main’
sh ‘git push origin main --force’
}
}
}
I am trying to merge the feature branch to main branch using above code but it fails with below error. Kindly help on this. Feature branch is updated with latest code but merging to main branch is failing. Kindly let me know what wrong is there in code

You didn’t checkout main. This pipeline should work:

stage('Commit & Push') {
    steps {
        dir("eks-python-demo") {
            // Configure Git
            sh "git config --global user.email ‘[email protected]’"
            sh "git remote set-url origin https://[email protected]/ksnithya/eks-python-demo.git"

            // Commit changes to feature branch
            sh 'git checkout feature'
            sh 'git add -A'
            sh "git commit -am 'Updated image version for Build - $VERSION'"
            sh 'git push origin feature'

            // Prepare main branch
            sh 'git fetch --all'
            sh 'git checkout main'
            sh 'git pull origin main'

            // Merge feature into main
            sh 'git merge feature -m "Merging feature into main"'

            // Push changes
            sh 'git push origin main --force'
        }
    }
}

Also you should avoid using force push where you can.

Also probably not a good idea to publish your email on a public forum.

1 Like

Thanks a lot. It is working now. :handshake: :handshake: :handshake: