Skip to content

Configure CI to run in Github Actions #3

Configure CI to run in Github Actions

Configure CI to run in Github Actions #3

Workflow file for this run

name: CI
on:
push:
branches:
- "dev"
- "main"
pull_request:
types: [opened, reopened, synchronize]
jobs:
meta:
name: Generate and check metadata
runs-on: ubuntu-latest
outputs:
environment: ${{ steps.environment.outputs.environment }}
labels: ${{ steps.meta.outputs.labels }}
tag: ${{ steps.meta.outputs.tags }}
tag_names: ${{ steps.meta.outputs.tag_names }}
tag_exists: ${{ steps.check-image.outputs.tag_exists }}
json: ${{ steps.meta.outputs.json }}
version: ${{ steps.meta.outputs.version }}
steps:
- name: Set environment variable based on branch
id: environment
run: |
if [[ "${{ github.ref_name }}" == "main" ]]; then
echo "environment=main" >> "$GITHUB_OUTPUT"
elif [[ "${{ github.ref_name }}" == "dev" ]]; then
echo "environment=dev" >> "$GITHUB_OUTPUT"
fi
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ vars.REGISTRY_IMAGE }}
tags: |
type=sha,format=long
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Check if image exists already
id: check-image
run: |
if docker manifest inspect ${{ steps.meta.outputs.tags }} > /dev/null 2>&1; then
echo "tag_exists=true" >> "$GITHUB_OUTPUT"
else
echo "tag_exists=false" >> "$GITHUB_OUTPUT"
fi
test:
name: Test
needs: meta
runs-on: ubuntu-latest
if: ${{ needs.meta.outputs.tag_exists == 'false' }}
container: node:18.10.0-bullseye
env:
DATABASE_URL: postgres://postgres@localhost/bats
SMTP_HOST: 'localhost'
services:
db:
image: postgres:12
mailcatcher:
image: dockage/mailcatcher:0.8.2
steps:
- name: Check out repository code
uses: actions/checkout@v4
- name: Set up package cache
uses: actions/setup-node@v4
with:
node-version: '18.10.0'
cache: 'yarn'
- name: Install Dependencies
run: yarn install && yarn workspace e2e install-chromium
- name: Create database
run: yarn workspace server sequelize db:create && yarn workspace server sequelize db:create --env test
- name: Run migrations
run: yarn workspace server sequelize db:migrate && yarn workspace server sequelize db:migrate --env test
- name: Run seeds
run: yarn workspace server sequelize db:seed:all
- name: Run Tests
run: eval $(cat example.env | grep -v -e 'AGORA_APP_CERTIFICATE' -e 'REACT_APP_AGORA_APP_ID' -e 'DATABASE_URL' -e 'SMTP_HOST' | sed 's/^/export /'); yarn run test
build:
name: Build Image
needs: [ meta, test ]
strategy:
fail-fast: false
matrix:
runner:
- ubuntu-24.04
- ubuntu-24.04-arm
runs-on: ${{ matrix.runner }}
if: ${{ github.ref_name == 'dev' || github.ref_name == 'main' }}
steps:
- name: Prepare
run: |
if [[ ${{ matrix.runner }} == "ubuntu-24.04-arm" ]]; then
platform=linux/arm64
else
platform=linux/amd64
fi
echo "PLATFORM=${platform}" >> $GITHUB_ENV
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
- name: Set up docker buildx
uses: docker/setup-buildx-action@v3
- name: Login to docker hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push by digest
id: build
uses: docker/build-push-action@v6
with:
platforms: ${{ env.PLATFORM }}
labels: ${{ needs.meta.outputs.labels }}
tags: ${{ vars.REGISTRY_IMAGE }}
outputs: type=image,push-by-digest=true,name-canonical=true,push=true
- name: Export digest
run: |
mkdir -p ${{ runner.temp }}/digests
digest="${{ steps.build.outputs.digest }}"
touch "${{ runner.temp }}/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-${{ env.PLATFORM_PAIR }}
path: ${{ runner.temp }}/digests/*
if-no-files-found: error
retention-days: 1
merge:
runs-on: ubuntu-latest
needs: [ meta, build ]
env:
DOCKER_METADATA_OUTPUT_VERSION: ${{ needs.meta.outputs.version }}
DOCKER_METADATA_OUTPUT_TAGS: ${{ needs.meta.outputs.tag }}
DOCKER_METADATA_OUTPUT_TAG_NAMES: ${{ needs.meta.outputs.tag_names }}
DOCKER_METADATA_OUTPUT_LABELS: ${{ needs.meta.outputs.labels }}
DOCKER_METADATA_OUTPUT_JSON: ${{ needs.meta.outputs.json }}
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
path: ${{ runner.temp }}/digests
pattern: digests-*
merge-multiple: true
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Create manifest list and push
working-directory: ${{ runner.temp }}/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ vars.REGISTRY_IMAGE }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ needs.meta.outputs.tag }}
deploy:
name: Deploy
needs: [ meta, merge ]
if: |
always()
&& (needs.meta.outputs.tag_exists == 'true' || needs.merge.result == 'success')
runs-on: ubuntu-latest
environment: ${{ needs.meta.outputs.environment }}
steps:
- name: Push to dokku
uses: dokku/github-action@master
with:
git_remote_url: ${{ vars.DOKKU_GIT_REMOTE_URL }}
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
deploy_docker_image: ${{ needs.meta.outputs.tag }}