-
Notifications
You must be signed in to change notification settings - Fork 3
74 lines (62 loc) · 2.01 KB
/
create-ami-base.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
name: Build Base Image with Packer
on:
schedule:
- cron: '0 17 * * *'
workflow_dispatch:
inputs:
environment:
description: 'Image deployment environment'
required: true
default: 'stg'
options:
- stg
- prod
permissions:
id-token: write
contents: read
jobs:
build-ami:
runs-on: codebuild-problem-github-actions-${{ github.run_id }}-${{ github.run_attempt }}
name: build isucon14 base image
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install unzip
run: |
sudo apt-get update
sudo apt-get install unzip
- name: Set up Packer
uses: hashicorp/setup-packer@main
with:
version: "1.11.2"
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install Ansible
run: |
python -m pip install --upgrade pip
pip install ansible
- name: Set AWS Role ARN
id: set-role
run: |
if [ "${{ github.event.inputs.environment }}" == "prod" ]; then
echo "AWS_PACKER_BUILD_ROLE_ARN=${{ vars.AWS_PROD_PACKER_BUILD_ROLE_ARN }}" >> $GITHUB_ENV
else
echo "AWS_PACKER_BUILD_ROLE_ARN=${{ vars.AWS_STG_PACKER_BUILD_ROLE_ARN }}" >> $GITHUB_ENV
fi
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ env.AWS_PACKER_BUILD_ROLE_ARN }}
aws-region: ap-northeast-1
- name: Build
run: |
cd provisioning/packer
make base-build
- name: Cleanup old AMIs
run: |
PREFIX="isucon14_baseimage-"
aws ec2 describe-images --owners self --filters "Name=name,Values=${PREFIX}*" --query 'Images[*].[ImageId,CreationDate]' --output json | \
jq -r 'sort_by(.[1]) | reverse | .[3:] | .[][0]' | \
xargs -I {} aws ec2 deregister-image --image-id {}