问题
I am trying to create a Jenkins workflow using a Jenkinsfile. All I want it to do is monitor the 'develop' branch for changes. When a change occurs, I want it to git tag and merge to master. I am using the GitSCM Step but the only thing that it appears to support is git clone. I don't want to have to shell out to do the tag / merge but I see no way around it. Does anyone know if this is possible? I am using BitBucket (on-prem) for my Git server.
回答1:
It is not possible at the moment because GitPublisher plugin, the plugin previously responsible for tagging/merging/pushing in freestyle jobs, has not been updated to be compatible with Jenkins pipelines. You can follow that issue on both the pipeline plugins compatibility page and the dedicated GitPublisher Jira issue.
So it seems the only option you have is to actually shell out your tag/merge commands... However, note that you can still benefit from some Jenkins built-in capabilities such as the use of credentials for your Git repo, which make it pretty straightforward to then tag / merge following your needs.
Example check-out :
git url: "ssh://jenkins@your-git-repo:12345/your-git-project.git",
credentialsId: 'jenkins_ssh_key',
branch: develop
Then the tag / merge / push will be pretty straightforward :
sh 'git tag -a tagName -m "Your tag comment"'
sh 'git merge develop'
sh 'git commit -am "Merged develop branch to master'
sh "git push origin master"
I hope that one day GitPublisher will be released in a pipeline-compatible version, but for now this workaround should do.
回答2:
If what you're after are the git credentials you can use the SSH Agent plugin like in this link: https://issues.jenkins-ci.org/browse/JENKINS-28335?focusedCommentId=260925&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-260925
sshagent(['git-credentials-id']) {
sh "git push origin master"
}
回答3:
In my case I was forced to work with HTTPS. I solved it by:
- Creating a username/password credential bitbucketUsernamePassword.
- Using that credential to checkout.
- Setting credential.helper before doing checkout.
- Doing a git checkout branch to get a local branch tracking remote.
Then I am able to push things with git push after that.
Like this:
sh 'git config --global credential.helper cache'
sh 'git config --global push.default simple'
checkout([
$class: 'GitSCM',
branches: [[name: branch]],
extensions: [
[$class: 'CloneOption', noTags: true, reference: '', shallow: true]
],
submoduleCfg: [],
userRemoteConfigs: [
[ credentialsId: 'bitbucketUsernamePassword', url: cloneUrl]
]
])
sh "git checkout ${branch}" //To get a local branch tracking remote
Then I can do things like:
sh 'git push'
回答4:
This thread was really helpful. My Jenkins credentials are username/password so I used:
withCredentials([usernamePassword(credentialsId: 'fixed', usernameVariable: 'username', passwordVariable: 'password')]){
{
sh("git push http://$username:$password@git.corp.mycompany.com/repo")
}
The username and password are both obscured in the log:
+ git push http://****:****@git.corp.mycompany.com/repo
回答5:
Yes, it is!! After struggling for days, I ended up with these simple code block for scripted pipeline script which worked for me.
withCredentials([sshUserPrivateKey(credentialsId: '<credential-id>', keyFileVariable: 'SSH_KEY')]) {
sh("git push origin <local-branch>:<remote-branch>")
}
Enjoyy!!
回答6:
I've had to do a similar task and managed to get it to work with a variation of this: https://issues.jenkins-ci.org/browse/JENKINS-28335?focusedCommentId=320383&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-320383
withCredentials([sshUserPrivateKey(credentialsId: 'ci', keyFileVariable: 'SSH_KEY')]) {
sh 'echo ssh -i $SSH_KEY -l git -o StrictHostKeyChecking=no \\"\\$@\\" > local_ssh.sh'
sh 'chmod +x local_ssh.sh'
withEnv(['GIT_SSH=./local_ssh.sh']) {
sh 'git push origin develop'
}
}
Whereas ci is the id of the credential you setup within Jenkins. The path to the ssh key becomes available as the environment variable SSH_KEY and local_ssh.sh is added to current directory.
回答7:
In my case, I want to push to a CodeCommit repository through SSH. sshagent doesn't work because it does not set User. This eventually did the job:
withCredentials([sshUserPrivateKey(credentialsId: CODECOMMIT_CREDENTIALS_ID, keyFileVariable: 'SSH_KEY', usernameVariable: 'SSH_USER')]) {
withEnv(["GIT_SSH_COMMAND=ssh -o StrictHostKeyChecking=no -o User=${SSH_USER} -i ${SSH_KEY}"]) {
sh 'git push ${CODECOMMIT_URL} ${COMMIT_ID}:refs/heads/${BRANCH}'
}
}
回答8:
Got this working with sshagent on blue ocean (which uses https authorisation)
sshagent(credentials: ["406ef572-9598-45ee-8d39-9c9a227a9227"]) {
def repository = "git@" + env.GIT_URL.replaceFirst(".+://", "").replaceFirst("/", ":")
sh("git remote set-url origin $repository")
sh("git tag --force build-${env.BRANCH_NAME}")
sh("git push --force origin build-${env.BRANCH_NAME}")
}
回答9:
Litty Philips' answer got me most of the way but I also needed to define GIT_SSH_COMMAND.
withCredentials([sshUserPrivateKey(credentialsId: '<credential-id>', keyFileVariable: 'SSH_KEY')]) {
sh """
GIT_SSH_COMMAND = "ssh -i $SSH_KEY"
git push origin <local-branch>:<remote-branch>
"""
}
来源:https://stackoverflow.com/questions/38769976/is-it-possible-to-git-merge-push-using-jenkins-pipeline