Skip to content

Commit e22bed0

Browse files
authored
Merge pull request #498 from saasquatch/unify-npm-deploy-action
Centralize publishing to publish-package action
2 parents 53a819b + 193aa0e commit e22bed0

2 files changed

Lines changed: 93 additions & 16 deletions

File tree

.github/workflows/mint-components-release.yml

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ permissions:
1313
issues: read
1414

1515
jobs:
16-
release:
16+
version:
1717
runs-on: ubuntu-latest
18-
name: Release Mint Components
18+
name: Version Mint Components
19+
outputs:
20+
should-publish: ${{ steps.check-publish.outputs.should-publish }}
1921
defaults:
2022
run:
2123
working-directory: packages/mint-components
@@ -32,34 +34,76 @@ jobs:
3234
- name: Install packages
3335
run: npm ci
3436

35-
- name: Create release pull request or publish to npm
37+
- name: Create release pull request
3638
id: changesets
3739
uses: changesets/action@001cd79f0a536e733315164543a727bdf2d70aff # 001cd = v1.5.1
3840
with:
3941
cwd: packages/mint-components
4042
version: npm run version
41-
publish: npm run release
4243
title: "mint-components - Version Packages"
4344
env:
4445
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
45-
NPM_CONFIG_PROVENANCE: true
46+
47+
- name: Check if version needs publishing
48+
id: check-publish
49+
if: steps.changesets.outputs.hasChangesets == 'false'
50+
run: |
51+
VERSION=$(jq -r '.version' package.json)
52+
OUTPUT=$(npm view "@saasquatch/mint-components@$VERSION" version 2>&1) && {
53+
echo "should-publish=false" >> $GITHUB_OUTPUT
54+
echo "Version $VERSION is already published"
55+
exit 0
56+
}
57+
if echo "$OUTPUT" | grep -q "E404"; then
58+
echo "should-publish=true" >> $GITHUB_OUTPUT
59+
echo "Version $VERSION not found on npm, publishing"
60+
else
61+
echo "::error::npm view failed unexpectedly: $OUTPUT"
62+
exit 1
63+
fi
64+
65+
publish:
66+
needs: version
67+
if: needs.version.outputs.should-publish == 'true'
68+
uses: ./.github/workflows/publish-package.yml
69+
with:
70+
package: mint-components
71+
npm-tag: latest
72+
permissions:
73+
contents: write
74+
id-token: write
75+
76+
deploy-docs:
77+
needs: publish
78+
runs-on: ubuntu-latest
79+
name: Deploy Docs
80+
defaults:
81+
run:
82+
working-directory: packages/mint-components
83+
steps:
84+
- name: Checkout code
85+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # de0fa = v6.0.2
86+
87+
- name: Setup Node.js
88+
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # 53b83 = v6.3.0
89+
with:
90+
node-version: 24
91+
92+
- name: Install packages
93+
run: npm ci
4694

4795
- name: Build docs site
48-
if: steps.changesets.outputs.published == 'true'
4996
run: NODE_ENV=dev npm run build
5097

5198
- name: Authenticate with Google Cloud
52-
if: steps.changesets.outputs.published == 'true'
5399
uses: google-github-actions/auth@7c6bc770dae815cd3e89ee6cdf493a5fab2cc093 # 7c6bc = v3
54100
with:
55101
token_format: "access_token"
56102
workload_identity_provider: "projects/167728399658/locations/global/workloadIdentityPools/github/providers/github"
57103
service_account: "github-static-site-actions@static-site-proxy.iam.gserviceaccount.com"
58104

59105
- name: Setup GCP
60-
if: steps.changesets.outputs.published == 'true'
61106
uses: google-github-actions/setup-gcloud@aa5489c8933f4cc7a4f7d45035b3b1440c9c10db # aa548 = v3.0.1
62107

63108
- name: Deploy docs to bucket
64-
if: steps.changesets.outputs.published == 'true'
65109
run: gsutil -m rsync -d -r www/ gs://stencilbook_static_saasquatch/mint-components/production/

.github/workflows/publish-package.yml

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ on:
2626
- minor
2727
- major
2828

29+
workflow_call:
30+
inputs:
31+
package:
32+
description: "The package to publish (must be one of: express-boilerplate, integration-boilerplate-node, logger, mint-components, program-boilerplate, program-test-suite, publish-helper)"
33+
required: true
34+
type: string
35+
npm-tag:
36+
description: The npm dist-tag to publish with
37+
required: false
38+
type: string
39+
default: latest
40+
2941
jobs:
3042
publish:
3143
runs-on: ubuntu-latest
@@ -34,22 +46,38 @@ jobs:
3446
contents: write
3547
id-token: write
3648

49+
env:
50+
VERSIONING: ${{ github.event_name == 'workflow_dispatch' }}
51+
3752
steps:
3853
- name: Enforce mint-components prerelease-only policy
39-
if: inputs.package == 'mint-components' && inputs.increment-type != 'prerelease'
54+
if: github.event_name == 'workflow_dispatch' && inputs.package == 'mint-components' && inputs.increment-type != 'prerelease'
4055
run: |
4156
echo "::error::Stable releases of @saasquatch/mint-components must go through the 'Mint Components Release' workflow (Changesets). This workflow only supports 'prerelease' for mint-components."
4257
exit 1
4358
59+
- name: Validate package input
60+
env:
61+
PACKAGE: ${{ inputs.package }}
62+
run: |
63+
case "$PACKAGE" in
64+
express-boilerplate|integration-boilerplate-node|logger|mint-components|program-boilerplate|program-test-suite|publish-helper) ;;
65+
*) echo "::error::Invalid package '${PACKAGE}'."; exit 1 ;;
66+
esac
67+
4468
- name: Generate Variables
4569
id: vars
70+
env:
71+
PACKAGE: ${{ inputs.package }}
72+
NPM_TAG: ${{ inputs.npm-tag || (inputs.increment-type == 'prerelease' && 'next') || 'latest' }}
4673
run: |
47-
echo "dir=packages/${{ inputs.package }}" >> ${GITHUB_OUTPUT}
48-
if [ "${{ inputs.increment-type }}" = "prerelease" ]; then
49-
echo "npmtag=next" >> ${GITHUB_OUTPUT}
50-
else
51-
echo "npmtag=latest" >> ${GITHUB_OUTPUT}
74+
# Validate npm-tag is a safe dist-tag value (alphanumeric, hyphens, dots)
75+
if [[ ! "$NPM_TAG" =~ ^[a-zA-Z0-9._-]+$ ]]; then
76+
echo "::error::Invalid npm-tag '${NPM_TAG}'. Must be alphanumeric with hyphens/dots only."
77+
exit 1
5278
fi
79+
echo "dir=packages/${PACKAGE}" >> ${GITHUB_OUTPUT}
80+
echo "npmtag=${NPM_TAG}" >> ${GITHUB_OUTPUT}
5381
5482
- name: Checkout
5583
id: checkout
@@ -65,16 +93,18 @@ jobs:
6593
# we could add this to devDependencies but we'd have to do it for every package
6694
# so might as well just install it inside the action
6795
- name: Install semver
96+
if: env.VERSIONING == 'true'
6897
id: npm-i-semver
6998
run: |
70-
npm i --no-save semver
99+
npm i --no-save semver@7.8.0
71100
72101
- name: Install dependencies
73102
id: npm-ci
74103
working-directory: ${{ steps.vars.outputs.dir }}
75104
run: npm ci
76105

77106
- name: Compute new version
107+
if: env.VERSIONING == 'true'
78108
id: compute-version
79109
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # ed597 = v8
80110
with:
@@ -115,6 +145,7 @@ jobs:
115145
return newVersion;
116146
117147
- name: Extract package name
148+
if: env.VERSIONING == 'true'
118149
id: extract-package-name
119150
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # ed597 = v8
120151
with:
@@ -135,6 +166,7 @@ jobs:
135166
return packageName;
136167
137168
- name: Write new version to file
169+
if: env.VERSIONING == 'true'
138170
working-directory: ${{ steps.vars.outputs.dir }}
139171
run: |
140172
jq '.version = "${{ steps.compute-version.outputs.result }}"' package.json > package.json.tmp
@@ -151,6 +183,7 @@ jobs:
151183
run: npm publish --provenance --access public --tag ${{ steps.vars.outputs.npmtag }}
152184

153185
- name: Commit and push changes
186+
if: env.VERSIONING == 'true'
154187
id: commit-and-push
155188
uses: saasquatch/git-commit-action@v0.0.15
156189
env:

0 commit comments

Comments
 (0)