generated from remal-github-actions/template-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
212 lines (178 loc) · 6.98 KB
/
build.yml
File metadata and controls
212 lines (178 loc) · 6.98 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
name: build
on:
push:
branches:
- main
pull_request: { }
permissions:
id-token: write
defaults:
run:
shell: bash
jobs:
build:
if: ${{
(github.event_name == 'push' && startsWith(github.ref, 'refs/tags/'))
|| (github.event_name == 'push' && !startsWith(github.event.head_commit.message, '[push-back]'))
|| github.event_name != 'push'
}}
concurrency:
group: build-${{github.ref}}
cancel-in-progress: true
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Get GitHub Token
id: get-token
uses: remal/github-repository-token-issuer@v1
with:
scopes: |
contents: write
workflows: write
- name: Checkout repository
uses: actions/checkout@v6
with:
token: ${{steps.get-token.outputs.token}}
submodules: recursive
lfs: true
fetch-depth: 1
- name: Read action's Node.js version
id: read-node-version
run: |
USING=$( yq '.runs.using' ./action.yml )
if ! [[ "$USING" =~ ^node[0-9]+$ ]]; then
echo "::error::action.yml: .runs.using doesn't start with 'node': '$USING'"
exit 1
fi
VERSION=${USING:4}
echo "Action's Node.js version: $VERSION"
echo "result=$VERSION" | tee -a $GITHUB_OUTPUT
- name: Setup Node.js ${{steps.read-node-version.outputs.result}}
uses: actions/setup-node@v6
with:
node-version: '${{steps.read-node-version.outputs.result}}'
- name: Update Node.js version in project files
run: |
node update-node-version-in-files "${{steps.read-node-version.outputs.result}}"
- name: Get package manager
id: package-manager
run: |
PACKAGE_MANAGER=$( yq -r '.packageManager' ./package.json )
PACKAGE_MANAGER=${PACKAGE_MANAGER%%@*}
if ! [[ "$PACKAGE_MANAGER" =~ ^[a-z]+$ ]]; then
echo "::error::package.json: unsupported .packageManager format: '$PACKAGE_MANAGER'"
exit 1
fi
echo "PACKAGE_MANAGER=$PACKAGE_MANAGER" | tee -a $GITHUB_ENV
echo "result=$PACKAGE_MANAGER" | tee -a $GITHUB_OUTPUT
PACKAGE_MANAGER_NAME=${PACKAGE_MANAGER^}
echo "name=$PACKAGE_MANAGER_NAME" | tee -a $GITHUB_OUTPUT
- name: Create cache params
id: cache-params
run: |
echo "key=${{runner.os}}-dependencies-${{steps.read-node-version.outputs.result}}-${{hashFiles('package.json', 'pnpm-*.yml', '.yarnrc.yml', 'yarn.lock' , '.pnp.*', '.yarn/patches/**', '.yarn/plugins/**', '.yarn/releases/**', '.yarn/versions/**')}}" | tee -a $GITHUB_OUTPUT
echo "paths<<EOF" | tee -a $GITHUB_OUTPUT
echo "node_modules" | tee -a $GITHUB_OUTPUT
echo ".yarn/cache" | tee -a $GITHUB_OUTPUT
echo "EOF" | tee -a $GITHUB_OUTPUT
- name: Restore dependencies cache
id: restore-dependencies-cache
uses: actions/cache/restore@v5
with:
key: ${{steps.cache-params.outputs.key}}
path: ${{steps.cache-params.outputs.paths}}
- name: Install dependencies
run: |
if [ "${{steps.package-manager.outputs.result}}" == "pnpm" ]; then
pnpm install --no-frozen-lockfile
elif [ "${{steps.package-manager.outputs.result}}" == "yarn" ]; then
yarn install --no-immutable
else
echo "::error::Unsupported package manager: $PACKAGE_MANAGER"
exit 1
fi
- name: Save dependencies cache
if: ${{steps.restore-dependencies-cache.outputs.cache-hit != 'true'}}
uses: actions/cache/save@v5
with:
key: ${{steps.cache-params.outputs.key}}
path: ${{steps.cache-params.outputs.paths}}
- name: Build
run: |
${{steps.package-manager.outputs.result}} run build
- name: Upload dist to artifacts
if: ${{always()}}
uses: actions/upload-artifact@v7
with:
name: dist
path: dist/
if-no-files-found: error
overwrite: true
retention-days: 30
# $$$sync-with-template-modifiable: validation $$$
- name: Run current action
uses: ./
with:
dryRun: 'true'
githubToken: ${{github.token}}
commitMessage: '[push-back] Bump repository activity'
# $$$sync-with-template-modifiable-end$$$
- name: Push back
if: ${{github.event_name == 'push' && startsWith(github.ref, 'refs/heads/')}}
uses: remal-github-actions/push-back@v2
with:
message: '[push-back] Push-back updated files during build'
- name: Read version
id: readVersion
if: ${{github.event_name == 'push' && github.ref == 'refs/heads/main'}}
uses: remal-github-actions/read-nodejs-package-version@v1
- name: Remove files listed in .releaseignore
if: ${{github.event_name == 'push' && github.ref == 'refs/heads/main'}}
id: releaseignore
uses: actions/github-script@v8
with:
script: |
const fs = require('fs')
const path = require('path')
const cwdPath = path.resolve(process.cwd())
const releaseIgnorePath = '.releaseignore'
const releaseIgnoreFullPath = path.resolve(cwdPath, releaseIgnorePath)
if (!fs.existsSync(releaseIgnoreFullPath)) {
core.info(`${releaseIgnorePath} file does not exist, skipping removal of files.`)
core.setOutput('skipped', 'true')
return
}
let patternsToIgnore = fs.readFileSync(releaseIgnoreFullPath, 'utf-8')
.split('\n')
.map(line => line.replace(/#.*/, '').trim())
.filter(line => line.length)
.join('\n')
if (!patternsToIgnore.length) {
core.info(`${releaseIgnorePath} file does not have patterns, skipping removal of files.`)
core.setOutput('skipped', 'true')
return
}
const gitDirPath = path.resolve(cwdPath, '.git')
const globber = await glob.create(patternsToIgnore)
const files = await globber.glob()
files
.map(file => path.resolve(cwdPath, file))
.filter(file => file != cwdPath)
.filter(file => file != gitDirPath && !file.startsWith(gitDirPath + path.sep))
.forEach(file => {
core.debug(`Removing: ${file}`)
fs.rmSync(file, { recursive: true, force: true })
})
- name: Prepare release commit
if: ${{github.event_name == 'push' && github.ref == 'refs/heads/main' && !steps.releaseignore.outputs.skipped}}
run: |
RELEASE_BRANCH="release/v${{steps.readVersion.outputs.majorVersion}}"
git switch --force-create "$RELEASE_BRANCH"
git add --all
git commit -m "[skip-ci] Release v${{steps.readVersion.outputs.majorVersion}}" || true
git push --force origin "$RELEASE_BRANCH"
- name: Create tag
if: ${{github.event_name == 'push' && github.ref == 'refs/heads/main'}}
uses: remal-github-actions/create-tag@v2
with:
tagName: 'v${{steps.readVersion.outputs.majorVersion}}'