简体   繁体   中英

Failure in git push tag from a Jenkinsfile

I'm trying to add "git push tag" function to the end of my CI so I'll have tracking for commits which passed CI. I know there are some plugins but they behaved differently from what I was trying to do. That's the failure I get for the 'git push' in console:

    [Pipeline] sh
DEBUG Print - git tag -a <generated-tag-name> -m 'CI-Passed'
DEBUG Print - git push origin <branch> <generated-tag-name>
error: src refspec <branch> does not match any.
error: failed to push some refs to 'https://github.com/<account>/<account>.git'
Error when executing success post condition:
hudson.AbortException: script returned exit code 1
    at org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.handleExit(DurableTaskStep.java:569)
    at org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.check(DurableTaskStep.java:515)
    at org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.run(DurableTaskStep.java:461)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
[Pipeline] echo

However, when I copy the git push command to my console and paste it the push succeeds. How the Jenkins interperates differently the git command from my desktop?

This might be caused by a different setting of the config option push.default . (Print the current value with git config push.default )

Try to specify the local and remote ref name explicitly in your command (which is best practice in scripts anyway):

git push origin branch:branch tagname:tagname

Thanks for the reply, I tried to so and recieved an error

Error when executing success post condition:hudson.AbortException: script returned exit code 1
at org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.handleExit(DurableTaskStep.java:569)
at org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.check(DurableTaskStep.java:515)
at org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.run(DurableTaskStep.java:461)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

So to answer this question, I am assuming that your code is already checked on the branch you want to tag. If there are any changes in the files, execute the commands in following order.

git add -A    // This command will stage all the files that have been changed. 
git commit -m "added changes" // This command wil commit the changes and will create a commit id you need to tag
git tag <tag name>
git push https://${GIT_AUTHOR_NAME}:${GIT_PASSWORD}@github.com<repository>.git <tag name>"

I tried a different push command and it worked although I didn't fully understand the git outputs differences between my desktop and Jenkins Master/Slave

    def FIXED_GIT_URL= GIT_URL.replace('https://','git@').replace('com/','com:')

    status = sh(script: """#!/bin/bash
            export REP_ROOT=`git rev-parse --show-toplevel`
            export Tag=${GitTag}
            export Git_url=${FIXED_GIT_URL}
            git tag -a \${Tag} -m \\'CI-Passed\\'               
            git push \${Git_url} \${Tag}
                    """)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM