0

Need help for creating tag for every bit bucket commit.

Please let me know if anyone have implemented this using Jenkins pipeline, if yes how can we achieve that

Varun
  • 35
  • 4

2 Answers2

0

I'm going to assume you have some sort of Multi-branch pipeline job set up, or some mechanism to trigger a jenkins job for each commit to BitBucket. Bitbucket will need to trigger a Jenkins job for each commit via a webhook

In your pipeline code, you'll need to do the following:

  1. Get the hash of your commit. You can get this data from the GIT_COMMIT variable in the Git Jenkins Plugin
  2. Create a Bitbucket tag via their api with the commit hash mentioned above
curl https://api.bitbucket.org/2.0/repositories/jdoe/myrepo/refs/tags \
-s -u jdoe -X POST -H "Content-Type: application/json" \
-d '{
    "name" : "new-tag-name",
    "target" : {
        "hash" : "a1b2c3d4e5f6",
    }
}'

There are few different ways to do #2.

  • Put that code in a powershell script and run it from your job.
  • Call it directly from your jenkins pipeline like this stackoverflow answer.
  • Or if you plan to do this from many jobs, you could also create your own Bitbucket Api Client shared library that does the api interaction for you.

Note: You'll likely need to authenticate these Bitbucket api requests

  • Thank You for response. I tried below pipeline and it is not working, please let me know if something is wrong. stage("Git Tagging"){ steps{ withCredentials([usernameColonPassword(credentialsId: 'SCM_Credential_ID', variable: 'USERPASS')]){ sh ''' curl -X POST -k -H 'Content-Type: application/json' -s -u $USERPASS -i 'https:///refs/tags' --data '{ "name": "Test-Tag", "target":{ "hash": "" // "hash": "$(GIT_COMMIT)" did not work } }' ''' }}} – Varun Oct 13 '21 at 11:12
  • Did the Bitbucket api give you an error message? –  Oct 13 '21 at 16:57
  • It dosent recognize GIT_COMMIT – Varun Oct 18 '21 at 07:35
  • I think you might need an 'env.' in front of GIT_COMMIT ("$(env.GIT_COMMIT)"). Per https://stackoverflow.com/a/38657848/17128827. –  Oct 18 '21 at 17:32
  • Issue is resolved with env.GIT_COMMIT – Varun Nov 09 '21 at 14:20
0

I was able to get rid of error using env.GIT_COMMIT

Varun
  • 35
  • 4