From 5f4c4336435d2c10f5401c1c3352e59eddf0b122 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Tue, 31 May 2022 16:23:17 +1200 Subject: [PATCH] MNT Add auto-tag workflow --- .github/workflows/auto-tag.yml | 14 ++++ README.md | 2 + action.yml | 113 +++++++++++++++++---------------- 3 files changed, 75 insertions(+), 54 deletions(-) create mode 100644 .github/workflows/auto-tag.yml diff --git a/.github/workflows/auto-tag.yml b/.github/workflows/auto-tag.yml new file mode 100644 index 0000000..982c17a --- /dev/null +++ b/.github/workflows/auto-tag.yml @@ -0,0 +1,14 @@ +on: + push: + tags: + - '*.*.*' +jobs: + auto-tag: + name: Auto-tag + runs-on: ubuntu-latest + steps: + - name: Auto-tag + uses: silverstripe/gha-auto-tag@main + with: + ref: ${{ github.ref }} + sha: ${{ github.sha }} diff --git a/README.md b/README.md index 84da2be..d26c6a9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # GitHub Actions - Tag release Create a tag and an optional release + +Note: this ctions seems to have issues creating tags and releases on forked repos, though it's fine on non-forked repos diff --git a/action.yml b/action.yml index 2cf86f5..463ebd3 100644 --- a/action.yml +++ b/action.yml @@ -22,8 +22,31 @@ inputs: runs: using: composite steps: + + - name: Delete existing release if one exists + if: ${{ inputs.release == 'true' && inputs.delete_existing == 'true' }} + shell: bash + env: + TAG: ${{ inputs.tag }} + run: | + echo "Deleting old release for $TAG if it exists" + # Get id for an existing release matching $TAG + curl -s \ + -X GET https://api.github.com/repos/${{ github.repository }}/releases/tags/$TAG \ + -H "Accept: application/vnd.github.v3+json" > __.json + RELEASE_ID=$(jq .id __.json) + if [ "$RELEASE_ID" != "null" ]; then + curl -s \ + -X DELETE https://api.github.com/repos/${{ github.repository }}/releases/$RELEASE_ID \ + -H "Accept: application/vnd.github.v3+json" \ + -H "Authorization: token ${{ github.token }}" + echo "Deleted existing release $RELEASE_ID for tag $TAG" + else + echo "Could not find an existing release for tag $TAG" + fi + - name: Delete existing tag if one exists - if: ${{ inputs.delete_existing }} + if: ${{ inputs.delete_existing == 'true' }} shell: bash # Add string inputs to memory instead of using string substitution in shell script # https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable @@ -39,12 +62,15 @@ runs: -H "Authorization: token ${{ github.token }}" - name: Create tag + if: ${{ inputs.release == 'false' }} shell: bash env: - SHA: ${{ inputs.branch }} + SHA: ${{ inputs.sha }} TAG: ${{ inputs.tag }} run: | - SHA=${{ github.sha }} + echo "SHA is $SHA" + echo "TAG is $TAG" + echo "url is https://api.github.com/repos/${{ github.repository }}/git/refs" # Create new tag via GitHub API # https://docs.github.com/en/rest/reference/git#create-a-reference curl -s \ @@ -57,55 +83,34 @@ runs: "ref": "refs/tags/$TAG" } EOF - echo "New tag $TAG created with sha $SHA" - -# Note: untestested below this line: + echo "New tag $TAG created for sha $SHA" -# - name: Delete existing release if one exists -# if: ${{ inputs.release && inputs.delete_existing }} -# shell: bash -# env: -# SHA: ${{ inputs.branch }} -# TAG: ${{ inputs.tag }} -# BODY: ${{ inputs.body }} -# run: | -# echo "Deleting old release for $TAG if it exists" -# # Get id for an existing release matching $TAG -# curl -s \ -# -X GET https://api.github.com/repos/silverstripe/silverstripe-framework/releases/tags/$TAG \ -# -H "Accept: application/vnd.github.v3+json" > __.json -# RELEASE_ID=$(jq .id __.json) -# if [ "$RELEASE_ID" != "null" ]; then -# curl -s \ -# -X DELETE https://api.github.com/repos/${{ github.repository }}/releases/$RELEASE_ID \ -# -H "Accept: application/vnd.github.v3+json" \ -# -H "Authorization: token ${{ github.token }}" -# echo "Deleted existing release $RELEASE_ID for tag $TAG" -# else -# echo "Could not find an existing release for tag $TAG" -# fi - -# - name: Create release -# if: ${{ inputs.release }} -# shell: bash -# env: -# SHA: ${{ inputs.branch }} -# TAG: ${{ inputs.tag }} -# BODY: ${{ inputs.body }} -# run: | -# # Run git commit, push and create pull-request as 'github-actions' user - -# # Create new pull-request via GitHub API -# # https://docs.github.com/en/rest/reference/releases#create-a-release -# curl -s \ -# -X POST https://api.github.com/repos/${{ github.repository }}/releases \ -# -H "Accept: application/vnd.github.v3+json" \ -# -H "Authorization: token ${{ github.token }}" \ -# -d @- << EOF -# { -# "tag_name": "$TAG", -# "name": "$TAG", -# "body": "$BODY", -# } -# EOF -# echo "New pull-request created" + # Creating a release will also create a tag + - name: Create release + if: ${{ inputs.release == 'true' }} + shell: bash + env: + SHA: ${{ inputs.sha }} + TAG: ${{ inputs.tag }} + BODY: ${{ inputs.body }} + run: | + # Create new release via GitHub API + # https://docs.github.com/en/rest/reference/releases#create-a-release + # Escape double quotes '"' => '\"' + BODY=${BODY//\"/\\\"} + curl -s \ + -X POST https://api.github.com/repos/${{ github.repository }}/releases \ + -H "Accept: application/vnd.github.v3+json" \ + -H "Authorization: token ${{ github.token }}" \ + -d @- << EOF + { + "tag_name": "$TAG", + "target_commitish": "$SHA", + "name": "$TAG", + "body": "$BODY", + "draft": false, + "prerelease": false + } + EOF + echo "New release $TAG created" +# ^ todo: test inputs.body with a single double quote in it \ No newline at end of file