Skip to content

Docker

Docker #399

name: Docker
on:
push:
# Publish `main` as Docker `latest` image.
branches:
- main
# Publish `v1.2.3` tags as releases.
tags:
- v*
# Run tests for any PRs.
pull_request:
# Build image once a week to apply updates in base image.
schedule:
- cron: 0 0 */7 * *
env:
IMAGE_NAME: ${{github.repository}}
jobs:
push:
runs-on: ubuntu-latest
if: github.event_name == 'push'
steps:
- uses: actions/checkout@v2
- name: Build image
run: docker build . --file Dockerfile --tag $IMAGE_NAME
- name: Log into registry
run: echo "${{ secrets.DOCKER_TOKEN }}" | docker login -u "${{ secrets.DOCKER_USER }}" --password-stdin
- name: Push image
run: |
GIT_HASH=$(git rev-parse --short "$GITHUB_SHA")
# Change all uppercase to lowercase
IMAGE_NAME=$(echo $IMAGE_NAME | 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" == "main" ] && VERSION=latest
echo IMAGE_NAME=$IMAGE_NAME
echo VERSION=$VERSION
docker tag $IMAGE_NAME $IMAGE_NAME:$GIT_HASH
docker tag $IMAGE_NAME $IMAGE_NAME:$VERSION
docker push -a $IMAGE_NAME
- name: Create and push tag
# skip this step if the push is a tag
if: startsWith(github.ref, 'refs/heads/') && github.event_name == 'push'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
GIT_HASH=$(git rev-parse --short "$GITHUB_SHA")
TAG_NAME="build-${{ github.run_number }}-${GIT_HASH}"
git tag $TAG_NAME
git push origin $TAG_NAME