Skip to content

Commit

Permalink
feature: add option to use git tag / git push instead of http soft ta…
Browse files Browse the repository at this point in the history
…gs (#210)
  • Loading branch information
sbe-arg committed Jun 13, 2023
1 parent 698f1bb commit a2c70ae
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 23 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion .github/workflows/test.yml
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`" ]
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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).
Expand Down
52 changes: 30 additions & 22 deletions entrypoint.sh
Expand Up @@ -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}
Expand All @@ -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}"
Expand Down Expand Up @@ -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

0 comments on commit a2c70ae

Please sign in to comment.