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:
push:
# Publish `$default-branch` as Docker `latest` image.
branches:
- $default-branch
branches: [ $default-branch ]
# Publish `v1.2.3` tags as releases.
tags:
- v*
tags: [ 'v*.*.*' ]
pull_request_target:
branches: [ $default-branch ]
# Run tests for any PRs.
pull_request:
env:
# TODO: Change variable to your image's name.
IMAGE_NAME: image
# TODO: Creates container named ACCOUNT/REPO
# 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:
# Run tests.
@@ -49,32 +51,31 @@ jobs:
packages: write
steps:
- uses: actions/checkout@v2
- name: Checkout repository
uses: actions/checkout@v2
- name: Build image
run: docker build . --file Dockerfile --tag $IMAGE_NAME
- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@v1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log into registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin
- name: Extract Docker metadata
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
run: |
IMAGE_ID=docker.pkg.github.com/${{ github.repository }}/$IMAGE_NAME
# Change all uppercase to lowercase
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
# Strip git ref prefix from version
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
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
# Don't push on pull requests
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}