-
Notifications
You must be signed in to change notification settings - Fork 0
266 lines (241 loc) · 9.71 KB
/
release_and_publish.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
name: Release and Publish
on:
workflow_dispatch
env:
CARGO_TERM_COLOR: always
jobs:
extract-version:
name: Extract Version
runs-on: ubuntu-latest
outputs:
cargo_version: ${{ steps.extract.outputs.cargo_version }}
steps:
- uses: actions/checkout@v4
- name: Extract version from Cargo.toml
id: extract
run: |
version=$(grep '^version =' Cargo.toml | sed 's/version = "//' | sed 's/"//')
echo "Extracted version: $version"
if [ -z "$version" ]; then
echo "Error: Version is blank."
exit 1
fi
echo "### Release Build: v$version" >> $GITHUB_STEP_SUMMARY
echo "cargo_version=$version" >> $GITHUB_OUTPUT
update-package-json-version:
name: Update package.json version
uses: ./.github/workflows/update_package_json_version.yml
needs: extract-version
with:
version: ${{ needs.extract-version.outputs.cargo_version }}
secrets: inherit
token: ${{ secrets.GITHUB_TOKEN }}
build-matrix:
name: Define Build Matrix
runs-on: ubuntu-latest
needs: extract-version
outputs:
target-matrix: ${{ steps.set-matrix.outputs.target-matrix }}
steps:
- name: Set matrix
id: set-matrix
env:
version: ${{ needs.extract-version.outputs.cargo_version }}
run: |
echo $RELEASE_TOKEN
echo "### Checking for artifact existence" >> $GITHUB_STEP_SUMMARY
response=$(curl -s "https://api.github.com/repos/GovCraft/paseto_cli/actions/artifacts")
process_artifact() {
local artifact_name="$1"
local artifact_info
local workflow_run_id
local exists
artifact_info=$(echo "$response" | jq -r --arg name "$artifact_name" '
.artifacts // []
| map(select(.name == $name and (.expired == false or .expired == null) and (.expired_at == null or .expired_at == "")))
| sort_by(.created_at)
| reverse
| first // empty
')
if [ -n "$artifact_info" ] && [ "$artifact_info" != "null" ]; then
workflow_run_id=$(echo "$artifact_info" | jq -r '.workflow_run.id')
echo "Artifact $artifact_name already exists from workflow id: $workflow_run_id."
exists="true"
else
echo "Artifact $artifact_name does not exist or has expired so using workflow_run_id ${{ github.run_id }}" >> $GITHUB_STEP_SUMMARY
workflow_run_id="${{ github.run_id }}"
exists="false"
fi
var_name=$(echo "$artifact_name" | tr '-' '_' | tr '.' '_'| cut -c1-13)
echo "Setting $var_name for $workflow_run_id"
eval "${var_name}_workflow_run_id=$workflow_run_id"
eval "${var_name}_exists=$exists"
}
win=$(echo "paseto_cli-win32-x64-v${{env.version}}")
linux=$(echo "paseto_cli-linux-x64-v${{env.version}}")
mac=$(echo "paseto_cli-darwin-v${{env.version}}")
for artifact_name in $win $linux $mac; do
process_artifact "$artifact_name"
done
echo "Results:"
for artifact_name in $win $linux $mac; do
var_name=$(echo "$artifact_name" | tr '-' '_' | tr '.' '_'| cut -c1-13)
eval "echo ::info::\"$artifact_name - Exists: \${${var_name}_exists}, Workflow Run ID: \${${var_name}_workflow_run_id}\""
eval "echo \"$artifact_name - Exists: \${${var_name}_exists}, Workflow Run ID: \${${var_name}_workflow_run_id}\"" >> $GITHUB_STEP_SUMMARY
done
var_win_name=$(echo "$win" | tr '-' '_' | tr '.' '_'| cut -c1-13)
var_linux_name=$(echo "$linux" | tr '-' '_' | tr '.' '_'| cut -c1-13)
var_mac_name=$(echo "$mac" | tr '-' '_' | tr '.' '_'| cut -c1-13)
echo $var_win_name
echo $var_linux_name
echo $var_mac_name
eval "var_win_exists=\${${var_win_name}_exists}"
eval "var_linux_exists=\${${var_linux_name}_exists}"
eval "var_mac_exists=\${${var_mac_name}_exists}"
matrix=$(jq -c -n \
--arg linux_exists ${var_linux_exists} \
--arg darwin_exists ${var_mac_exists} \
--arg win_exists ${var_win_exists} \
--arg win_artifact_name ${win}.zip \
--arg linux_artifact_name ${linux}.tar.gz \
--arg mac_artifact_name ${mac}.tar.gz \
'{
"include": [
{"os": "ubuntu-latest", "artifact_name": $linux_artifact_name, "exists": $linux_exists, "target": "x86_64-unknown-linux-gnu"},
{"os": "macos-latest", "artifact_name": $mac_artifact_name, "exists": $darwin_exists, "target": "x86_64-apple-darwin"},
{"os": "windows-latest", "artifact_name": $win_artifact_name, "exists": $win_exists, "target": "x86_64-pc-windows-msvc"}
]
}')
echo "target-matrix=$matrix" >> $GITHUB_OUTPUT
shell: bash
build-jobs:
needs: build-matrix
strategy:
matrix: ${{ fromJson(needs.build-matrix.outputs.target-matrix) }}
runs-on: ${{ matrix.os }}
env:
filename: ${{ matrix.artifact_name }}
steps:
- name: Checkout code
if: matrix.exists == 'false'
uses: actions/checkout@v4
- name: Install Rust
if: matrix.exists == 'false'
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.target }}
override: true
- name: Build
if: matrix.exists == 'false'
uses: actions-rs/cargo@v1
with:
command: build
args: --release --target ${{ matrix.target }}
- name: Package Binary
if: matrix.exists == 'false'
run: |
if [[ "${{ matrix.os }}" == "ubuntu-latest" ]]; then
cd target/x86_64-unknown-linux-gnu/release
tar -czf paseto_cli-linux-x64.tar.gz paseto_cli
mv paseto_cli-linux-x64.tar.gz ../../../${{ env.filename }}
elif [[ "${{ matrix.os }}" == "macos-latest" ]]; then
cd target/x86_64-apple-darwin/release
tar -czf paseto_cli-darwin.tar.gz paseto_cli
mv paseto_cli-darwin.tar.gz ../../../${{ env.filename }}
elif [[ "${{ matrix.os }}" == "windows-latest" ]]; then
cd target/x86_64-pc-windows-msvc/release
7z a -r paseto_cli-win32-x64.zip paseto_cli.exe
powershell -command "Move-Item -Path paseto_cli-win32-x64.zip -Destination ../../../${{ env.filename }}"
fi
echo "### Artifact created: ${{ env.filename }}" >> $GITHUB_STEP_SUMMARY
shell: bash
- name: Upload Binary
if: matrix.exists == 'false'
uses: actions/upload-artifact@v4
with:
name: ${{ env.filename }}
path: ${{ env.filename }}
compression-level: 9
create-release:
needs: [ build-jobs, extract-version, build-matrix ]
runs-on: ubuntu-latest
steps:
- name: Download Artifacts
uses: actions/download-artifact@v4
with:
run-id: ${{ needs.build.outputs.workflow_run_id }}
path: .
- name: Create Release and Upload Release Assets
uses: softprops/action-gh-release@v2
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}
with:
tag_name: v${{ needs.extract-version.outputs.cargo_version }}
name: Release ${{ needs.extract-version.outputs.cargo_version }}
draft: false
prerelease: false
token: ${{ secrets.RELEASE_TOKEN }}
files: |
*.tar.gz
*.zip
publish-npm:
needs: [ create-release, extract-version, build-jobs ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
registry-url: 'https://registry.npmjs.org'
- name: Using Workflow Run ID
run: |
echo "Downloading artifacts from workflow id: ${{ needs.build-jobs.outputs.workflow_run_id }}"
- name: Download Artifacts
uses: actions/download-artifact@v4
with:
run-id: ${{ needs.build-jobs.outputs.workflow_run_id }}
path: .
- name: Install dependencies
env:
BINARY_DISTRIBUTION_VERSION: ${{ needs.extract-version.outputs.cargo_version }}
CI: true
run: |
npm install
npm ci
- name: Publish Linux x64 Binary
run: |
mkdir -p paseto_cli-linux-x64/bin
tar -xzf paseto_cli-linux-x64.tar.gz -C paseto_cli-linux-x64/bin
cd paseto_cli-linux-x64
npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Publish macOS x64 Binary
run: |
ls -la
mkdir -p paseto_cli-darwin-x64/bin
tar -xzf paseto_cli-darwin-x64.tar.gz -C paseto_cli-darwin-x64/bin
cd paseto_cli-darwin-x64
npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Publish macOS arm64 Binary
run: |
ls -la
mkdir -p paseto_cli-darwin-arm64/bin
tar -xzf paseto_cli-darwin-arm64.tar.gz -C paseto_cli-darwin-arm64/bin
cd paseto_cli-darwin-arm64
npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Publish Windows x64 Binary
run: |
dir
mkdir -p paseto_cli-windows-x64/bin
unzip paseto_cli-win32-x64.zip -d paseto_cli-windows-x64/bin
cd paseto_cli-windows-x64
npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}