-
Notifications
You must be signed in to change notification settings - Fork 2
/
action.yml
104 lines (98 loc) · 3.59 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
name: Auto-tag
description: Automatically delete and re-release tags so that other github action modules can use something similar to carets e.g. v0.1 or v3
runs:
using: composite
steps:
- name: Checkout code
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
with:
fetch-depth: 0
if: ${{ github.event_name == 'workflow_dispatch' }}
- name: Install PHP
uses: shivammathur/setup-php@c541c155eee45413f5b09a52248675b1a2575231 # v2.31.1
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=""
if [[ $GITHUB_EVENT_NAME == 'push' ]]; then
# Use the current tag name the workflow was triggered on
echo "Getting tag from github.ref"
TAG=$(echo $GITHUB_REF | cut -c 11-)
if ! [[ $TAG =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
echo "Invalid semver tag $TAG"
exit 1
fi
echo "TAG is $TAG"
MAJOR="${BASH_REMATCH[1]}"
MINOR="${BASH_REMATCH[2]}"
else
echo "Getting tag from github api"
# 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
echo "TAG is $TAG"
[[ $TAG =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
# Ensure we are on the correct branch prior to tagging
MAJOR="${BASH_REMATCH[1]}"
MINOR="${BASH_REMATCH[2]}"
git checkout $MAJOR.$MINOR
if [[ $? != 0 ]]; then
echo "Branch $MAJOR.$MINOR does not exist"
exit 1
fi
fi
NEW_TAG="v${MAJOR}"
if [ "$MAJOR" == "0" ]; then
NEW_TAG=("v${MAJOR}.${MINOR}")
fi
echo "NEW_TAG is $NEW_TAG"
echo "new_tag=$NEW_TAG" >> "$GITHUB_OUTPUT"
- name: Add tag to repo
uses: silverstripe/gha-tag-release@v2
with:
skip_gauge_release: true
tag: ${{ steps.generate_tag_name.outputs.new_tag }}
delete_existing: true
release: false
- name: Delete temporary files
shell: bash
if: always()
run: |
if [[ -f __tags.json ]]; then
rm __tags.json
fi