Update push docker container to support container registry

Also simplify the existing workflow by replacing shell code with actions
This commit is contained in:
Sven Pfleiderer
2021-05-26 10:41:53 -07:00
parent 7c29ac2481
commit 282b038713
+35 -34
View File
@@ -2,20 +2,22 @@ name: Docker
on: on:
push: push:
# Publish `$default-branch` as Docker `latest` image. branches: [ $default-branch ]
branches:
- $default-branch
# Publish `v1.2.3` tags as releases. # Publish `v1.2.3` tags as releases.
tags: tags: [ 'v*.*.*' ]
- v* pull_request_target:
branches: [ $default-branch ]
# Run tests for any PRs. # Run tests for any PRs.
pull_request: pull_request:
env: env:
# TODO: Change variable to your image's name. # TODO: Creates container named ACCOUNT/REPO
IMAGE_NAME: image # Append /IMAGE-NAME to create a container named ACCOUNT/REPO/IMAGE-NAME
IMAGE_NAME: ${{ github.repository }}
# Registry. Use docker.io for DockerHub
REGISTRY: ghcr.io
jobs: jobs:
# Run tests. # Run tests.
@@ -49,32 +51,31 @@ jobs:
packages: write packages: write
steps: steps:
- uses: actions/checkout@v2 - name: Checkout repository
uses: actions/checkout@v2
- name: Build image - name: Log into registry ${{ env.REGISTRY }}
run: docker build . --file Dockerfile --tag $IMAGE_NAME uses: docker/login-action@v1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log into registry - name: Extract Docker metadata
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin id: meta
uses: docker/metadata-action@v3
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
- name: Push image - name: Build and push Docker image
run: | uses: docker/build-push-action@v2
IMAGE_ID=docker.pkg.github.com/${{ github.repository }}/$IMAGE_NAME with:
context: .
# Change all uppercase to lowercase # Don't push on pull requests
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
# Strip git ref prefix from version labels: ${{ steps.meta.outputs.labels }}
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
# Use Docker `latest` tag convention
[ "$VERSION" == "$default-branch" ] && VERSION=latest
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION