Skip to content

Commit e6fb999

Browse files
committed
ci: publish packages with npm trusted publishing
1 parent 826379d commit e6fb999

1 file changed

Lines changed: 55 additions & 11 deletions

File tree

.github/workflows/publish.yml

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,37 @@ jobs:
5151
http_status_code=$(curl -LI $GET_API_URL -o /dev/null -w '%{http_code}\n' -s \
5252
-H "Authorization: token ${GITHUB_TOKEN}")
5353
if [ "$http_status_code" -ne "404" ] ; then
54-
echo "::set-output name=exists_tag::true"
54+
echo "exists_tag=true" >> "$GITHUB_OUTPUT"
5555
else
56-
echo "::set-output name=exists_tag::false"
56+
echo "exists_tag=false" >> "$GITHUB_OUTPUT"
5757
fi
5858
env:
5959
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
60+
- name: Package Check
61+
id: package_check
62+
run: |
63+
set -eu
64+
./node_modules/.bin/lerna list --toposort --json --loglevel silent > /tmp/publish-packages.json
65+
node -e '
66+
const fs = require("fs");
67+
const path = require("path");
68+
const packages = JSON.parse(fs.readFileSync("/tmp/publish-packages.json", "utf8"));
69+
for (const pkg of packages) {
70+
const manifest = require(path.resolve(pkg.location, "package.json"));
71+
if (manifest.private) continue;
72+
console.log(`${pkg.location}\t${manifest.name}\t${manifest.version}`);
73+
}
74+
' > /tmp/publish-list.tsv
75+
has_unpublished_packages=false
76+
while IFS="$(printf '\t')" read -r location name version; do
77+
if npm view "${name}@${version}" version >/dev/null 2>&1; then
78+
echo "Found ${name}@${version}"
79+
else
80+
echo "Need publish ${name}@${version}"
81+
has_unpublished_packages=true
82+
fi
83+
done < /tmp/publish-list.tsv
84+
echo "has_unpublished_packages=${has_unpublished_packages}" >> "$GITHUB_OUTPUT"
6085
- name: Create Git Tag
6186
if: steps.tag_check.outputs.exists_tag == 'false'
6287
uses: pkgdeps/git-tag-action@v2
@@ -67,38 +92,57 @@ jobs:
6792
git_commit_sha: ${{ github.sha }}
6893
git_tag_prefix: "v"
6994
- name: Setup npm trusted publishing
70-
if: steps.tag_check.outputs.exists_tag == 'false'
95+
if: steps.package_check.outputs.has_unpublished_packages == 'true'
7196
uses: actions/setup-node@v4
7297
with:
7398
node-version: 24
7499
registry-url: https://registry.npmjs.org
75100
- name: Install npm with OIDC support
76-
if: steps.tag_check.outputs.exists_tag == 'false'
101+
if: steps.package_check.outputs.has_unpublished_packages == 'true'
77102
run: |
78103
npm install -g npm@^11.5.1
79104
npm --version
80105
- name: Publish
81106
uses: nick-fields/retry@v2
82-
if: steps.tag_check.outputs.exists_tag == 'false'
107+
if: steps.package_check.outputs.has_unpublished_packages == 'true'
83108
with:
84109
timeout_minutes: 10 # 设置超时时间,单位为分钟
85110
max_attempts: 3 # 设置最大重试次数
86111
command: |
87-
yarn lerna publish from-package --no-verify-access --yes
112+
set -eu
113+
./node_modules/.bin/lerna list --toposort --json --loglevel silent > /tmp/publish-packages.json
114+
node -e '
115+
const fs = require("fs");
116+
const path = require("path");
117+
const packages = JSON.parse(fs.readFileSync("/tmp/publish-packages.json", "utf8"));
118+
for (const pkg of packages) {
119+
const manifest = require(path.resolve(pkg.location, "package.json"));
120+
if (manifest.private) continue;
121+
console.log(`${pkg.location}\t${manifest.name}\t${manifest.version}`);
122+
}
123+
' > /tmp/publish-list.tsv
124+
while IFS="$(printf '\t')" read -r location name version; do
125+
if npm view "${name}@${version}" version >/dev/null 2>&1; then
126+
echo "Skip ${name}@${version}: already published"
127+
else
128+
echo "Publish ${name}@${version}"
129+
npm publish "${location}" --access public
130+
fi
131+
done < /tmp/publish-list.tsv
88132
- name: Get Output
89-
if: steps.tag_check.outputs.exists_tag == 'false'
133+
if: steps.package_check.outputs.has_unpublished_packages == 'true'
90134
uses: dawidd6/action-download-artifact@v6
91135
with:
92136
name: publish-output-v${{ env.CURRENT_VERSION }}
93137
github_token: ${{ secrets.GITHUB_TOKEN }}
94138
workflow: create-publish-pr.yml
95139
workflow_conclusion: success
96140
- name: Read the output
97-
if: steps.tag_check.outputs.exists_tag == 'false'
141+
if: steps.package_check.outputs.has_unpublished_packages == 'true'
98142
run: |
99143
echo "SLACK_USER=$(cat output.txt)" >> $GITHUB_ENV
100144
- name: Send to Slack
101-
if: steps.tag_check.outputs.exists_tag == 'false'
145+
if: steps.package_check.outputs.has_unpublished_packages == 'true'
102146
id: slack
103147
uses: slackapi/slack-github-action@v1.24.0
104148
with:
@@ -112,7 +156,7 @@ jobs:
112156
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
113157
- name: Build Changelog
114158
id: github_release
115-
if: steps.tag_check.outputs.exists_tag == 'false'
159+
if: steps.package_check.outputs.has_unpublished_packages == 'true'
116160
uses: mikepenz/release-changelog-builder-action@v3
117161
with:
118162
configurationJson: |
@@ -140,7 +184,7 @@ jobs:
140184
env:
141185
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
142186
- name: Create Release
143-
if: steps.tag_check.outputs.exists_tag == 'false'
187+
if: steps.package_check.outputs.has_unpublished_packages == 'true'
144188
uses: softprops/action-gh-release@v1
145189
with:
146190
body: ${{steps.github_release.outputs.changelog}}

0 commit comments

Comments
 (0)