Skip to content

Commit

Permalink
MNT Add auto-tag workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed May 31, 2022
1 parent 86cec8d commit e79a1d6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 53 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/auto-tag.yml
Original file line number Diff line number Diff line change
@@ -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 }}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
113 changes: 60 additions & 53 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,12 +62,17 @@ 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: |
# TODO: remove
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 \
Expand All @@ -57,55 +85,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

0 comments on commit e79a1d6

Please sign in to comment.