forked from tractorcow/cow
-
Notifications
You must be signed in to change notification settings - Fork 8
68 lines (55 loc) · 2.53 KB
/
docker-image-publish.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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