you may want to omit the -b in "git checkout -b master" if the master 
branch already exists (seems likely).

we use this to make maven release work on any branch (maven release has a 
similar issue where it cannot work with detached head):

sh 'git checkout ' + env.BRANCH_NAME


On Friday, March 24, 2017 at 10:37:58 AM UTC-4, Idan Adar wrote:
>
> Joshua, perhaps you mean something like this?
> The git commands likely miss something...
>
> stage ("Publish to npm") {
>    steps {
>       script {
>          STAGE_NAME = "Publish to npm"
>       }
>       
>       // Prepare the workspace
>       deleteDir()
>       sh '''
>          git checkout -b master
>          npm --no-git-tag-version version minor
>          git commit -am 'Bumped version number [ci skip]'
>          git push origin master
>       '''
>       }
>    }
> }
>
>
> On Friday, March 24, 2017 at 4:31:59 PM UTC+3, Idan Adar wrote:
>>
>> Thanks for the question Joshua.
>> Here's the full Jenkinsfile: http://pastebin.com/nG4iTZhQ
>>
>> What happens is this:
>> 1. Developers make a pull request to the master branch from their feature 
>> branch.
>> 2. The pull_request webhook tells Jenkins to start working based on the 
>> stages in the Jenkinsfile. This includes: checkout, unit test, sonar 
>> analysis and merge to master.
>> 3. The effect of the merge is basically a push webhook, which means that 
>> Jenkins is now going to run the same Jenkinsfile, but on the master branch 
>> instead of the Pull Request. So, this time: checkout, unit tests and 
>> publish to npm.
>>
>> In the case where it is running from the master branch, I also bump the 
>> version number in package.json and want to push this change back to the 
>> master branch.
>> Developers will then update their feature branch with the updated 
>> package.json number...
>>
>> The part that is not working is the git push.
>> You have mentioned "localbranch". I haven't heard of this or know how to 
>> do this. Can you elaborate?
>>
>> On Friday, March 24, 2017 at 4:18:28 PM UTC+3, Joshua Noble wrote:
>>>
>>> Generally when you do a basic checkout, the Jenkins workspace is in a 
>>> detached HEAD state. You want to check out to a local branch likely using "
>>> localBranch". I have to ask though - if Jenkins does a merge of the PR 
>>> - why would you need to push?
>>>
>>> On Friday, March 24, 2017 at 7:37:57 AM UTC-4, Idan Adar wrote:
>>>>
>>>> How do you get "git push" to work in a Jenkinsfile? I am not sure what 
>>>> am I missing.
>>>>
>>>> The Jenkinsfile checkouts a repo using an access token (this is 
>>>> configured in the Jenkins UI for the job).
>>>> The Jenkins file also does a merge of a PR in one of its stages: 
>>>>
>>>> withCredentials([usernamePassword(credentialsId: '****', 
>>>> usernameVariable: 'ACCESS_TOKEN_USERNAME', passwordVariable: 
>>>> 'ACCESS_TOKEN_PASSWORD',)]) {
>>>>     sh "curl -X PUT -d '{\"commit_title\": \"Merge pull request\"}' 
>>>>  https://***/pulls/$CHANGE_ID/merge?access_token=$ACCESS_TOKEN_PASSWORD";
>>>> }
>>>>
>>>> But I can't get "git push" to work in another stage:
>>>>
>>>> git config --global user.name '****'
>>>> git config --global user.email ****
>>>> git commit -am 'Bumped version number'
>>>> git remote set-url origin git@****.git
>>>> git push origin master
>>>>
>>>> I tried wrapping the above in withCredential and also . with agent, 
>>>> but nothing works...  
>>>>
>>>> Tips?
>>>>
>>>

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/b96ad9e4-a965-4455-9ef0-f3d27cf9c06a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to