Skip to content

Commit 9af52b2

Browse files
Merge pull request AlmaLinux#271 from yuravk/feat-build-ami-add-images-testing
feat(CI: build-ami.yml): Add simple testing of built AMIs
2 parents e5f57af + b300525 commit 9af52b2

File tree

1 file changed

+91
-2
lines changed

1 file changed

+91
-2
lines changed

.github/workflows/build-ami.yml

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ on:
1515
- 9
1616
- 8
1717

18+
test_ami:
19+
description: "Test built AMI"
20+
required: true
21+
type: boolean
22+
default: true
23+
1824
notify_mattermost:
1925
description: "Send notification to Mattermost"
2026
required: true
@@ -23,12 +29,16 @@ on:
2329

2430
env:
2531
ALMALINUX_AWS_ACCOUNT_ID: 764336703387
32+
ALMALINUX_AWS_INFRA_ACCOUNT_ID: 383541928683
2633
PACKER_GITHUB_API_TOKEN: ${{ secrets.GIT_HUB_TOKEN }}
2734

2835
jobs:
29-
build-images:
30-
name: ${{ matrix.variant }} ${{ matrix.arch }} AMI
36+
build-ami:
37+
name: Build ${{ matrix.variant }} ${{ matrix.arch }} AMI
3138
runs-on: ubuntu-24.04
39+
outputs:
40+
ami_x86_64: ${{ steps.get-ami-id.outputs.ami_x86_64 }}
41+
ami_aarch64: ${{ steps.get-ami-id.outputs.ami_aarch64 }}
3242
strategy:
3343
fail-fast: false
3444
matrix:
@@ -105,6 +115,7 @@ jobs:
105115
path: ${{ matrix.variant }}_${{ matrix.arch }}_build.log
106116

107117
- name: Get AMI ID
118+
id: get-ami-id
108119
run: |
109120
AMI_ID=$(grep -E '${{ vars.AWS_REGION }}: ami-' ${{ matrix.variant }}_${{ matrix.arch }}_build.log | awk '{print $2}')
110121
if [[ "${AMI_ID}" == "" ]]; then
@@ -113,6 +124,7 @@ jobs:
113124
echo "[Debug] AMI ID found in the build log: '${AMI_ID}'"
114125
fi
115126
echo "AMI_ID=${AMI_ID}" >> $GITHUB_ENV
127+
echo "ami_${{ matrix.arch }}=${AMI_ID}" >> $GITHUB_OUTPUT
116128
117129
- name: Get AMI Name
118130
run: |
@@ -124,6 +136,10 @@ jobs:
124136
fi
125137
echo "AMI_NAME=${AMI_NAME}" >> $GITHUB_ENV
126138
139+
- name: Launch permission for the AMI to Infra account
140+
run: |
141+
aws ec2 modify-image-attribute --image-id ${{ env.AMI_ID }} --launch-permission "Add=[{UserId=${{ env.ALMALINUX_AWS_INFRA_ACCOUNT_ID }}}]"
142+
127143
- name: Print AMI summary
128144
uses: actions/github-script@v7
129145
with:
@@ -145,3 +161,76 @@ jobs:
145161
:almalinux: **${{ env.AMI_NAME }}** AWS AMI, built by the GitHub [Action](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
146162
147163
**AMI ID**: `${{ env.AMI_ID }}`
164+
165+
test-ami:
166+
name: Test ${{ inputs.version_major }} ${{ matrix.arch }} AMI
167+
if: inputs.test_ami
168+
needs: [build-ami]
169+
runs-on: ${{ format('runs-on={0}/family={1}/ami={2}/region={3}', github.run_id, contains(matrix.arch, 'aarch64') && 't4g.medium' || 't3.medium', contains(matrix.arch, 'aarch64') && needs.build-ami.outputs.ami_aarch64 || needs.build-ami.outputs.ami_x86_64, vars.AWS_REGION )}}
170+
strategy:
171+
fail-fast: false
172+
matrix:
173+
arch:
174+
- aarch64
175+
- x86_64
176+
177+
steps:
178+
- name: Configure AWS credentials
179+
uses: aws-actions/[email protected]
180+
with:
181+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
182+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
183+
aws-region: ${{ vars.AWS_REGION }}
184+
185+
- name: Install aws CLI
186+
run: |
187+
# Install aws CLI
188+
sudo dnf install -y -q unzip
189+
curl "https://awscli.amazonaws.com/awscli-exe-linux-$(uname -m).zip" -o "awscliv2.zip"
190+
unzip -qq awscliv2.zip
191+
sudo ./aws/install
192+
193+
- name: Get AMI ID
194+
run: |
195+
AMI_ID=${{ matrix.arch == 'x86_64' && needs.build-ami.outputs.ami_x86_64 || needs.build-ami.outputs.ami_aarch64 }}
196+
echo "AMI_ID=${AMI_ID}" >> $GITHUB_ENV
197+
198+
- name: Get AMI architecture
199+
run: |
200+
AMI_ARCH=$(aws ec2 describe-images --image-ids ${{ env.AMI_ID }} --query 'Images[0].Tags' | jq -r '.[] | select(.Key == "Architecture") | .Value')
201+
if [[ "${AMI_ARCH}" == "" || "${AMI_ARCH}" == "None" ]]; then
202+
exit 1
203+
else
204+
echo "[Debug] AMI Arch: '${AMI_ARCH}'"
205+
fi
206+
echo "AMI_ARCH=${AMI_ARCH}" >> $GITHUB_ENV
207+
208+
- name: Get AlmaLinux versions
209+
id: ami-version
210+
run: |
211+
AMI_VERSION=$(aws ec2 describe-images --image-ids ${{ env.AMI_ID }} --query 'Images[0].Tags' | jq -r '.[] | select(.Key == "Version") | .Value')
212+
if [[ "${AMI_VERSION}" == "" || "${AMI_VERSION}" == "None" ]]; then
213+
exit 1
214+
else
215+
echo "[Debug] AMI Version: '${AMI_VERSION}'"
216+
fi
217+
echo "AMI_VERSION=${AMI_VERSION}" >> $GITHUB_ENV
218+
echo "OS_VERSION=$(echo $AMI_VERSION | sed 's/\.[0-9]\{8\}.*$//g')" >> $GITHUB_ENV
219+
220+
- name: Test AMI
221+
run: |
222+
case "${{ env.OS_VERSION }}" in
223+
10)
224+
OS_RELEASE="AlmaLinux Kitten release ${{ env.OS_VERSION }}"
225+
;;
226+
*)
227+
OS_RELEASE="AlmaLinux release ${{ env.OS_VERSION }}"
228+
;;
229+
esac
230+
231+
echo "[Debug] AlmaLinux release:"
232+
grep "${OS_RELEASE}" /etc/almalinux-release || exit 1
233+
echo "[Debug] System architecture:"
234+
rpm -q --qf='%{ARCH}\n' $(rpm -qf /etc/almalinux-release) | grep '${{ env.AMI_ARCH }}' || exit 1
235+
echo "[Debug] Check for updates:"
236+
dnf check-update || exit 1

0 commit comments

Comments
 (0)