Repository navigation
138 lines (123 loc) · 4.18 KB
/
Copy pathpublish.yml
File metadata and controls
138 lines (123 loc) · 4.18 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
name: Publish to crates.io and npm
# Runs whenever a GitHub Release is published, publishing the CLI to both
# crates.io (the `commitor-cli` crate) and npm (the `commitor-cli` wrapper).
# Can also be triggered manually to (re)publish a specific tag.
on:
release:
types: [published]
workflow_dispatch:
inputs:
tag:
description: "Tag to publish (e.g. v0.3.3). Leave empty for release events."
required: false
permissions:
contents: read
jobs:
cargo:
name: Publish to crates.io
runs-on: ubuntu-latest
steps:
- name: Resolve tag
id: tag
run: |
if [ -n "${{ github.event.inputs.tag }}" ]; then
echo "value=${{ github.event.inputs.tag }}" >> "$GITHUB_OUTPUT"
else
echo "value=${GITHUB_REF_NAME}" >> "$GITHUB_OUTPUT"
fi
- uses: actions/checkout@v4
with:
ref: ${{ steps.tag.outputs.value }}
- uses: dtolnay/rust-toolchain@stable
- name: Verify crate version matches tag
run: |
tag="${TAG#v}"
pkg=$(cargo pkgid -p commitor-cli | sed -E 's/.*[@#]//')
if [ "$tag" != "$pkg" ]; then
echo "Version mismatch: tag $tag vs crate $pkg" >&2
exit 1
fi
env:
TAG: ${{ steps.tag.outputs.value }}
- name: Skip if version already on crates.io
id: crates_io
env:
TAG: ${{ steps.tag.outputs.value }}
run: |
tag="${TAG#v}"
status=$(curl -s -o /dev/null -w "%{http_code}" \
-H "User-Agent: commitor-cli-publish" \
"https://crates.io/api/v1/crates/commitor-cli/$tag")
echo "status=$status" >> "$GITHUB_OUTPUT"
- name: Publish commitor-cli
if: steps.crates_io.outputs.status != '200'
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: |
set +e
out=$(cargo publish -p commitor-cli 2>&1)
rc=$?
echo "$out"
# Tolerate a version that slipped through (e.g. a prior partial
# run already published it) so the workflow doesn't fail on a
# re-run of the same release.
if echo "$out" | grep -qi "already uploaded"; then
echo "Version already published — treating as success."
exit 0
fi
exit $rc
npm:
name: Publish to npm
runs-on: ubuntu-latest
steps:
- name: Resolve tag
id: tag
run: |
if [ -n "${{ github.event.inputs.tag }}" ]; then
echo "value=${{ github.event.inputs.tag }}" >> "$GITHUB_OUTPUT"
else
echo "value=${GITHUB_REF_NAME}" >> "$GITHUB_OUTPUT"
fi
- uses: actions/checkout@v4
with:
ref: ${{ steps.tag.outputs.value }}
- uses: actions/setup-node@v4
with:
node-version: "20"
- name: Verify npm version matches tag
run: |
tag="${TAG#v}"
pkg=$(node -p "require('./npm/package.json').version")
if [ "$tag" != "$pkg" ]; then
echo "Version mismatch: tag $tag vs package $pkg" >&2
exit 1
fi
env:
TAG: ${{ steps.tag.outputs.value }}
- name: Skip if version already on npm
id: npm_check
env:
TAG: ${{ steps.tag.outputs.value }}
run: |
tag="${TAG#v}"
if npm view "commitor-cli@$tag" version >/dev/null 2>&1; then
echo "published=true" >> "$GITHUB_OUTPUT"
else
echo "published=false" >> "$GITHUB_OUTPUT"
fi
- name: Configure npm auth
if: steps.npm_check.outputs.published != 'true'
run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > npm/.npmrc
- name: Publish commitor-cli
if: steps.npm_check.outputs.published != 'true'
run: |
set +e
out=$(npm publish --access public 2>&1)
rc=$?
echo "$out"
if echo "$out" | grep -qi "previously published\|cannot publish over"; then
echo "Version already published — treating as success."
exit 0
fi
exit $rc
working-directory: npm