modify action to build on ARM #92
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: CI/CD for website | |
on: | |
push: | |
branches: [main] | |
pull_request: | |
branches: [main] | |
jobs: | |
deploy-site: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up QEMU for multi-platform builds | |
uses: docker/setup-qemu-action@v2 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- id: install-aws-cli | |
uses: unfor19/install-aws-cli-action@v1 | |
with: | |
version: 2 | |
verbose: true | |
arch: arm64 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v2 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ${{ secrets.AWS_REGION }} | |
- name: Build Docker image | |
run: | | |
docker buildx build --platform linux/arm64 -t hbd:latest -f prod.Dockerfile --load . | |
docker tag hbd:latest hbd:latest | |
- name: Push Docker image to ECR | |
run: | | |
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com | |
docker tag hbd:latest ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/hbd:latest | |
docker push ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/hbd:latest | |
- name: Delete untagged Docker images from ECR | |
run: | | |
REPOSITORY_NAME="hbd" | |
AWS_REGION=${{ secrets.AWS_REGION }} | |
# Get all untagged image IDs | |
UNTAGGED_IMAGE_IDS=$(aws ecr list-images --repository-name $REPOSITORY_NAME --region $AWS_REGION --filter "tagStatus=UNTAGGED" --query 'imageIds[*]' --output json) | |
# Delete untagged images | |
if [ "$UNTAGGED_IMAGE_IDS" != "[]" ]; then | |
aws ecr batch-delete-image --repository-name $REPOSITORY_NAME --region $AWS_REGION --image-ids "$UNTAGGED_IMAGE_IDS" | |
fi | |
- name: Deploy to EC2 via SSM | |
run: | | |
aws ssm send-command \ | |
--document-name "AWS-RunShellScript" \ | |
--targets "Key=instanceids,Values=${{ secrets.EC2_INSTANCE_ID }}" \ | |
--parameters '{ | |
"commands":[ | |
"cd /home/ubuntu/hbd", | |
"sudo git pull", | |
"aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com", | |
"docker pull ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/hbd:latest", | |
"AWS_ACCOUNT_ID=${{ secrets.AWS_ACCOUNT_ID }} AWS_REGION=${{ secrets.AWS_REGION }} docker compose -f docker-compose-prod.yml up -d hbd" | |
] | |
}' \ | |
--comment "Deploying new version of hbd" |