Merge pull request #250 from creative-commoners/pulls/master/class #55
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Publish Docker image | |
on: | |
push: | |
branches: | |
- master | |
tags: | |
- '*' | |
jobs: | |
build: | |
name: Docker image build and publish | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Build image | |
run: | | |
# The following AWK command removes first two parts of the value delimited by slash | |
# As such, "ref/tags/1.0" becomes "1.0", but "ref/heads/feature/foo" becomes "feature/foo" | |
export COW_VERSION=$(echo $GITHUB_REF | awk '{ n=split($0, parts, "/"); result = ""; for(i=3; i<=n; ++i){result = result "/" parts[i] }; print substr(result, 2) }') | |
if [[ "$GITHUB_REF" == "refs/heads/"* ]] ; then | |
COMPOSER_COW_VERSION="dev-$COW_VERSION"; | |
else | |
COMPOSER_COW_VERSION="$COW_VERSION" | |
fi | |
# Log/Debug output | |
echo "Building version: $COW_VERSION" | |
# The next line preserves $COW_VERSION across the steps | |
echo "COW_VERSION=$COW_VERSION" >> $GITHUB_ENV | |
# Building a new image and tag it as "cow-image" | |
docker build -t 'cow-image' --target stable --build-arg COW_VERSION=$COMPOSER_COW_VERSION - < docker/Dockerfile | |
- name: Log into GitHub Packages registry | |
run: | | |
# The security token is issued automatically by GitHub for GitHub Actions of the same repository | |
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin | |
- name: Push image | |
run: | | |
if [[ "$GITHUB_REF" == "refs/heads/master" ]] ; then | |
# If it's master, release a timestamp version | |
IMAGE_ID=ghcr.io/${{ github.repository }}/master | |
VERSION=$(date +%s) | |
elif [[ "$GITHUB_REF" == "refs/tags/"* ]] ; then | |
# If it's a tag, name it "release" and use the tag as the release version | |
IMAGE_ID=ghcr.io/${{ github.repository }}/release | |
VERSION=$COW_VERSION | |
else | |
# If it's a branch, use the branch name and timestamp as the version | |
# WARNING: branch names out of [0-9a-z_-] won't work | |
# Also, see "on.push.branches" rule at the top of this file (only whitelisted branches would get here) | |
IMAGE_ID=ghcr.io/${{ github.repository }}/$COW_VERSION | |
VERSION=$(date +%s) | |
fi | |
echo "Publishing $IMAGE_ID:$VERSION" | |
docker tag cow-image $IMAGE_ID:$VERSION | |
docker tag cow-image $IMAGE_ID:latest | |
docker push $IMAGE_ID:$VERSION | |
docker push $IMAGE_ID:latest |