-
Notifications
You must be signed in to change notification settings - Fork 0
164 lines (139 loc) · 5 KB
/
Copy pathversion.yml
File metadata and controls
164 lines (139 loc) · 5 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
name: Version Tagger
on:
workflow_run:
workflows: ["CI"]
types: [completed]
workflow_dispatch:
inputs:
bump:
description: "Version segment to bump"
required: false
default: patch
type: choice
options:
- patch
- minor
- major
- revision
prefix:
description: "Tag prefix"
required: false
default: v
type: string
concurrency:
group: version-${{ github.ref }}
cancel-in-progress: false
permissions:
actions: write
contents: write
jobs:
tag:
runs-on: ubuntu-latest
if: >
github.event_name == 'workflow_dispatch' ||
(github.event_name == 'workflow_run' &&
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.head_branch == 'main')
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_sha || github.sha }}
- name: Fetch tags
run: git fetch --tags --force
- name: Detect bump type
id: bump
shell: bash
run: |
set -euo pipefail
if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ -n "${{ github.event.inputs.bump }}" ]; then
echo "value=${{ github.event.inputs.bump }}" >> "$GITHUB_OUTPUT"
exit 0
fi
prefix="${{ github.event.inputs.prefix || 'v' }}"
latest=$(git tag --list "${prefix}*" --sort=-v:refname | head -n 1)
# Auto-mode from commits since latest version tag:
# major -> BREAKING CHANGE or !: marker, minor -> feat:, else patch.
if [ -n "$latest" ]; then
range="${latest}..HEAD"
else
range="HEAD"
fi
messages=$(git log --format=%B "$range" || true)
bump="patch"
if echo "$messages" | grep -Eiq 'BREAKING CHANGE|^[^\n]*!:'; then
bump="major"
elif echo "$messages" | grep -Eiq '^feat(\(|:)|^feature(\(|:)'; then
bump="minor"
fi
echo "value=$bump" >> "$GITHUB_OUTPUT"
- name: Determine next version tag
id: next
shell: bash
run: |
set -euo pipefail
prefix="${{ github.event.inputs.prefix || 'v' }}"
bump="${{ steps.bump.outputs.value }}"
# If HEAD is already tagged with the configured prefix, do nothing.
existing_head_tag=$(git tag --points-at HEAD --list "${prefix}*" | head -n 1 || true)
if [ -n "$existing_head_tag" ]; then
echo "already_tagged=true" >> "$GITHUB_OUTPUT"
echo "latest=$existing_head_tag" >> "$GITHUB_OUTPUT"
echo "tag=$existing_head_tag" >> "$GITHUB_OUTPUT"
exit 0
fi
latest=$(git tag --list "${prefix}*" --sort=-v:refname | head -n 1)
if [ -z "$latest" ]; then
latest="${prefix}0.0.0.0"
fi
version="${latest#${prefix}}"
IFS='.' read -r major minor patch revision <<< "$version"
major=${major:-0}
minor=${minor:-0}
patch=${patch:-0}
revision=${revision:-0}
case "$bump" in
major)
major=$((major + 1)); minor=0; patch=0; revision=0 ;;
minor)
minor=$((minor + 1)); patch=0; revision=0 ;;
patch)
patch=$((patch + 1)); revision=0 ;;
revision)
revision=$((revision + 1)) ;;
*)
echo "Unknown bump type: $bump" >&2
exit 1 ;;
esac
new_tag="${prefix}${major}.${minor}.${patch}.${revision}"
if git rev-parse -q --verify "refs/tags/${new_tag}" >/dev/null; then
echo "Tag already exists: ${new_tag}" >&2
exit 1
fi
echo "already_tagged=false" >> "$GITHUB_OUTPUT"
echo "latest=$latest" >> "$GITHUB_OUTPUT"
echo "tag=$new_tag" >> "$GITHUB_OUTPUT"
- name: Create and push tag
if: steps.next.outputs.already_tagged != 'true'
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag "${{ steps.next.outputs.tag }}"
git push origin "${{ steps.next.outputs.tag }}"
- name: Trigger Release workflow
if: steps.next.outputs.already_tagged != 'true'
env:
GH_TOKEN: ${{ github.token }}
run: |
gh workflow run release.yml --ref main -f tag_name="${{ steps.next.outputs.tag }}"
- name: Summary
run: |
echo "Bump : ${{ steps.bump.outputs.value }}"
echo "Previous: ${{ steps.next.outputs.latest }}"
echo "Tag : ${{ steps.next.outputs.tag }}"
if [ "${{ steps.next.outputs.already_tagged }}" = "true" ]; then
echo "Result : skipped (HEAD already tagged)"
else
echo "Result : created"
fi