-
Notifications
You must be signed in to change notification settings - Fork 0
238 lines (212 loc) · 9.6 KB
/
publish_sample.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
name: Publish to Public Repo Release
#
# plan
# 0) read in current repo the release meta-info (versions.yml)
# 1) prepare in current repo the release content (package)
# 2) push to remote public web repo the release definition (version, description...)
# 3) wait for remote workflow end with success
# 4) create a new release draft of public web repo
# 5) upload and attach package to the public release
# 6) publish draft release
# 7) let report release links, and download links
on:
push:
tags:
- 'v*.*.*'
jobs:
release:
runs-on: ubuntu-latest
env:
REPO_WEB_URL: https://github.com/chickarmy/chickenbot-web
REPO_API_URL: https://api.github.com/repos/chickarmy/chickenbot-web
FILE_NAME: mySampleTestFile-1.2.3.exe
SKIP_PUSH_VERSION_WORKFLOW: true
SKIP_PUBLISH: true
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: 0) Read package.json version
id: package_version
run: |
# Extract the version from package.json and store it as an environment variable
version=$(jq -r '.version' package.json)
expectedVersion="v$version-test"
echo "Package version is $version"
if [[ "${{ github.ref_name }}" != "$expectedVersion" ]]; then
echo "Error: Tag ${{ github.ref_name }} does not match version ${expectedVersion}"
exit 1
fi
echo "VERSION=$version" >> $GITHUB_ENV
# https://github.com/mikefarah/yq?tab=readme-ov-file#github-action
- name: Install yq
uses: mikefarah/yq@master
- name: 0) Read version meta-info from versions.yml to env
id: yaml_to_env
run: |
# Extract data for version ${{ env.VERSION }} from versions.yml
version_data=$(yq '.versions[] | select(.version == "${{ env.VERSION }}")' versions.yml)
# Check if version_data is empty
if [ -z "$version_data" ]; then
echo "Error: No data found for version ${{ env.VERSION }} in versions.yml"
exit 1
fi
########
# Function to extract specific fields and format values
########
# Extract and
# yq -r '.description' : use yq to extract (-r) raw value (without quotes)
# sed ':a;N;$!ba;s/\n/\\n/g; s/\'/’/g' :
## :a;N;$!ba; : get all line in a buffer
## s/\n/\\n/g : substitute all carriage return by \n.
## s/\'/’/g : substitute single quote by typographic quote 0146.
extract_value() {
echo "$(echo "$version_data" | yq -r "$1" | sed ':a;N;$!ba;s/\n/\\n/g; s/'\''/’/g')"
}
# and store them in environment variables
echo "LABEL=$(extract_value '.label')" >> $GITHUB_ENV
echo "LABEL_FR=$(extract_value '.label_fr')" >> $GITHUB_ENV
echo "DESCRIPTION=$(extract_value '.description')" >> $GITHUB_ENV
echo "DESCRIPTION_FR=$(extract_value '.description_fr')" >> $GITHUB_ENV
- name: 1) Prepare release file (Simulation)
run: |
mkdir -p ./packages
head -c 82M </dev/urandom >./packages/$FILE_NAME
echo "Preparing release file ./packages/$FILE_NAME done :"
ls -lartkh ./packages/
- name: 2) push new release definition to another repo
if: ${{ env.SKIP_PUSH_VERSION_WORKFLOW != 'true' }}
run: |
# Disable -e temporarily to capture the curl exit status manually
set +e
response=$(curl -sf -X POST \
-H "Authorization: token ${{ secrets.PAT_TOKEN }}" \
-H "Accept: application/vnd.github.everest-preview+json" \
${{ env.REPO_API_URL }}/dispatches \
-d '{
"event_type": "push-new-version",
"client_payload": {
"version": "${{ env.VERSION }}",
"label": "${{ env.LABEL }}",
"label_fr": "${{ env.LABEL_FR }}",
"description": "${{ env.DESCRIPTION }}",
"description_fr": "${{ env.DESCRIPTION_FR }}",
"note": "${{ env.REPO_WEB_URL }}/releases/tag/${{ env.VERSION }}",
"download": "${{ env.REPO_WEB_URL }}/releases/download/${{ env.VERSION }}/package.readme.txt"
}
}')
# Capture the curl exit status
exit_status=$?
# Re-enable -e for the rest of the script
set -e
# Check for errors
if [ $exit_status -ne 0 ]; then
echo "Error: curl command failed with status $exit_status"
exit 1
fi
# Output the curl response
echo "$response"
- name: 3) Waiting for push-new-version workflow completion
if: ${{ env.SKIP_PUSH_VERSION_WORKFLOW != 'true' }}
id: wait_for_completion
run: |
# Wait and check the status of the triggered workflow
workflow="push-new-version"
token="${{ secrets.PAT_TOKEN }}"
run_id=""
# Poll for the workflow run ID of the triggered workflow
until [ -n "$run_id" ]; do
curl -s \
-H "Authorization: token $token" \
"${{ env.REPO_API_URL }}/actions/runs?event=repository_dispatch&status=in_progress" \
> curl_response
cat curl_response
run_id=$(jq -r ".workflow_runs[] | select(.display_title==\"push-new-version\") | .id" < curl_response)
[ -n "$run_id" ] || echo "Waiting for workflow run ID..."
[ -n "$run_id" ] || sleep 3
done
echo "ℹ️ Workflow ID: $run_id"
# Poll for the workflow status until it's completed
conclusion=""
until [ "$conclusion" == "success" ]; do
conclusion=$(curl -s \
-H "Authorization: token $token" \
"${{ env.REPO_API_URL }}/actions/runs/$run_id" \
| jq -r ".conclusion")
if [ "$conclusion" == "failure" ]; then
echo " 🔴 The workflow FAILED."
exit 1
elif [ "$conclusion" == "cancelled" ]; then
echo " 🚫 The workflow was CANCELLED."
exit 1
fi
echo "Waiting for the workflow to complete..."
sleep 10
done
echo " ✅ Workflow completed successfully."
- name: 4) Create Release Draft in Public Repo
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
run: |
RESPONSE_FILE="release_response.json"
# Create the release and save the response in a file
curl -f -s -X POST \
-H "Authorization: token ${{ secrets.PAT_TOKEN }}" \
-H "Content-Type: application/json" \
-d '{
"tag_name": "'${{ github.ref_name }}'",
"target_commitish": "main",
"name": "${{ env.LABEL}}",
"body": "${{ env.DESCRIPTION}}\n\n# ${{ env.LABEL_FR}}\n\n${{ env.DESCRIPTION_FR}}",
"draft": true,
"prerelease": false
}' \
${{ env.REPO_API_URL }}/releases > $RESPONSE_FILE
# Display the API response
echo "Release creation response:"
cat $RESPONSE_FILE
# Extract the release ID from the response file
RELEASE_ID=$(jq -r '.id' $RESPONSE_FILE)
RELEASE_UPLOAD_URL=$(jq -r '.upload_url' $RESPONSE_FILE)
if [ "$RELEASE_ID" == "null" ]; then
echo "Failed to create release: $(cat $RESPONSE_FILE)"
exit 1
fi
echo "Release created successfully! id: $RELEASE_ID - upload_url: $RELEASE_UPLOAD_URL"
echo "RELEASE_ID=$RELEASE_ID" >> $GITHUB_ENV
echo "RELEASE_UPLOAD_URL=$RELEASE_UPLOAD_URL" >> $GITHUB_ENV
# rely on @actions/github NodeJS library
## original action - WARN: archived repository
## uses: actions/upload-release-asset@v1
## https://github.com/actions/upload-release-asset/blob/main/src/upload-release-asset.js
- name: 5) Upload Release Asset
id: upload-release-asset
# more recent fork node16 @v1.1.1 => @main WARN about changes
uses: sekwah41/upload-release-assets@main
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
with:
upload_url: ${{ env.RELEASE_UPLOAD_URL }}
asset_path: ./packages/${{ env.FILE_NAME }}
asset_name: fake_app.data
asset_content_type: text/plain
- name: 6) Publish Draft Release
if: ${{ env.SKIP_PUBLISH != 'true' }}
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
run: |
# Convert the draft release to a published release
curl -f -s -X PATCH \
-H "Authorization: token ${{ secrets.PAT_TOKEN }}" \
-H "Content-Type: application/json" \
-d '{"draft": false}' \
${{ env.REPO_API_URL }}/releases/${{ env.RELEASE_ID }}
## WARN : links are ok only when release is published
- name: 7) Echo published release and download links
if: ${{ env.SKIP_PUBLISH != 'true' }}
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
run: |
RELEASE_URL="${{ env.REPO_WEB_URL }}/releases/tag/${{ github.ref_name }}"
DOWNLOAD_URL="${{ env.REPO_WEB_URL }}/releases/download/${{ github.ref_name }}/package.readme.txt"
echo "Release page: $RELEASE_URL"
echo "Direct download link: $DOWNLOAD_URL"