-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.yml
113 lines (102 loc) · 3.56 KB
/
action.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
---
# https://help.github.com/en/articles/metadata-syntax-for-github-actions
name: 'Docker tag action'
description: "GitHub Action to create Docker tag based on branch or tag"
author: 'cytopia'
branding:
icon: 'code'
color: 'red'
inputs:
latest_git_branch:
description: 'Git branch name which will return "latest" as the Docker tag (default: master).'
required: false
default: 'master'
latest_docker_tag_name:
description: 'The name for the "latest" Docker tag (default: latest).'
required: false
default: 'latest'
non_latest_docker_tag_prefix:
description: 'The prefix for all Docker tags, which do not identify as the conventional latest Tag (default: empty)'
required: false
default: ''
non_latest_docker_tag_suffix:
description: 'The suffix for all Docker tags, which do not identify as the conventional latest Tag (default: empty)'
required: false
default: ''
outputs:
docker-tag:
description: "Docker tag to be used"
value: ${{ steps.get-docker-tag.outputs.docker-tag }}
runs:
using: "composite"
steps:
- name: Get Docker Tag
id: get-docker-tag
shell: bash
run: |
# Commit, Branch or Tag
HEAD="$( git branch --no-color | grep '^\*' | sed 's|)||g' | xargs -n1 | tail -1 )"
# Get current Commit Hash (short)
HASH="$( git rev-parse --short HEAD )"
# Hash is found in what Branch?
FROM="$( git branch --contains "${HASH}" | head -2 | tac | head -1 | sed 's|^*[[:space:]]*||g' | sed 's|)||g' | xargs -n1 | tail -1 | xargs )"
# How many commits behind FROM branch
BEHIND="$( git rev-list --count "${HASH}..${FROM}" )"
###
### TAG
###
if git name-rev "${HEAD}" | grep -E "^${HEAD} tags/${HEAD}" > /dev/null; then
GIT_NAME="${HEAD}"
GIT_TYPE="TAG"
GIT_HEAD="${HASH}"
DETACHED_FROM=${FROM}"
BEHIND=${BEHIND}"
###
### Branch or Detached Commit
###
else
# Detached Commit (not latest in branch)
if [ "${BEHIND}" -gt "0" ]; then
GIT_NAME="${HEAD}"
GIT_TYPE="COMMIT"
GIT_HEAD="${HASH}"
DETACHED_FROM="${FROM}"
BEHIND="${BEHIND}"
else
if [ "$( git branch | grep '^\*' | sed 's|^*[[:space:]]*||g' | xargs )" = "${FROM}" ]; then
DETACHED=
else
DETACHED="${FROM}"
fi
GIT_NAME="${FROM}"
GIT_TYPE="BRANCH"
GIT_HEAD="${HASH}"
DETACHED_FROM="${DETACHED}"
BEHIND="${BEHIND}"
fi
fi
###
### Determine Docker tag
###
if [ "${GIT_TYPE}" = "BRANCH" ] && [ "${GIT_NAME}" = "${{ inputs.latest_git_branch }}" ]; then
DOCKER_TAG="${{ inputs.latest_docker_tag_name }}"
else
DOCKER_TAG="${{ inputs.non_latest_docker_tag_prefix }}${GIT_NAME}${{ inputs.non_latest_docker_tag_suffix }}"
fi
###
### Replace slashes '/' with dashes '-'
###
DOCKER_TAG="${DOCKER_TAG//\//-}"
###
### Set final output for 'docker-tag'
###
echo "docker-tag=${DOCKER_TAG}" >> $GITHUB_OUTPUT
###
### Output information
###
echo "DOCKER_TAG=${DOCKER_TAG}"
echo "GIT_NAME=${GIT_NAME}"
echo "GIT_TYPE=${GIT_TYPE}"
echo "GIT_HEAD=${GIT_HEAD}"
echo "DETACHED_FROM=${DETACHED_FROM}"
echo "BEHIND=${BEHIND}"