From 745cd22d85231a59e914449f431dbdda6bd44b6e Mon Sep 17 00:00:00 2001 From: Ryan Lewis Date: Fri, 8 Dec 2023 12:55:43 -0600 Subject: [PATCH] Add docker automation --- .github/workflows/docker-publish.yml | 65 ++++++++++++++++++++++++++++ scripts/build.sh | 9 +++- scripts/name.sh | 5 +++ scripts/publish.sh | 8 ++++ scripts/version.sh | 5 +++ 5 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/docker-publish.yml create mode 100755 scripts/name.sh create mode 100755 scripts/publish.sh create mode 100755 scripts/version.sh diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..4738982 --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,65 @@ +name: Docker Build and Publish + +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +on: + push: + branches: [ "main" ] + # Publish semver tags as releases. + tags: [ '*' ] + pull_request: + branches: [ "main" ] + +env: + # Use docker.io for Docker Hub if empty + REGISTRY: ghcr.io + # github.repository as / + IMAGE_NAME: ${{ github.repository }} + + +jobs: + build: + + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + # Login against a Docker registry except on PR + # https://github.com/docker/login-action + - name: Log into registry ${{ env.REGISTRY }} + if: github.event_name != 'pull_request' + uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # Extract metadata (tags, labels) for Docker + # https://github.com/docker/metadata-action + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + # Build and push Docker image with Buildx (don't push on PR) + # https://github.com/docker/build-push-action + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 + with: + context: . + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + #cache-from: type=gha + #cache-to: type=gha,mode=max + diff --git a/scripts/build.sh b/scripts/build.sh index a318616..1129237 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -2,7 +2,12 @@ SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" -cd $SCRIPTPATH/.. +version=`$SCRIPTPATH/version.sh` +name=`$SCRIPTPATH/name.sh` -docker compose build +cd $SCRIPTPATH/.. +docker build \ + -t ghcr.io/ad-sdl/${name}:${version} \ + -t ghcr.io/ad-sdl/${name}:dev \ + . diff --git a/scripts/name.sh b/scripts/name.sh new file mode 100755 index 0000000..d28c978 --- /dev/null +++ b/scripts/name.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" + +echo `grep -oP '(?<=name = ")[^"]+' $SCRIPTPATH/../pyproject.toml | head -n 1` diff --git a/scripts/publish.sh b/scripts/publish.sh new file mode 100755 index 0000000..f773432 --- /dev/null +++ b/scripts/publish.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" + +version=`$SCRIPTPATH/version.sh` +name=`$SCRIPTPATH/name.sh` + +docker push ghcr.io/ad-sdl/${name}:${version} diff --git a/scripts/version.sh b/scripts/version.sh new file mode 100755 index 0000000..f7e983c --- /dev/null +++ b/scripts/version.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" + +echo `grep -oP '(?<=version = ")[^"]+' $SCRIPTPATH/../pyproject.toml | head -n 1`