Build Base Image with Packer #30
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |