Repository navigation
114 lines (107 loc) · 4.63 KB
/
Copy pathsemver-version.yml
File metadata and controls
114 lines (107 loc) · 4.63 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
# Reusable workflow that works out the version a pull request releases from its semver label (major,
# minor or patch): on a pull request, the pre-release X.Y.Z-PRE-<number>, asking for the label when
# it is missing; on a push to the main branch, X.Y.Z from the label of the merged pull request. See
# README.md for how to call it.
name: Semver version
on:
workflow_call:
inputs:
tag-prefix:
description: Text the release tags start with (default v); empty for tags like 1.2.3
type: string
default: v
outputs:
version:
description: X.Y.Z on a push, X.Y.Z-PRE-<pull request number> on a pull request
value: ${{ jobs.version.outputs.version }}
bump:
description: major, minor or patch, from the label
value: ${{ jobs.version.outputs.bump }}
jobs:
version:
name: Semver version
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
outputs:
version: ${{ steps.version.outputs.version }}
bump: ${{ steps.label.outputs.bump }}
steps:
# all the tags, to find the latest release
- name: Check out the repository
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
fetch-depth: 0
path: repo
persist-credentials: false
# the script from the same commit as this workflow, not from the caller's repository
- name: Locate this workflow
env:
WORKFLOW_REPOSITORY: ${{ job.workflow_repository }}
WORKFLOW_SHA: ${{ job.workflow_sha }}
run: |
if [ -z "$WORKFLOW_REPOSITORY" ] || [ -z "$WORKFLOW_SHA" ]; then
echo "::error::job.workflow_repository and job.workflow_sha are empty: this workflow has to run as a reusable workflow on github.com"
exit 1
fi
echo "Using $WORKFLOW_REPOSITORY@$WORKFLOW_SHA"
- name: Check out the version script
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
repository: ${{ job.workflow_repository }}
ref: ${{ job.workflow_sha }}
path: semver-workflows
persist-credentials: false
# On a pull request, the labels it has now, not the ones of the event that started the run (see
# chart-pr.yml). On a push, the labels of the pull request the pushed commit was merged from.
- name: Read the semver label
id: label
env:
GH_TOKEN: ${{ github.token }}
EVENT: ${{ github.event_name }}
PR: ${{ github.event.pull_request.number }}
SHA: ${{ github.sha }}
run: |
if [ "$EVENT" = pull_request ]; then
labels=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR" --jq '[.labels[].name]')
else
pr=$(gh api "repos/$GITHUB_REPOSITORY/commits/$SHA/pulls" \
--jq '[.[] | select(.merged_at != null)][0] // empty | {number, labels: [.labels[].name]}')
if [ -z "$pr" ]; then
echo "::error::No merged pull request contains $SHA, so there is no semver label to release it with"
exit 1
fi
echo "Releasing pull request #$(jq -r .number <<< "$pr")"
labels=$(jq -c .labels <<< "$pr")
fi
bumps=$(jq -r '[.[] | select(. == "major" or . == "minor" or . == "patch")] | join(" ")' <<< "$labels")
if [ "$(wc -w <<< "$bumps")" -ne 1 ]; then
echo "::error::The pull request needs exactly one of the labels major, minor or patch (found: ${bumps:-none})"
exit 1
fi
echo "bump=$bumps" >> "$GITHUB_OUTPUT"
- name: Ask for the label
if: failure() && steps.label.outcome == 'failure' && github.event_name == 'pull_request'
env:
GH_TOKEN: ${{ github.token }}
PR: ${{ github.event.pull_request.number }}
run: |
gh pr comment "$PR" --repo "$GITHUB_REPOSITORY" \
--body "Please apply exactly one of the following labels to the PR: 'patch', 'minor', 'major'." \
|| echo "::warning::Could not comment on the pull request"
- name: Work out the version
id: version
working-directory: repo
env:
BUMP: ${{ steps.label.outputs.bump }}
TAG_PREFIX: ${{ inputs.tag-prefix }}
EVENT: ${{ github.event_name }}
PR: ${{ github.event.pull_request.number }}
run: |
version=$(../semver-workflows/scripts/next-version.sh)
if [ "$EVENT" = pull_request ]; then
version="$version-PRE-$PR"
fi
echo "version=$version" >> "$GITHUB_OUTPUT"
echo "Version: $version"