Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Allow workflow_dispatch event #9

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"])) {
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
$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