Skip to content

Commit 333b7ab

Browse files
Merge pull request AlmaLinux#270 from yuravk/refactor-aws-copy-ami-workflow-to-merge-md-csv
refactor(CI: copy-ami.yml): Upload both x86_64 and arm64 AMIs and generate MD and CSV data for the Wiki
2 parents 9af52b2 + 270d015 commit 333b7ab

File tree

1 file changed

+204
-32
lines changed

1 file changed

+204
-32
lines changed

.github/workflows/copy-ami.yml

Lines changed: 204 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ on:
44
workflow_dispatch:
55
inputs:
66

7-
ami_id:
8-
description: "AMI ID to copy and make public"
7+
x86_64_ami_id:
8+
description: "AMI ID (x86_64)"
9+
required: true
10+
default: ''
11+
12+
aarch64_ami_id:
13+
description: "AMI ID (aarch64)"
914
required: true
1015
default: ''
1116

@@ -15,16 +20,35 @@ on:
1520
type: boolean
1621
default: true
1722

23+
draft:
24+
description: "Pull Request to Wiki as Draft"
25+
required: true
26+
type: boolean
27+
default: true
28+
1829
notify_mattermost:
1930
description: "Send notification to Mattermost"
2031
required: true
2132
type: boolean
2233
default: true
34+
env:
35+
# AlmaLinux Wiki repository almalinux/wiki
36+
wiki_repo: almalinux/wiki
37+
38+
permissions:
39+
id-token: write
40+
contents: read
2341

2442
jobs:
25-
build-images:
26-
name: "Copy ${{ inputs.ami_id }} AMI to all available AWS regions and make public"
43+
copy-ami:
44+
name: "Copy ${{ matrix.ami_id }} AMI to all available AWS regions and make public"
2745
runs-on: ubuntu-24.04
46+
strategy:
47+
fail-fast: false
48+
matrix:
49+
ami_id: ${{ fromJSON(format('["{0}", "{1}"]', inputs.x86_64_ami_id, inputs.aarch64_ami_id)) }}
50+
exclude:
51+
- ami_id: ''
2852

2953
steps:
3054
- uses: actions/checkout@v4
@@ -38,44 +62,43 @@ jobs:
3862

3963
- name: Get AMI Name
4064
run: |
41-
AMI_NAME=$(aws ec2 describe-images --filters "Name=image-id,Values=${{ inputs.ami_id }}" --query 'Images[0].Name' --output text)
65+
AMI_NAME=$(aws ec2 describe-images --filters "Name=image-id,Values=${{ matrix.ami_id }}" --query 'Images[0].Name' --output text)
4266
if [[ "${AMI_NAME}" == "" || "${AMI_NAME}" == "None" ]]; then
4367
exit 1
4468
else
4569
echo "[Debug] AMI Name: '${AMI_NAME}'"
4670
fi
4771
echo "AMI_NAME=${AMI_NAME}" >> $GITHUB_ENV
4872
49-
- name: Print AMI summary
50-
uses: actions/github-script@v7
51-
with:
52-
result-encoding: string
53-
script: |
54-
core.summary
55-
.addHeading('${{ env.AMI_NAME }}', '4')
56-
.addHeading('AMI ID: ${{ inputs.ami_id }}', '5')
57-
.addHeading('Public: ${{ inputs.make_public && '✅' || '❌'}}', '5')
58-
.write()
59-
6073
- name: Copy an AMI to all available AWS regions and make it public
6174
if: inputs.make_public
6275
run: |
6376
# Lowercase AMI Name, replace spaces and dots with underscores
6477
wiki_data_file="${{ env.AMI_NAME }}"
65-
wiki_data_file="${wiki_data_file,,}"
6678
wiki_data_file="${wiki_data_file// /_}"
6779
wiki_data_file="${wiki_data_file//./_}"
68-
echo "wiki_data_file=${wiki_data_file}" >> $GITHUB_ENV
80+
echo "wiki_data_file=${wiki_data_file,,}" >> $GITHUB_ENV
6981
7082
pip3 install markdown_table
7183
7284
tools/aws_ami_mirror.py \
7385
--csv-output ${{ github.workspace }}/${wiki_data_file}.csv \
7486
--md-output ${{ github.workspace }}/${wiki_data_file}.md \
75-
--ami ${{ inputs.ami_id }}
87+
--ami ${{ matrix.ami_id }}
88+
89+
- name: Print AMI summary
90+
uses: actions/github-script@v7
91+
with:
92+
result-encoding: string
93+
script: |
94+
core.summary
95+
.addHeading('${{ env.AMI_NAME }}', '4')
96+
.addHeading('AMI ID: ${{ matrix.ami_id }}', '5')
97+
.addHeading('Copied and public: ${{ inputs.make_public && '✅' || '❌'}}', '5')
98+
.write()
7699
77-
- uses: actions/upload-artifact@v4
78-
name: Store Wiki's CSV and Markdown data as artifact
100+
- name: Store Wiki's CSV and Markdown data as artifact
101+
uses: actions/upload-artifact@v4
79102
if: inputs.make_public
80103
with:
81104
compression-level: 6
@@ -84,16 +107,165 @@ jobs:
84107
almalinux*.md
85108
name: ${{ env.wiki_data_file }}_wiki_data
86109

87-
- name: Send notification to Mattermost
88-
uses: mattermost/action-mattermost-notify@master
89-
if: inputs.notify_mattermost
90-
with:
91-
MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_WEBHOOK_URL }}
92-
MATTERMOST_CHANNEL: ${{ vars.MATTERMOST_CHANNEL }}
93-
MATTERMOST_USERNAME: ${{ github.triggering_actor }}
94-
TEXT: |
95-
**${{ env.AMI_NAME }}** AWS AMI, generated by the GitHub [Action](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
110+
prepare-data-for-wiki:
111+
name: Prepare MD and CSV data for Wiki
112+
runs-on: ubuntu-24.04
113+
needs: [copy-ami]
114+
if: inputs.make_public
115+
116+
steps:
117+
- name: Checkout ${{ env.wiki_repo }}, branch 'master'
118+
uses: actions/checkout@v4
119+
with:
120+
repository: ${{ env.wiki_repo }}
121+
path: wiki
122+
token: ${{ secrets.GIT_HUB_TOKEN }}
123+
124+
- name: Download Wiki's CSV and Markdown data artifacts
125+
uses: actions/download-artifact@v4
126+
127+
- name: Merge MD and CSV data
128+
run: |
129+
# Install prettier for Markdown formatting
130+
sudo apt install npm
131+
132+
# Artifacts are downloaded into *x86_64_wiki_data/ and *aarch64_wiki_data/ directories
133+
# They are like:
134+
# almalinux_os_kitten_10_20250904_0_x86_64_wiki_data/almalinux_os_kitten_10_20250904_0_x86_64_wiki_data.md
135+
# almalinux_os_10_0_20250904_aarch64_wiki_data/almalinux_os_10_0_20250904_aarch64_wiki_data.csv
136+
137+
# From x86_64 artifact extract string like:
138+
# almalinux_os_kitten_10_20250904_0_wiki_data
139+
# almalinux_os_kitten_10_0_20250904_wiki_data
140+
RESULT=$( find . -name \*x86_64_wiki_data -printf '%P\n' | sed 's/x86_64_//g' )
141+
[[ -z "$RESULT" ]] && { echo "[Error] Could not find *x86_64_wiki_data directory"; exit 1; }
142+
143+
# Extract full release, release major version, and date stamp
144+
# Remove the prefix "almalinux_os_" and suffix "_wiki_data"
145+
middle="${RESULT#almalinux_os_}"
146+
middle="${middle%_wiki_data}"
147+
# Find the date stamp (8 digits followed by optional _number)
148+
if [[ $middle =~ ([0-9]{8}(_[0-9]+)?)$ ]]; then
149+
datestamp="${BASH_REMATCH[1]}"
150+
# Remove the datestamp and trailing underscore to get full release
151+
release_full="${middle%_${datestamp}}"
152+
153+
# Prepare full release and release major version
154+
if [[ $release_full == "kitten"* ]]; then
155+
release=$release_full
156+
release_full="${release_full//_/ }"
157+
release_full="${release_full^}"
158+
else
159+
release=${release_full%_*}
160+
release_full="${release_full//_/.}"
161+
fi
162+
163+
datestamp="${datestamp//_/.}"
164+
else
165+
echo "[Error] Could not parse: $input"
166+
exit 1
167+
fi
168+
169+
# Locate source .md and .csv files
170+
file_x86_64_md=$( ls -1 *x86_64_wiki_data/*x86_64.md )
171+
file_aarch64_md=$( ls -1 *aarch64_wiki_data/*aarch64.md )
172+
file_x86_64_csv=$( ls -1 *x86_64_wiki_data/*x86_64.csv )
173+
file_aarch64_csv=$( ls -1 *aarch64_wiki_data/*aarch64.csv )
174+
175+
# Set target .md document path and name
176+
md_documents_path="docs/cloud"
177+
[[ $release == *"kitten"* ]] && md_documents_path="docs/development"
178+
md_document="${md_documents_path}/AWS_AMIS_${release}.md"
179+
180+
# Set target .csv document path and name
181+
csv_documents_path="docs/.vuepress/public/ci-data"
182+
csv_document="${csv_documents_path}/aws_amis_${release}.csv"
183+
184+
# Print header
185+
head -n 2 "$file_x86_64_md" > wiki/${md_document}
186+
# Merge .md files: sort primarily by Region (3rd column)
187+
# and secondarily by Arch (5th column) reverse
188+
(tail -n +3 "$file_x86_64_md"; tail -n +3 "$file_aarch64_md") | sort -t'|' -k3,3 -k5,5r >> wiki/${md_document}
189+
# Make the resulted MD file pretty
190+
npx prettier --parser markdown --write wiki/${md_document}
191+
192+
# Merge .csv files: sort by Region (3rd column) and Version (5th column, reverse)
193+
(cat "$file_x86_64_csv"; cat "$file_aarch64_csv") | sort -t',' -k3,3 -k5,5r > wiki/${csv_document}
194+
195+
echo "RESULT=${RESULT}" >> $GITHUB_ENV
196+
echo "RELEASE_STR=AlmaLinux OS ${release_full} ${datestamp}" >> $GITHUB_ENV
197+
echo "RESULT_MD=wiki/${md_document}" >> $GITHUB_ENV
198+
echo "RESULT_CSV=wiki/${csv_document}" >> $GITHUB_ENV
199+
200+
- name: Store merged CSV and Markdown data as artifact
201+
uses: actions/upload-artifact@v4
202+
with:
203+
compression-level: 6
204+
include-hidden-files: true
205+
path: |
206+
${{ env.RESULT_MD }}
207+
${{ env.RESULT_CSV }}
208+
name: ${{ env.RESULT }}
209+
210+
- name: Commit and push MD and CSV to ${{ env.wiki_repo }}, branch '${{ env.RESULT }}'
211+
uses: EndBug/add-and-commit@v9
212+
with:
213+
cwd: wiki
214+
new_branch: ${{ env.RESULT }}
215+
default_author: user_info
216+
message: "Update ${{ env.RELEASE_STR }} AWS AMI IDs"
217+
push: true
218+
219+
- name: Create Pull Request for ${{ env.wiki_repo }}, branch 'master'
220+
run: |
221+
# create pull request with 'gh pr create'
222+
gh_opts=''
223+
[ "${{ inputs.draft }}" = "true" ] && gh_opts='--draft'
224+
title="Update ${{ env.RELEASE_STR }} AWS AMI IDs"
225+
body="This is an auto-generated PR. Any concern or issues, please contact Cloud SIG."
226+
227+
cd wiki
228+
gh auth login --with-token < <(echo ${{ secrets.GIT_HUB_TOKEN }})
229+
230+
gh pr create \
231+
--title "${title}" \
232+
--body "${body}" \
233+
--repo ${{ env.wiki_repo }} \
234+
--base master \
235+
${gh_opts} \
236+
|& tee gh_pr_create.log
237+
238+
exit_code=${PIPESTATUS[0]}
239+
if [[ "${exit_code}" != "0" ]]; then
240+
exit 1
241+
fi
242+
243+
echo "WIKI_PR_URL=$( grep '${{ env.wiki_repo }}/pull/' gh_pr_create.log )" >> $GITHUB_ENV
244+
245+
- name: Print PR summary
246+
uses: actions/github-script@v7
247+
if: ${{ env.WIKI_PR_URL != '' }}
248+
with:
249+
result-encoding: string
250+
script: |
251+
core.summary
252+
.addHeading('${{ env.AMI_NAME }}', '4')
253+
.addLink('${{ env.WIKI_PR_URL }}', '${{ env.WIKI_PR_URL }}')
254+
.addHeading('The PR is draft: ${{ inputs.draft && '✅' || '❌'}}', '5')
255+
.write()
256+
257+
- name: Send notification to Mattermost
258+
uses: mattermost/action-mattermost-notify@master
259+
if: inputs.notify_mattermost
260+
with:
261+
MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_WEBHOOK_URL }}
262+
MATTERMOST_CHANNEL: ${{ vars.MATTERMOST_CHANNEL }}
263+
MATTERMOST_USERNAME: ${{ github.triggering_actor }}
264+
TEXT: |
265+
:almalinux: **${{ env.RELEASE_STR }}** AWS AMIs copied over regions and made public, by the GitHub [Action](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
96266
97-
:almalinux: **AMI ID**: `${{ inputs.ami_id }}`
267+
- **AMI ID (x86_64)**: `${{ inputs.x86_64_ami_id }}`
268+
- **AMI ID (aarch64)**: `${{ inputs.aarch64_ami_id }}`
98269
99-
Public: ${{ inputs.make_public && '✅' || '❌'}}
270+
Copied and public: ${{ inputs.make_public && '✅' || '❌'}}
271+
${{ env.WIKI_PR_URL != '' && format('Wiki PR: [{0}]({0})', env.WIKI_PR_URL) || '' }}

0 commit comments

Comments
 (0)