Build AMI with Packer #67
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 AMI with Packer | |
on: | |
schedule: | |
- cron: '0 18 * * *' | |
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 contestant 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: Setup Go | |
uses: actions/setup-go@v5 | |
with: | |
go-version: 1.23 | |
- name: Setup Task | |
uses: arduino/setup-task@v2 | |
with: | |
version: 3.x | |
repo-token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Setup Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: 20 | |
- name: Setup pnpm | |
uses: pnpm/action-setup@v4 | |
with: | |
version: 9 | |
- 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 | |
# - name: Cleanup old AMIs | |
# run: | | |
# PREFIX="isucon14_contestant-" | |
# 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 {} |