Skip to content

Commit 11ffb7b

Browse files
committed
fix(release): remove useless registry-token input and add proper default for tag input
Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
1 parent 7ce4920 commit 11ffb7b

2 files changed

Lines changed: 47 additions & 23 deletions

File tree

.github/workflows/release.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,9 @@ jobs:
5252
id-token: write
5353
packages: write
5454
secrets:
55-
# GitHub token to use for authentication.
55+
# GitHub token to use when downloading the package tarball artifact.
5656
# Defaults to `GITHUB_TOKEN` if not provided.
5757
github-token: ""
58-
59-
# Authentication token for the package registry.
60-
registry-token: ""
6158
with:
6259
# JSON array of runner(s) to use.
6360
# See https://docs.github.com/en/actions/using-jobs/choosing-the-runner-for-a-job.
@@ -77,16 +74,19 @@ jobs:
7774
# Default: `public`
7875
access: public
7976

80-
# npm distribution tag for the published package.
77+
# npm distribution tag for the published package. Leave empty to use npm defaults.
8178
# Common values:
8279
# - `latest` - Default tag for stable releases
8380
# - `next` - Prerelease or beta versions
8481
# - `canary` - Canary/nightly builds
8582
#
83+
# If omitted for a pushed Git tag, the workflow tries to reuse the Git tag
84+
# as the npm dist-tag unless it looks like a version tag such as `v1.2.3`.
85+
#
8686
# See https://docs.npmjs.com/adding-dist-tags-to-packages.
8787
#
88-
# Default: `latest`
89-
tag: latest
88+
# Default: `""`
89+
tag: ""
9090

9191
# Whether to generate npm provenance for npmjs.org publishes.
9292
# Default: `true`
@@ -113,12 +113,15 @@ jobs:
113113
| **`package-tarball-artifact-id`** | Artifact ID of the package tarball produced by CI. | **true** | **string** | - |
114114
| **`registry-url`** | Registry URL used by npm publish. | **false** | **string** | `https://registry.npmjs.org` |
115115
| **`access`** | Package access level passed to npm publish. Leave empty to use npm defaults. | **false** | **string** | `public` |
116-
| **`tag`** | npm distribution tag for the published package. | **false** | **string** | `latest` |
116+
| **`tag`** | npm distribution tag for the published package. Leave empty to use npm defaults. | **false** | **string** | `""` |
117117
| | Common values: | | | |
118118
| | - `latest` - Default tag for stable releases | | | |
119119
| | - `next` - Prerelease or beta versions | | | |
120120
| | - `canary` - Canary/nightly builds | | | |
121121
| | | | | |
122+
| | If omitted for a pushed Git tag, the workflow tries to reuse the Git tag as the | | | |
123+
| | npm dist-tag unless it looks like a version tag such as `v1.2.3`. | | | |
124+
| | | | | |
122125
| | See <https://docs.npmjs.com/adding-dist-tags-to-packages>. | | | |
123126
| **`provenance`** | Whether to generate npm provenance for npmjs.org publishes. | **false** | **boolean** | `true` |
124127
| **`dry-run`** | Whether to run npm publish without publishing the package. | **false** | **boolean** | `false` |
@@ -131,11 +134,10 @@ jobs:
131134

132135
## Secrets
133136

134-
| **Secret** | **Description** | **Required** |
135-
| -------------------- | ---------------------------------------------- | ------------ |
136-
| **`github-token`** | GitHub token to use for authentication. | **false** |
137-
| | Defaults to `GITHUB_TOKEN` if not provided. | |
138-
| **`registry-token`** | Authentication token for the package registry. | **false** |
137+
| **Secret** | **Description** | **Required** |
138+
| ------------------ | ------------------------------------------------------------------ | ------------ |
139+
| **`github-token`** | GitHub token to use when downloading the package tarball artifact. | **false** |
140+
| | Defaults to `GITHUB_TOKEN` if not provided. | |
139141

140142
<!-- secrets:end -->
141143

@@ -173,8 +175,6 @@ jobs:
173175
contents: read
174176
packages: write
175177
id-token: write
176-
secrets:
177-
registry-token: ${{ secrets.NPM_TOKEN }}
178178
with:
179179
package-tarball-artifact-id: ${{ needs.ci.outputs.package-tarball-artifact-id }}
180180
```

.github/workflows/release.yml

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,19 @@ on:
2828
default: "public"
2929
tag:
3030
description: |
31-
npm distribution tag for the published package.
31+
npm distribution tag for the published package. Leave empty to use npm defaults.
3232
Common values:
3333
- `latest` - Default tag for stable releases
3434
- `next` - Prerelease or beta versions
3535
- `canary` - Canary/nightly builds
3636
37+
If omitted for a pushed Git tag, the workflow tries to reuse the Git tag
38+
as the npm dist-tag unless it looks like a version tag such as `v1.2.3`.
39+
3740
See https://docs.npmjs.com/adding-dist-tags-to-packages.
3841
type: string
3942
required: false
40-
default: "latest"
43+
default: ""
4144
provenance:
4245
description: "Whether to generate npm provenance for npmjs.org publishes."
4346
type: boolean
@@ -51,12 +54,9 @@ on:
5154
secrets:
5255
github-token:
5356
description: |
54-
GitHub token to use for authentication.
57+
GitHub token to use when downloading the package tarball artifact.
5558
Defaults to `GITHUB_TOKEN` if not provided.
5659
required: false
57-
registry-token:
58-
description: "Authentication token for the package registry."
59-
required: false
6060

6161
permissions: {}
6262

@@ -131,10 +131,14 @@ jobs:
131131
- name: Publish package
132132
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
133133
env:
134-
NODE_AUTH_TOKEN: ${{ secrets.registry-token }}
135134
PACKAGE_TARBALL_PATH: ${{ steps.package-tarball.outputs.path }}
136135
ACCESS: ${{ inputs.access }}
137136
TAG: ${{ inputs.tag }}
137+
GITHUB_EVENT_NAME: ${{ github.event_name }}
138+
GITHUB_EVENT_REF: ${{ github.event.ref }}
139+
GITHUB_REF: ${{ github.ref }}
140+
GITHUB_REF_NAME: ${{ github.ref_name }}
141+
GITHUB_REF_TYPE: ${{ github.ref_type }}
138142
PROVENANCE: ${{ inputs.provenance }}
139143
DRY_RUN: ${{ inputs.dry-run }}
140144
REGISTRY_URL: ${{ inputs.registry-url }}
@@ -144,11 +148,31 @@ jobs:
144148
145149
const packageTarballPath = process.env.PACKAGE_TARBALL_PATH;
146150
const access = process.env.ACCESS.trim();
147-
const tag = process.env.TAG.trim();
151+
const inputTag = process.env.TAG.trim();
152+
const eventName = process.env.GITHUB_EVENT_NAME ?? '';
153+
const eventRef = process.env.GITHUB_EVENT_REF ?? '';
154+
const gitRef = process.env.GITHUB_REF ?? '';
155+
const gitRefName = process.env.GITHUB_REF_NAME ?? '';
156+
const gitRefType = process.env.GITHUB_REF_TYPE ?? '';
148157
const registryUrl = process.env.REGISTRY_URL;
149158
const dryRun = process.env.DRY_RUN === 'true';
150159
const provenance = process.env.PROVENANCE === 'true';
151160
161+
let tag = inputTag;
162+
163+
if (!tag) {
164+
const isPushTagEvent = eventName === 'push' && gitRefType === 'tag' && gitRef.startsWith('refs/tags/');
165+
const hasPushTagRef = eventRef.startsWith('refs/tags/');
166+
const canReuseGitTag = gitRefName && !/^(?:v)?\d/.test(gitRefName);
167+
168+
if ((isPushTagEvent || hasPushTagRef) && canReuseGitTag) {
169+
tag = gitRefName;
170+
core.info(`No npm dist-tag input provided. Using Git tag "${tag}" as npm dist-tag.`);
171+
} else if ((isPushTagEvent || hasPushTagRef) && gitRefName) {
172+
core.info(`No npm dist-tag input provided. Git tag "${gitRefName}" looks like a version tag, so npm will use its default dist-tag.`);
173+
}
174+
}
175+
152176
const args = ['publish', packageTarballPath];
153177
154178
if (access) {

0 commit comments

Comments
 (0)