From a2c70ae13a881faf2b4953baaa9e49731997ab36 Mon Sep 17 00:00:00 2001 From: Santiago Bernhardt Date: Wed, 14 Jun 2023 09:21:00 +1200 Subject: [PATCH] feature: add option to use git tag / git push instead of http soft tags (#210) --- .github/workflows/main.yml | 1 + .github/workflows/test.yml | 24 +++++++++++++++++- README.md | 1 + entrypoint.sh | 52 ++++++++++++++++++++++---------------- 4 files changed, 55 insertions(+), 23 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 534411ca..f582fdd2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,6 +28,7 @@ jobs: env: VERBOSE: true GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GIT_API_TAGGING: false # uses git cli # auto releases is not working atm and is deleting releases due branch tags - name: automatic-draft-release diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5e80bc33..77264ac8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -66,6 +66,16 @@ jobs: DEFAULT_BUMP: none GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Test action main5 (with_v true) + id: test_main5 + uses: ./ + env: + DRY_RUN: true + WITH_V: true + VERBOSE: true + DEFAULT_BUMP: patch + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # Use the action to generate a tag for itself - name: Test action pre1-release (with_v true) id: test_pre1 @@ -118,6 +128,10 @@ jobs: MAIN4_OUTPUT_NEWTAG=${{ steps.test_main4.outputs.new_tag }} MAIN4_OUTPUT_PART=${{ steps.test_main4.outputs.part }} + MAIN5_OUTPUT_TAG=${{ steps.test_main5.outputs.old_tag }} + MAIN5_OUTPUT_NEWTAG=${{ steps.test_main5.outputs.new_tag }} + MAIN5_OUTPUT_PART=${{ steps.test_main5.outputs.part }} + echo -e "> MAIN tests with_v, default bump:\n" >> $GITHUB_STEP_SUMMARY echo "MAIN1 with_v Tag: $MAIN1_OUTPUT_TAG" >> $GITHUB_STEP_SUMMARY @@ -154,6 +168,12 @@ jobs: echo "MAIN4 with_v New tag: $MAIN4_OUTPUT_NEWTAG" >> $GITHUB_STEP_SUMMARY echo "MAIN4 with_v Part: $MAIN4_OUTPUT_PART" >> $GITHUB_STEP_SUMMARY + echo -e "> MAIN tests with_v, bump patch:\n" >> $GITHUB_STEP_SUMMARY + + echo "MAIN5 with_v Tag: $MAIN5_OUTPUT_TAG" >> $GITHUB_STEP_SUMMARY + echo "MAIN5 with_v New tag: $MAIN5_OUTPUT_NEWTAG" >> $GITHUB_STEP_SUMMARY + echo "MAIN5 with_v Part: $MAIN5_OUTPUT_PART" >> $GITHUB_STEP_SUMMARY + # check that the original tag got bumped either major, minor, patch verlte() { [ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ] @@ -171,8 +191,10 @@ jobs: # needs to be the latest tag of the repo when bump is none main3="$([ "$MAIN3_OUTPUT_TAG" = "$MAIN3_OUTPUT_NEWTAG" ] && true || false)" main4="$([ "$MAIN4_OUTPUT_TAG" = "$MAIN4_OUTPUT_NEWTAG" ] && true || false)" + # needs to be a greater tag in bump patch + main5="$(verlt $MAIN5_OUTPUT_TAG $MAIN5_OUTPUT_NEWTAG && true || false)" - if $main1 && $pre1 && $main2 && $pre2 && $main3 && $main4 + if $main1 && $pre1 && $main2 && $pre2 && $main3 && $main4 && $main5 then echo -e "\n>>>>The tags were created correctly" >> $GITHUB_STEP_SUMMARY else diff --git a/README.md b/README.md index eabac226..0d2c3c4e 100755 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ _NOTE: set the fetch-depth for `actions/checkout@v2` or newer to be sure you ret - **CUSTOM_TAG** _(optional)_ - Set a custom tag, useful when generating tag based on f.ex FROM image in a docker image. **Setting this tag will invalidate any other settings set!** - **SOURCE** _(optional)_ - Operate on a relative path under $GITHUB_WORKSPACE. - **DRY_RUN** _(optional)_ - Determine the next version without tagging the branch. The workflow can use the outputs `new_tag` and `tag` in subsequent steps. Possible values are `true` and `false` (default). +- **GIT_API_TAGGING** _(optional)_ - Set if using git cli or git api calls for tag push operations. Possible values are `false` and `true` (default). - **INITIAL_VERSION** _(optional)_ - Set initial version before bump. Default `0.0.0`. - **TAG_CONTEXT** _(optional)_ - Set the context of the previous tag. Possible values are `repo` (default) or `branch`. - **PRERELEASE** _(optional)_ - Define if workflow runs in prerelease mode, `false` by default. Note this will be overwritten if using complex suffix release branches. Use it with checkout `ref: ${{ github.sha }}` for consistency see [issue 266](https://github.com/anothrNick/github-tag-action/issues/266). diff --git a/entrypoint.sh b/entrypoint.sh index 28dd1ac5..83e3d39a 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -10,6 +10,7 @@ release_branches=${RELEASE_BRANCHES:-master,main} custom_tag=${CUSTOM_TAG:-} source=${SOURCE:-.} dryrun=${DRY_RUN:-false} +git_api_tagging=${GIT_API_TAGGING:-true} initial_version=${INITIAL_VERSION:-0.0.0} tag_context=${TAG_CONTEXT:-repo} prerelease=${PRERELEASE:-false} @@ -33,6 +34,7 @@ echo -e "\tRELEASE_BRANCHES: ${release_branches}" echo -e "\tCUSTOM_TAG: ${custom_tag}" echo -e "\tSOURCE: ${source}" echo -e "\tDRY_RUN: ${dryrun}" +echo -e "\tGIT_API_TAGGING: ${git_api_tagging}" echo -e "\tINITIAL_VERSION: ${initial_version}" echo -e "\tTAG_CONTEXT: ${tag_context}" echo -e "\tPRERELEASE: ${prerelease}" @@ -238,36 +240,42 @@ then exit 0 fi +echo "EVENT: creating local tag $new" # create local git tag -git tag "$new" +git tag -f "$new" || exit 1 +echo "EVENT: pushing tag $new to origin" -# push new tag ref to github -# this needs permissions in the workflow as contents: write -dt=$(date '+%Y-%m-%dT%H:%M:%SZ') -full_name=$GITHUB_REPOSITORY -git_refs_url=$(jq .repository.git_refs_url "$GITHUB_EVENT_PATH" | tr -d '"' | sed 's/{\/sha}//g') - -echo "$dt: **pushing tag $new to repo $full_name" +if $git_api_tagging +then + # use git api to push + dt=$(date '+%Y-%m-%dT%H:%M:%SZ') + full_name=$GITHUB_REPOSITORY + git_refs_url=$(jq .repository.git_refs_url "$GITHUB_EVENT_PATH" | tr -d '"' | sed 's/{\/sha}//g') -git_refs_response=$( -curl -s -X POST "$git_refs_url" \ --H "Authorization: token $GITHUB_TOKEN" \ --d @- << EOF + echo "$dt: **pushing tag $new to repo $full_name" + git_refs_response=$( + curl -s -X POST "$git_refs_url" \ + -H "Authorization: token $GITHUB_TOKEN" \ + -d @- << EOF { - "ref": "refs/tags/$new", - "sha": "$commit" + "ref": "refs/tags/$new", + "sha": "$commit" } EOF ) -git_ref_posted=$( echo "${git_refs_response}" | jq .ref | tr -d '"' ) + git_ref_posted=$( echo "${git_refs_response}" | jq .ref | tr -d '"' ) -echo "::debug::${git_refs_response}" -if [ "${git_ref_posted}" = "refs/tags/${new}" ] -then - exit 0 + echo "::debug::${git_refs_response}" + if [ "${git_ref_posted}" = "refs/tags/${new}" ] + then + exit 0 + else + echo "::error::Tag was not created properly." + exit 1 + fi else - echo "::error::Tag was not created properly." - exit 1 -fi + # use git cli to push + git push -f origin "$new" || exit 1 +fi \ No newline at end of file