Skip to content

Commit

Permalink
Build on codebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
dwilkie committed Sep 14, 2024
1 parent e30792a commit 90f9df2
Showing 1 changed file with 98 additions and 40 deletions.
138 changes: 98 additions & 40 deletions .github/workflows/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name: Services

env:
CI: true
ECR_REGISTRY: 324279636507.dkr.ecr.ap-southeast-1.amazonaws.com
GHCR_REGISTRY: ghcr.io/somleng
REPOSITORY_NAME: somleng-switch-services

jobs:
build:
Expand All @@ -11,6 +14,8 @@ jobs:
outputs:
matrix: ${{ steps.set-deployment-matrix.outputs.matrix }}
matrixLength: ${{ steps.set-deployment-matrix.outputs.matrixLength }}
packageMatrix: ${{ steps.set-deployment-matrix.outputs.packageMatrix }}

defaults:
run:
working-directory: components/services
Expand Down Expand Up @@ -62,25 +67,114 @@ jobs:
"identifier": "switch-services-staging",
"environment": "staging",
"branch": "use_official_aws_lambda_images",
"image_tag": "staging"
"friendly_image_tag": "beta",
"image_tag": "stag-${{ github.sha }}"
},
{
"identifier": "switch-services",
"environment": "production",
"branch": "master",
"image_tag": "latest"
"friendly_image_tag": "latest",
"image_tag": "prod-${{ github.sha }}"
}
]
EOF
)
matrix=$(echo $matrixSource | jq --arg branchName "$branchName" 'map(. | select((.branch==$branchName)) )')
echo "matrix={\"include\":$(echo $matrix)}" >> $GITHUB_OUTPUT
echo "matrixLength=$(echo $matrix | jq length)" >> $GITHUB_OUTPUT
echo "packageMatrix={\"platform\":[\"arm64\"],\"include\":$(echo $matrix)}" >> $GITHUB_OUTPUT
build-packages:
name: Build Packages
runs-on: ubuntu-latest
if: needs.build.outputs.matrixLength > 0

strategy:
matrix: ${{fromJSON(needs.build.outputs.packageMatrix)}}
fail-fast: false

needs:
- build

steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
role-skip-session-tagging: true
role-duration-seconds: 3600
aws-region: ap-southeast-1

- name: Build image
uses: aws-actions/aws-codebuild-run-build@v1
with:
project-name: somleng-switch-${{ matrix.platform }}
buildspec-override: |
version: 0.2
phases:
build:
steps:
- name: Build
run: |
aws ecr get-login-password --region ap-southeast-1 | docker login --username AWS --password-stdin ${{ env.ECR_REGISTRY }}
export DOCKER_BUILDKIT=1
docker buildx --cache-from ${{ env.ECR_REGISTRY }}/${{ env.REPOSITORY_NAME }}:${{ matrix.friendly_image_tag }}-${{ matrix.platform }} --tag ${{ env.ECR_REGISTRY }}/${{ env.REPOSITORY_NAME }}:${{ matrix.friendly_image_tag }}-${{ matrix.platform }} --push .
docker buildx imagetools create -t ${{ env.ECR_REGISTRY }}/${{ env.REPOSITORY_NAME }}:${{ matrix.friendly_image_tag }} -t ${{ env.ECR_REGISTRY }}/${{ env.REPOSITORY_NAME }}:${{ matrix.image_tag }} ${{ env.ECR_REGISTRY }}/${{ env.REPOSITORY_NAME }}:${{ matrix.friendly_image_tag }}-arm64
publish_images:
name: Publish Images
runs-on: ubuntu-latest

needs:
- build
- build-packages

strategy:
matrix: ${{fromJSON(needs.build.outputs.matrix)}}

steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
role-skip-session-tagging: true
role-duration-seconds: 3600
aws-region: ap-southeast-1

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Login to ECR
uses: docker/login-action@v3
with:
registry: ${{ env.ECR_REGISTRY }}

- name: Publish Images
run: |
docker image pull ${{ env.ECR_REGISTRY }}/${{ env.REPOSITORY_NAME }}:${{ matrix.friendly_image_tag }}-arm64
docker tag ${{ env.ECR_REGISTRY }}/${{ env.REPOSITORY_NAME }}:${{ matrix.friendly_image_tag }}-arm64 ${{ env.GHCR_REGISTRY }}/${{ env.REPOSITORY_NAME }}:${{ matrix.friendly_image_tag }}-arm64
docker push ${{ env.GHCR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ matrix.friendly_image_tag }}-arm64
docker buildx imagetools create -t ${{ env.GHCR_REGISTRY }}/${{ env.REPOSITORY_NAME }}:${{ matrix.friendly_image_tag }} "${{ env.GHCR_REGISTRY }}/${{ env.REPOSITORY_NAME }}:${{ matrix.friendly_image_tag }}-arm64"
deploy:
name: Deploy
runs-on: ubuntu-latest
needs: build
needs:
- build
- build-packages

if: needs.build.outputs.matrixLength > 0
defaults:
run:
Expand All @@ -90,10 +184,6 @@ jobs:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ap-southeast-1
ECR_REGISTRY: 324279636507.dkr.ecr.ap-southeast-1.amazonaws.com
ECR_REPOSITORY: 324279636507.dkr.ecr.ap-southeast-1.amazonaws.com/somleng-switch-services
GHCR_REPOSITORY: ghcr.io/somleng/switch-services
IMAGE_TAG: ${{ github.sha }}

strategy:
matrix: ${{fromJson(needs.build.outputs.matrix)}}
Expand All @@ -113,42 +203,10 @@ jobs:
role-duration-seconds: 3600
aws-region: ap-southeast-1

- name: Login to AWS ECR
uses: docker/login-action@v3
with:
registry: ${{ env.ECR_REGISTRY }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push
uses: docker/build-push-action@v6
with:
push: true
provenance: false
platforms: linux/arm64
cache-from: type=gha,scope=${{ matrix.identifier }}
cache-to: type=gha,mode=max,scope=${{ matrix.identifier }}
context: components/services
tags: |
${{ env.ECR_REPOSITORY }}:${{ matrix.image_tag }}
${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }}
${{ env.GHCR_REPOSITORY }}:${{ matrix.image_tag }}
- name: Deploy Lambda
run: |
aws lambda update-function-code --function-name ${{ matrix.identifier }} \
--image-uri ${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }} \
--image-uri ${{ env.REPOSITORY_NAME }}:${{ matrix.image_tag }} \
--architectures "arm64" \
--publish
Expand Down

0 comments on commit 90f9df2

Please sign in to comment.