-
Notifications
You must be signed in to change notification settings - Fork 61
236 lines (204 loc) · 8.5 KB
/
Copy pathbuild-ami.yml
File metadata and controls
236 lines (204 loc) · 8.5 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
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
name: AWS AMI Build
on:
workflow_dispatch:
inputs:
version_major:
description: 'AlmaLinux major version'
required: true
default: '10'
type: choice
options:
- kitten_10
- 10
- 9
- 8
test_ami:
description: "Test built AMI"
required: true
type: boolean
default: true
notify_mattermost:
description: "Send notification to Mattermost"
required: true
type: boolean
default: true
env:
ALMALINUX_AWS_ACCOUNT_ID: 764336703387
ALMALINUX_AWS_INFRA_ACCOUNT_ID: 383541928683
PACKER_GITHUB_API_TOKEN: ${{ secrets.GIT_HUB_TOKEN }}
jobs:
build-ami:
name: Build ${{ matrix.variant }} ${{ matrix.arch }} AMI
runs-on: ubuntu-24.04
outputs:
ami_x86_64: ${{ steps.get-ami-id.outputs.ami_x86_64 }}
ami_aarch64: ${{ steps.get-ami-id.outputs.ami_aarch64 }}
strategy:
fail-fast: false
matrix:
variant: ${{ fromJSON(format('["{0}"]', inputs.version_major )) }}
arch:
- x86_64
- aarch64
steps:
- uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4.0.2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ vars.AWS_REGION }}
- name: Prepare staff
run: |
packer_template=amazon-ebssurrogate.almalinux_${{ matrix.variant }}_ami_${{ matrix.arch }}
echo "PACKER_TEMPLATE=${packer_template}" >> $GITHUB_ENV
variant="${{ matrix.variant }}"
variant="${variant^}"
variant="${variant//_/ }"
SOURCE_AMI_ID=$(aws ec2 describe-images --owners ${{ env.ALMALINUX_AWS_ACCOUNT_ID }} --query "Images | max_by(@, &CreationDate) | ImageId" --filters "Name=name,Values=AlmaLinux OS ${variant}*${{ matrix.arch }}" --region ${{ vars.AWS_REGION }} --output text)
if [[ "${SOURCE_AMI_ID}" == "" || "${SOURCE_AMI_ID}" == "None" ]]; then
echo "[Error] Failed to get AMI ID for AlmaLinux ${variant} ${{ matrix.arch }}."
exit 1
else
echo "[Debug] Source AMI ID: '${SOURCE_AMI_ID}'"
fi
echo "SOURCE_AMI_ID=${SOURCE_AMI_ID}" >> $GITHUB_ENV
- name: Update system
run: |
sudo apt-get -y update
- name: Add Hashicorp repository
run: |
wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt-get -y update
- name: Install packer
run: |
sudo apt-get -y install packer
- name: Install ansible
run: sudo apt-get -y install ansible
- name: Initialize packer
run: packer init -upgrade .
- name: Build ${{ matrix.variant }} ${{ matrix.arch }} AMI
run: |
packer build \
-var='aws_source_ami_${{ matrix.variant }}_${{ matrix.arch }}=${{ env.SOURCE_AMI_ID }}' \
-var='aws_ami_region=${{ vars.AWS_REGION }}' \
-only=${{ env.PACKER_TEMPLATE }} . |& tee ${{ matrix.variant }}_${{ matrix.arch }}_build.log
exit_code=${PIPESTATUS[0]}
if [[ "${exit_code}" != "0" ]]; then
exit 1
fi
- uses: actions/upload-artifact@v4
name: Store build log as artifact
if: always()
with:
compression-level: 6
name: ${{ matrix.variant }}_${{ matrix.arch }}_build.log
path: ${{ matrix.variant }}_${{ matrix.arch }}_build.log
- name: Get AMI ID
id: get-ami-id
run: |
AMI_ID=$(grep -E '${{ vars.AWS_REGION }}: ami-' ${{ matrix.variant }}_${{ matrix.arch }}_build.log | awk '{print $2}')
if [[ "${AMI_ID}" == "" ]]; then
exit 1
else
echo "[Debug] AMI ID found in the build log: '${AMI_ID}'"
fi
echo "AMI_ID=${AMI_ID}" >> $GITHUB_ENV
echo "ami_${{ matrix.arch }}=${AMI_ID}" >> $GITHUB_OUTPUT
- name: Get AMI Name
run: |
AMI_NAME=$(aws ec2 describe-images --filters "Name=image-id,Values=${{ env.AMI_ID }}" --query 'Images[0].Name' --output text)
if [[ "${AMI_NAME}" == "" || "${AMI_NAME}" == "None" ]]; then
exit 1
else
echo "[Debug] AMI Name: '${AMI_NAME}'"
fi
echo "AMI_NAME=${AMI_NAME}" >> $GITHUB_ENV
- name: Launch permission for the AMI to Infra account
run: |
aws ec2 modify-image-attribute --image-id ${{ env.AMI_ID }} --launch-permission "Add=[{UserId=${{ env.ALMALINUX_AWS_INFRA_ACCOUNT_ID }}}]"
- name: Print AMI summary
uses: actions/github-script@v7
with:
result-encoding: string
script: |
core.summary
.addHeading('${{ env.AMI_NAME }}', '4')
.addHeading('AMI ID: ${{ env.AMI_ID }}', '5')
.write()
- name: Send notification to Mattermost
uses: mattermost/action-mattermost-notify@master
if: inputs.notify_mattermost
with:
MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_WEBHOOK_URL }}
MATTERMOST_CHANNEL: ${{ vars.MATTERMOST_CHANNEL }}
MATTERMOST_USERNAME: ${{ github.triggering_actor }}
TEXT: |
:almalinux: **${{ env.AMI_NAME }}** AWS AMI, built by the GitHub [Action](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
**AMI ID**: `${{ env.AMI_ID }}`
test-ami:
name: Test ${{ inputs.version_major }} ${{ matrix.arch }} AMI
if: inputs.test_ami
needs: [build-ami]
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 )}}
strategy:
fail-fast: false
matrix:
arch:
- aarch64
- x86_64
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4.0.2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ vars.AWS_REGION }}
- name: Install aws CLI
run: |
# Install aws CLI
sudo dnf install -y -q unzip
curl "https://awscli.amazonaws.com/awscli-exe-linux-$(uname -m).zip" -o "awscliv2.zip"
unzip -qq awscliv2.zip
sudo ./aws/install
- name: Get AMI ID
run: |
AMI_ID=${{ matrix.arch == 'x86_64' && needs.build-ami.outputs.ami_x86_64 || needs.build-ami.outputs.ami_aarch64 }}
echo "AMI_ID=${AMI_ID}" >> $GITHUB_ENV
- name: Get AMI architecture
run: |
AMI_ARCH=$(aws ec2 describe-images --image-ids ${{ env.AMI_ID }} --query 'Images[0].Tags' | jq -r '.[] | select(.Key == "Architecture") | .Value')
if [[ "${AMI_ARCH}" == "" || "${AMI_ARCH}" == "None" ]]; then
exit 1
else
echo "[Debug] AMI Arch: '${AMI_ARCH}'"
fi
echo "AMI_ARCH=${AMI_ARCH}" >> $GITHUB_ENV
- name: Get AlmaLinux versions
id: ami-version
run: |
AMI_VERSION=$(aws ec2 describe-images --image-ids ${{ env.AMI_ID }} --query 'Images[0].Tags' | jq -r '.[] | select(.Key == "Version") | .Value')
if [[ "${AMI_VERSION}" == "" || "${AMI_VERSION}" == "None" ]]; then
exit 1
else
echo "[Debug] AMI Version: '${AMI_VERSION}'"
fi
echo "AMI_VERSION=${AMI_VERSION}" >> $GITHUB_ENV
echo "OS_VERSION=$(echo $AMI_VERSION | sed 's/\.[0-9]\{8\}.*$//g')" >> $GITHUB_ENV
- name: Test AMI
run: |
case "${{ env.OS_VERSION }}" in
10)
OS_RELEASE="AlmaLinux Kitten release ${{ env.OS_VERSION }}"
;;
*)
OS_RELEASE="AlmaLinux release ${{ env.OS_VERSION }}"
;;
esac
echo "[Debug] AlmaLinux release:"
grep "${OS_RELEASE}" /etc/almalinux-release || exit 1
echo "[Debug] System architecture:"
rpm -q --qf='%{ARCH}\n' $(rpm -qf /etc/almalinux-release) | grep '${{ env.AMI_ARCH }}' || exit 1
echo "[Debug] Check for updates:"
dnf check-update || exit 1