-
Notifications
You must be signed in to change notification settings - Fork 0
168 lines (153 loc) · 7.42 KB
/
Copy pathcode-release_preview.yml
File metadata and controls
168 lines (153 loc) · 7.42 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
165
166
167
168
# SPDX-FileCopyrightText: 2026 INDUSTRIA DE DISEÑO TEXTIL S.A. (INDITEX S.A.)
# SPDX-License-Identifier: Apache-2.0
name: code-release-preview
on:
pull_request:
types: [labeled, synchronize, ready_for_review, opened, reopened]
branches: [main, main-*, develop, develop-*]
permissions:
contents: read
concurrency:
group: code-release-preview-${{ github.repository }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
env:
PROJECT_TYPE: ${{ vars.PROJECT_TYPE }}
WORKING_DIRECTORY: ${{ vars.WORKING_DIRECTORY }}
jobs:
release-preview:
name: Release Preview
if: >-
github.event.pull_request.draft == false &&
github.event.pull_request.head.repo.full_name == github.repository &&
(
(vars.DEVELOPMENT_FLOW == 'trunk-based-development' && (github.event.pull_request.base.ref == 'main' || startsWith(github.event.pull_request.base.ref, 'main-'))) ||
(vars.DEVELOPMENT_FLOW == 'git-flow' && (github.event.pull_request.base.ref == 'main' || startsWith(github.event.pull_request.base.ref, 'main-')))
)
runs-on: ubuntu-24.04
timeout-minutes: 15
permissions:
contents: read
pull-requests: write # Publishes the calculated release preview.
steps:
- name: Check out pull request head
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
persist-credentials: false
ref: ${{ github.event.pull_request.head.sha }}
- name: Check CHANGELOG has Unreleased changes
# Skip for PRs that opt out of a release (skip-release label) and for the engine's automated reconcile PR, which never carries release content.
if: >-
!contains(github.event.pull_request.labels.*.name, 'skip-release') &&
github.event.pull_request.head.ref != 'automated/ci-governance-sync'
env:
GH_TOKEN: ${{ github.token }}
GITHUB_EVENT_NUMBER: ${{ github.event.pull_request.number }}
BASE_REF: ${{ github.event.pull_request.base.ref }}
working-directory: ${{ env.WORKING_DIRECTORY }}
shell: bash
run: |
set -euo pipefail
if git diff --quiet "origin/$BASE_REF"...HEAD -- CHANGELOG.md; then
echo "::error title=No CHANGELOG changes::Update the Unreleased section with this release's changes."
gh pr comment "$GITHUB_EVENT_NUMBER" --repo "$GITHUB_REPOSITORY" --body $'### :x: No changes in `CHANGELOG.md`\nUpdate the `## [Unreleased]` section listing the changes for this release before merging.'
exit 1
fi
- name: Calculate release preview
env:
DEVELOPMENT_FLOW: ${{ vars.DEVELOPMENT_FLOW }}
GITHUB_EVENT_NUMBER: ${{ github.event.pull_request.number }}
GH_TOKEN: ${{ github.token }}
RELEASE_LABELS: ${{ join(github.event.pull_request.labels.*.name, ',') }}
working-directory: ${{ env.WORKING_DIRECTORY }}
shell: bash
run: |
set -euo pipefail
if [[ ",$RELEASE_LABELS," == *",skip-release,"* ]]; then
body=$'### Release Preview\n\nThis pull request has the `skip-release` label and will not create an npm release.'
gh pr comment "$GITHUB_EVENT_NUMBER" \
--repo "$GITHUB_REPOSITORY" \
--body "$body"
exit 0
fi
case ",$RELEASE_LABELS," in
*",release-type/major,"*) release_bump="major" ;;
*",release-type/minor,"*) release_bump="minor" ;;
*",release-type/hotfix,"*|*",release-type/multi-hotfix,"*|*",release-type/patch,"*)
release_bump="patch"
;;
*)
if [[ "$DEVELOPMENT_FLOW" == "trunk-based-development" ]]; then
release_bump="minor"
else
body=$'### Release Preview\n\nAdd a `release-type/...` label to calculate a release for this git-flow pull request.'
gh pr comment "$GITHUB_EVENT_NUMBER" \
--repo "$GITHUB_REPOSITORY" \
--body "$body"
exit 0
fi
;;
esac
merge_strategy="Create a merge commit"
if [[ "$DEVELOPMENT_FLOW" == "trunk-based-development" ]]; then
merge_strategy="Squash and merge"
fi
# npm version helpers (mirror release-core): release version is strip-SNAPSHOT of each unit's package.json; the release-type label only drives the next-dev bump.
member_dir() { # <npm package name> -> flat workspaces member directory
# @scope/api -> api, client_lib -> client-lib (mirrors the CREATE scaffolder).
printf '%s' "${1##*/}" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9]+/-/g; s/^-+//; s/-+$//'
}
strip_snapshot() { printf '%s' "${1%-SNAPSHOT}"; }
read_version() { # <dir under WORKING_DIRECTORY, empty for the root>
local manifest="package.json"
[[ -n "$1" ]] && manifest="$1/package.json"
jq -r '.version' "$manifest"
}
bump_semver() { # <X.Y.Z> <major|minor|patch>
local major minor patch
IFS=. read -r major minor patch <<< "$1"
case "$2" in
major) major=$((major + 1)); minor=0; patch=0 ;;
minor) minor=$((minor + 1)); patch=0 ;;
patch) patch=$((patch + 1)) ;;
esac
printf '%s.%s.%s' "$major" "$minor" "$patch"
}
if [[ "$PROJECT_TYPE" == "single" ]]; then
current_version="$(read_version "")"
release_version="$(strip_snapshot "$current_version")"
next_dev="$(bump_semver "$release_version" "$release_bump")-SNAPSHOT"
versions="$(printf '%s\n' \
"- Current version: \`$current_version\`" \
"- Proposed release version: \`$release_version\`" \
"- Next development version: \`$next_dev\`")"
elif [[ "$PROJECT_TYPE" == "workspaces" ]]; then
descriptor="$GITHUB_WORKSPACE/.github/inditextech-ci-node.json"
release_strategy="$(jq -r '.release.strategy' "$descriptor")"
versions="- Reactor release strategy: \`$release_strategy\`"
while IFS= read -r package; do
member="$(member_dir "$package")"
current_version="$(read_version "packages/$member")"
release_version="$(strip_snapshot "$current_version")"
next_dev="$(bump_semver "$release_version" "$release_bump")-SNAPSHOT"
versions+=$'\n'"- \`$package\` (packages/$member): \`$current_version\` -> \`$release_version\` (next dev: \`$next_dev\`)"
done < <(jq -r '.release.packages[]' "$descriptor")
else
echo "::error title=Invalid project type::PROJECT_TYPE must be single or workspaces."
exit 1
fi
changes="$(awk '
BEGIN { capture=0 }
/^## \[[Uu]nreleased\]/ { capture=1; next }
capture && /^## \[/ { capture=0 }
capture { print }
' CHANGELOG.md)"
body="$(printf "%s\n\n%s\n- Release type: \`%s\`\n- Expected merge strategy: **%s**\n\n#### Changes\n%s" \
'### Node Release Preview' \
"$versions" \
"$release_bump" \
"$merge_strategy" \
"$changes")"
gh pr comment "$GITHUB_EVENT_NUMBER" \
--repo "$GITHUB_REPOSITORY" \
--body "$body"