Skip to content

infra: preview branches use self hosting #23

infra: preview branches use self hosting

infra: preview branches use self hosting #23

name: Build Docker Images for Preview Branches
on:
pull_request:
types: [opened, synchronize, reopened]
concurrency:
group: ${{ github.head_ref }}-build-images
cancel-in-progress: true
jobs:
build_images:
permissions:
contents: read
timeout-minutes: 30
runs-on: ${{ matrix.runner }}
strategy:
matrix:
include:
- service: api
runner: ubuntu-latest-8-cores
- service: client
runner: ubuntu-latest-8-cores
- service: connection
runner: ubuntu-latest-8-cores
- service: files
runner: ubuntu-latest-8-cores
- service: multiplayer
runner: ubuntu-latest-8-cores
fail-fast: true
steps:
- uses: actions/checkout@v4
- name: Generate Build Metadata
id: build-metadata
run: |
echo "BUILD_TIME=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
echo "GIT_SHA_SHORT=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
echo "BRANCH_NAME=$(echo "${{ github.head_ref }}" | tr '/' '-')" >> $GITHUB_OUTPUT
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_DEVELOPMENT }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_DEVELOPMENT }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Login to Amazon ECR Private
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Define repository name
id: repo-name
run: |
echo "REPO_NAME=quadratic-${{ matrix.service }}-development" >> $GITHUB_OUTPUT
- name: Create Private ECR Repository if not exists
id: create-ecr
env:
REPO_NAME: ${{ steps.repo-name.outputs.REPO_NAME }}
run: |
# Try to describe the repository first
if ! aws ecr describe-repositories --repository-names $REPO_NAME 2>/dev/null; then
# Repository doesn't exist, create it
aws ecr create-repository --repository-name $REPO_NAME || true
fi
# Get the repository URI either way
REPO_INFO=$(aws ecr describe-repositories --repository-names $REPO_NAME)
ECR_URL=$(echo $REPO_INFO | jq -r '.repositories[0].repositoryUri')
echo "ECR_URL=$ECR_URL" >> $GITHUB_OUTPUT
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
buildkitd-flags: --debug
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ matrix.service }}-pr-${{ github.event.pull_request.number }}
restore-keys: |
${{ runner.os }}-buildx-${{ matrix.service }}-
${{ runner.os }}-buildx-
- name: Build, Tag, and Push Image to Amazon ECR Private
env:
ECR_URL: ${{ steps.create-ecr.outputs.ECR_URL }}
run: |
docker buildx build \
--cache-from=type=local,src=/tmp/.buildx-cache \
--cache-to=type=local,dest=/tmp/.buildx-cache-new,mode=max \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--build-arg BUILD_TIME="${{ steps.build-metadata.outputs.BUILD_TIME }}" \
--build-arg GIT_SHA="${{ github.sha }}" \
--build-arg PR_NUMBER="${{ github.event.pull_request.number }}" \
--label "org.opencontainers.image.created=${{ steps.build-metadata.outputs.BUILD_TIME }}" \
--label "org.opencontainers.image.revision=${{ github.sha }}" \
--push \
-t $ECR_URL:pr-${{ github.event.pull_request.number }} \
-f quadratic-${{ matrix.service }}/Dockerfile \
.
# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache