Skip to content

Commit

Permalink
Merge pull request #9 from creative-commoners/pulls/1.0/auto-tag-disp…
Browse files Browse the repository at this point in the history
…atch

ENH Allow workflow_dispatch event
  • Loading branch information
GuySartorelli authored Aug 21, 2023
2 parents 2f731d0 + f86e00b commit 2c9d8d5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ on:
push:
tags:
- '*.*.*'
workflow_dispatch:

jobs:
auto-tag:
Expand Down
61 changes: 56 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,61 @@ runs:
using: composite
steps:

- name: Install PHP
uses: shivammathur/setup-php@1a18b2267f80291a81ca1d33e7c851fe09e7dfc4 # v2.22.0
with:
php-version: 8.1

- name: Generate tag name
id: generate_tag_name
shell: bash
env:
GITHUB_REF: ${{ github.ref }}
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_EVENT_NAME: ${{ github.event_name }}
run: |
# refs/tags/0.1.23 => 0.1.23
TAG=$(echo $GITHUB_REF | cut -c 11-)
if ! [[ $TAG =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
echo "Invalid semver tag $TAG"
exit 1
fi
TAG=""
if [[ $GITHUB_EVENT_NAME == 'tag' ]]; then
# Use the current tag name the workflow was triggered on
TAG=$(echo $GITHUB_REF | cut -c 11-)
if ! [[ $TAG =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
echo "Invalid semver tag $TAG"
exit 1
fi
else
# Find the highest semver tag on repo
# Gets 100 most recently created tags from GitHub API
# https://docs.github.com/en/rest/git/tags?apiVersion=2022-11-28
RESP_CODE=$(curl -w %{http_code} -s -o __tags.json \
-X GET "https://api.github.com/repos/$GITHUB_REPOSITORY/tags?per_page=100" \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}" \
-H "X-GitHub-Api-Version: 2022-11-28")
if [[ $RESP_CODE != "200" ]]; then
echo "Unable to read list of tags - HTTP response code was $RESP_CODE"
cat __tags.json
exit 1
fi
# Get highest semver tag from list
TAG=$(php -r '
$json = json_decode(file_get_contents("__tags.json"), true);
$tags = [];
foreach ($json as $record) {
if (preg_match("#^[0-9]+\.[0-9]+\.[0-9]+$#", $record["name"])) {
$tags[] = $record["name"];
}
}
usort($tags, "version_compare");
echo $tags[count($tags) - 1] ?? "";
')
if [[ $TAG == "" ]]; then
echo "Unable to find any semver tags on repo"
cat __tags.json
exit 1
fi
[[ $TAG =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
fi
MAJOR="${BASH_REMATCH[1]}"
MINOR="${BASH_REMATCH[2]}"
PATCH="${BASH_REMATCH[3]}"
Expand All @@ -33,3 +76,11 @@ runs:
tag: ${{ steps.generate_tag_name.outputs.tag }}
delete_existing: true
release: false

- name: Delete temporary files
shell: bash
if: always()
run: |
if [[ -f __tags.json ]]; then
rm __tags.json
fi

0 comments on commit 2c9d8d5

Please sign in to comment.