From 643a0997e918c5b91c4a3de4264a62e68f41787b Mon Sep 17 00:00:00 2001 From: Louis Orleans Date: Thu, 31 Aug 2023 13:59:56 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20build=20&=20publish=20container?= =?UTF-8?q?=20image?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build-publish.yaml | 66 ++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 .github/workflows/build-publish.yaml diff --git a/.github/workflows/build-publish.yaml b/.github/workflows/build-publish.yaml new file mode 100644 index 0000000..971c94f --- /dev/null +++ b/.github/workflows/build-publish.yaml @@ -0,0 +1,66 @@ +name: 'Publish Container Image' + +# Controls when the action will run. Triggers the workflow on push or pull +# request events but only for the master branch +on: + push: + branches: + - '*' + +permissions: + contents: 'read' + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build-and-publish: + # The type of runner that the job will run on + runs-on: 'ubuntu-latest' + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + - name: 'Checkout Repository 🛎️' + uses: 'actions/checkout@v2' + + - name: 'Build 🏗️' + id: 'build' + run: | + image="$( \ + basename "$(echo "${{ github.repository }}")" \ + | tr '[:upper:]' '[:lower:]' \ + | sed 's/docker-//' \ + )" + echo "image=$image" >> "$GITHUB_OUTPUT" + + docker build --file "Dockerfile" --tag "$image" . + + - name: 'Login to GitHub Container Registry 🔑' + uses: 'docker/login-action@v1' + if: '${{ github.ref_name }} == ${{ github.event.repository.default_branch }}' + with: + registry: 'ghcr.io' + username: '${{ github.repository_owner }}' + password: '${{ secrets.GITHUB_TOKEN }}' + + - name: 'Publish to Registry 🐳' + if: '${{ github.ref_name }} == ${{ github.event.repository.default_branch }}' + run: | + image="${{ steps.build.outputs.image }}" + repo="ghcr.io/${{ github.repository_owner }}/$image" + branch="$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')" + version="$branch" + + echo "repo = $repo" + echo "image = $image" + echo "version = $version" + + docker tag "$image" "$repo/$image:$version" + docker push "$repo/$image:$version" + + # Use Docker `latest` tag convention + if [[ "${{ github.event.repository.default_branch }}" == "$branch" ]]; then + docker tag "$image" "$repo/$image:latest" + docker push "$repo/$image:latest" + fi + + echo "registry_uri=$repo/$image" >> "$GITHUB_OUTPUT"