-
Notifications
You must be signed in to change notification settings - Fork 0
226 lines (226 loc) · 10.4 KB
/
publish-deno-package.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: "Publish Deno Package"
on:
release:
types:
- "published"
jobs:
main:
permissions:
contents: "read"
id-token: "write"
runs-on: "ubuntu-latest"
steps:
- name: "Checkout Repository"
uses: "actions/checkout@v4"
- name: "Setup Deno"
uses: "denoland/setup-deno@v2"
with:
deno-version: "^2.0.0"
- name: "Get Deno Cache Path"
id: "deno-cache-path"
shell: "pwsh"
run: |-
[PSCustomObject]$DenoInfo = deno info --json |
ConvertFrom-Json -Depth 100
Add-Content -LiteralPath $Env:GITHUB_OUTPUT -Value "value=$($DenoInfo.denoDir)" -Confirm:$False -Encoding 'UTF8NoBOM'
- name: "Restore Deno Cache"
id: "deno-cache-restore"
uses: "actions/cache/restore@v4"
with:
key: "${{runner.os}}/Deno/${{github.run_id}}-${{github.run_attempt}}-${{github.job}}"
restore-keys: |-
${{runner.os}}/Deno/${{github.run_id}}-${{github.run_attempt}}-
${{runner.os}}/Deno/${{github.run_id}}-
${{runner.os}}/Deno/
path: "${{steps.deno-cache-path.outputs.value}}"
- name: "Resolve Metadata"
id: "metadata"
shell: "pwsh"
run: |-
[PSCustomObject]$Config = Get-Content -LiteralPath '.\deno.jsonc' -Encoding 'UTF8NoBOM' |
ConvertFrom-Json -Depth 100
Add-Content -LiteralPath $Env:GITHUB_OUTPUT -Value "build=$(($Null -ine $Config.tasks.build).ToString().ToLower())"
Add-Content -LiteralPath $Env:GITHUB_OUTPUT -Value "jsr-publish=$(($Null -ine $Config.tasks.('jsr-publish')).ToString().ToLower())"
Add-Content -LiteralPath $Env:GITHUB_OUTPUT -Value "jsr-publish-provenance=$(($Null -ine $Config.tasks.('jsr-publish-provenance')).ToString().ToLower())"
Add-Content -LiteralPath $Env:GITHUB_OUTPUT -Value "npm-build=$(($Null -ine $Config.tasks.('npm-build')).ToString().ToLower())"
Add-Content -LiteralPath $Env:GITHUB_OUTPUT -Value "npm-root=$(Join-Path -Path $Env:GITHUB_WORKSPACE -ChildPath ($Config._behaviour.npm.root ?? 'npm'))"
- name: "Build Repository"
if: "${{steps.metadata.outputs.build == 'true'}}"
run: |-
deno task build
- name: "Publish Package To JSR (Provenance)"
id: "jsr-publish-provenance"
if: "${{steps.metadata.outputs.jsr-publish-provenance == 'true'}}"
run: |-
deno task jsr-publish-provenance
- name: "Publish Package To JSR"
if: "${{!cancelled() && ((steps.metadata.outputs.jsr-publish-provenance == 'true' && steps.jsr-publish-provenance.outcome == 'failure') || (steps.metadata.outputs.jsr-publish-provenance == 'false' && steps.metadata.outputs.jsr-publish == 'true'))}}"
run: |-
deno task jsr-publish
- name: "Build Package For NPM"
id: "npm-build"
if: "${{!cancelled() && steps.metadata.outputs.npm-build == 'true'}}"
run: |-
deno task npm-build
- name: "Save NPM Package Artifact"
id: "npm-artifact"
if: "${{!cancelled() && steps.metadata.outputs.npm-build == 'true' && steps.npm-build.outcome == 'success'}}"
uses: "actions/upload-artifact@v4"
with:
name: "npm-artifact"
path: "${{steps.metadata.outputs.npm-root}}"
if-no-files-found: "error"
retention-days: "1"
include-hidden-files: "true"
- name: "Save Deno Cache"
if: "${{!cancelled() && steps.deno-cache-restore.outcome == 'success'}}"
uses: "actions/cache/save@v4"
with:
key: "${{runner.os}}/Deno/${{github.run_id}}-${{github.run_attempt}}-${{github.job}}"
path: "${{steps.deno-cache-path.outputs.value}}"
outputs:
npm-artifact: "${{steps.npm-artifact.outcome == 'success'}}"
npm-build: "${{steps.metadata.outputs.npm-build}}"
npm:
name: "NPM"
permissions:
contents: "write"
id-token: "write"
needs:
- "main"
if: "${{!cancelled() && needs.main.outputs.npm-build == 'true' && needs.main.outputs.npm-artifact == 'true'}}"
runs-on: "ubuntu-latest"
env:
NODE_AUTH_TOKEN: "${{secrets.NPM_TOKEN}}"
steps:
- name: "Restore NPM Package Artifact"
uses: "actions/download-artifact@v4"
with:
name: "npm-artifact"
path: "${{github.workspace}}"
- name: "Setup NodeJS"
uses: "actions/setup-node@v4"
with:
node-version: "lts/*"
check-latest: true
registry-url: "https://registry.npmjs.org/"
- name: "Get NPM Cache Path"
id: "npm-cache-path"
shell: "pwsh"
run: |-
[String]$NpmCachePath = (
npm config get cache |
Join-String -Separator "`n"
).Trim()
Add-Content -LiteralPath $Env:GITHUB_OUTPUT -Value "value=$($NpmCachePath)" -Confirm:$False -Encoding 'UTF8NoBOM'
- name: "Restore NPM Cache"
id: "npm-cache-restore"
uses: "actions/cache/restore@v4"
with:
key: "${{runner.os}}/NPM/${{github.run_id}}-${{github.run_attempt}}-${{github.job}}"
restore-keys: |-
${{runner.os}}/NPM/${{github.run_id}}-${{github.run_attempt}}-
${{runner.os}}/NPM/${{github.run_id}}-
${{runner.os}}/NPM/
path: "${{steps.npm-cache-path.outputs.value}}"
- name: "Resolve Metadata"
id: "metadata"
shell: "pwsh"
run: |-
[PSCustomObject]$Manifest = Get-Content -LiteralPath '.\package.json' -Encoding 'UTF8NoBOM' |
ConvertFrom-Json -Depth 100
[SemVer]$VersionManifest = [SemVer]::Parse($Manifest.version)
[SemVer]$VersionTag = [SemVer]::Parse(('${{github.event.release.tag_name}}' -ireplace '^v', ''))
If ($VersionTag -ine $VersionManifest) {
npm version $VersionTag.ToString()
$VersionManifest = $VersionTag
}
[String]$Name = $Manifest.Name
[Boolean]$VersionIsPreRelease = $Null -ine $VersionManifest.PreReleaseLabel
[Boolean]$TagLatest = !$VersionIsPreRelease
[Boolean]$TagPre = $True
Try {
[PSCustomObject]$NpmMetadata = npm show $Name --json |
ConvertFrom-Json -Depth 100
If ($LASTEXITCODE -ne 0) {
Throw 'Package not found!'
}
If ($VersionManifest -ile [SemVer]::Parse(($NpmMetadata.'dist-tags'.latest ?? '0.0.0'))) {
$TagLatest = $False
}
If ($VersionManifest -ile [SemVer]::Parse(($NpmMetadata.'dist-tags'.pre ?? '0.0.0'))) {
$TagPre = $False
}
}
Catch {
Write-Warning -Message "Unable to resolve package NPM publish tags: $_"
$LASTEXITCODE = 0
}
[String[]]$Tags = @()
If ($TagLatest) {
$Tags += 'latest'
}
If ($TagPre) {
$Tags += 'pre'
}
If (!$VersionIsPreRelease) {
$Tags += "latest-$($VersionManifest.Major)"
}
$Tags += "pre-$($VersionManifest.Major)"
If ($Tags.Count -eq 0) {
Write-Error -Message 'No NPM publish tags!' -ErrorAction 'Stop'
}
Add-Content -LiteralPath $Env:GITHUB_OUTPUT -Value "name=$($Name)" -Confirm:$False -Encoding 'UTF8NoBOM'
Add-Content -LiteralPath $Env:GITHUB_OUTPUT -Value "version=$($VersionManifest.ToString())" -Confirm:$False -Encoding 'UTF8NoBOM'
Add-Content -LiteralPath $Env:GITHUB_OUTPUT -Value "descriptor=$($Name)@$($VersionManifest.ToString())" -Confirm:$False -Encoding 'UTF8NoBOM'
Add-Content -LiteralPath $Env:GITHUB_OUTPUT -Value "build=$(($Null -ne $Manifest.scripts.'build').ToString().ToLower())" -Confirm:$False -Encoding 'UTF8NoBOM'
Add-Content -LiteralPath $Env:GITHUB_OUTPUT -Value "tags-first=$($Tags[0])" -Confirm:$False -Encoding 'UTF8NoBOM'
Add-Content -LiteralPath $Env:GITHUB_OUTPUT -Value "tags-rest=$(
$Tags |
Select-Object -SkipIndex 0 |
Join-String -Separator ','
)" -Confirm:$False -Encoding 'UTF8NoBOM'
Add-Content -LiteralPath $Env:GITHUB_OUTPUT -Value "ballname=$($Name -ireplace '^@', '' -ireplace '\/', '-')-$($VersionManifest.ToString()).tgz" -Confirm:$False -Encoding 'UTF8NoBOM'
- name: "Install Dependencies"
if: "${{steps.metadata.outputs.build == 'true'}}"
run: |-
npm install
- name: "Build Repository"
if: "${{steps.metadata.outputs.build == 'true'}}"
run: |-
npm run build
- name: "Publish Package To NPM With Tag `${{steps.metadata.outputs.tags-first}}` (Provenance)"
id: "npm-publish-provenance"
shell: "pwsh"
run: |-
npm publish --provenance --tag '${{steps.metadata.outputs.tags-first}}'
- name: "Publish Package To NPM With Tag `${{steps.metadata.outputs.tags-first}}`"
if: "${{!cancelled() && steps.npm-publish-provenance.outcome == 'failure'}}"
id: "npm-publish-raw"
shell: "pwsh"
run: |-
npm publish --tag '${{steps.metadata.outputs.tags-first}}'
- name: "Publish Package To NPM With Rest Tags"
if: "${{!cancelled() && (steps.npm-publish-provenance.outcome == 'success' || steps.npm-publish-raw.outcome == 'success') && steps.metadata.outputs.tags-rest != ''}}"
shell: "pwsh"
run: |-
[String]$Descriptor = '${{steps.metadata.outputs.descriptor}}'
[String[]]$TagsRest = '${{steps.metadata.outputs.tags-rest}}' -isplit ','
ForEach ($TagRest In $TagsRest) {
npm dist-tag add $Descriptor $TagRest
}
- name: "Upload Package `${{steps.metadata.outputs.ballname}}` To GitHub Release Tag `${{github.event.release.tag_name}}`"
if: "${{!cancelled() && steps.metadata.outcome == 'success'}}"
env:
GH_TOKEN: "${{secrets.GITHUB_TOKEN}}"
shell: "pwsh"
run: |-
npm pack
gh release upload '${{github.event.release.tag_name}}' '${{steps.metadata.outputs.ballname}}' --clobber --repo $Env:GITHUB_REPOSITORY
- name: "Save NPM Cache"
if: "${{!cancelled() && steps.npm-cache-restore.outcome == 'success'}}"
uses: "actions/cache/save@v4"
with:
key: "${{runner.os}}/NPM/${{github.run_id}}-${{github.run_attempt}}-${{github.job}}"
path: "${{steps.npm-cache-path.outputs.value}}"