@@ -15,24 +15,27 @@ description: Generate a list of Docker image tags for the current workflow.
15
15
16
16
inputs :
17
17
shortcut-api-token :
18
- description : Shortcut API token for fetching Story IDs related to a PR.
18
+ description : >
19
+ Shortcut API token for fetching Story IDs related to a PR.
19
20
required : false
20
21
prefix :
21
- description : |
22
- The prefix for tagging commits that are not yet merged to main/master.
23
- For example: a PR from some-branch would receive the tags <prefix>.some-branch, <prefix>.<COMMIT_SHA>.
22
+ description : >
23
+ Prefix for tagging commits that are not yet merged to main/master.
24
+ For example, a PR from some-branch would receive tags "<prefix>.some-branch"
25
+ and "<prefix>.<COMMIT_SHA>".
24
26
required : false
25
27
default : temp
26
28
27
29
outputs :
28
30
tags :
29
31
description : Comma-separated list of tags.
30
- value : ${{ steps.get-tags.outputs.tags }}
32
+ value : ${{ steps.get-all- tags.outputs.tags }}
31
33
32
34
runs :
33
35
using : composite
34
36
steps :
35
- - id : get-branch
37
+ - name : Get Branch
38
+ id : get-branch
36
39
shell : bash
37
40
run : |
38
41
if [ "$GITHUB_EVENT_NAME" == "pull_request" ]
@@ -45,55 +48,95 @@ runs:
45
48
printf 'Cannot determine tags: Workflow must be triggered by "push", "pull_request", or "release" event.' && exit 1
46
49
fi
47
50
51
+ echo "Branch is '$BRANCH'."
48
52
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
49
53
50
- - id : get-tags
54
+ - name : Get Commit SHA
55
+ id : get-sha
51
56
shell : bash
52
57
run : |
53
- BRANCH=${{ steps.get-branch.outputs.branch }}
54
- COMMIT_SHA=${GITHUB_SHA:0:7}
58
+ SHA="${{ github.event.pull_request.head.sha || github.sha }}"
59
+ SHA=${SHA:0:7}
60
+ echo "SHA is '$SHA'."
61
+ echo "sha=$SHA" >> $GITHUB_OUTPUT
62
+
63
+ - name : Get PR Number
64
+ id : get-pr-number
65
+ shell : bash
66
+ env :
67
+ GITHUB_TOKEN : ${{ github.token }}
68
+ run : |
69
+ SHA="${{ steps.get-sha.outputs.sha }}"
70
+
71
+ PR_NUMBER=$(
72
+ gh api \
73
+ -H "Accept: application/vnd.github+json" \
74
+ -H "X-GitHub-Api-Version: 2022-11-28" \
75
+ /repos/$GITHUB_REPOSITORY/commits/$SHA/pulls \
76
+ | jq -r ".[].number"
77
+ )
78
+
79
+ echo "PR number is '$PR_NUMBER'."
80
+ echo "pr-number=$PR_NUMBER" >> $GITHUB_OUTPUT
81
+
82
+ - name : Get Tags for Shortcut Stories
83
+ id : get-story-tags
84
+ shell : bash
85
+ if : ${{ inputs.shortcut-api-token && steps.get-pr-number.outputs.pr-number }}
86
+ run : |
87
+ PR_NUMBER="${{ steps.get-pr-number.outputs.pr-number }}"
88
+ url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$PR_NUMBER"
89
+
90
+ ids=$(
91
+ curl --silent -X GET \
92
+ -H "Content-Type: application/json" \
93
+ -H "Shortcut-Token: ${{ inputs.shortcut-api-token }}" \
94
+ -d "{ \"page_size\": 20, \"query\": \"is:story and pr:$url\" }" \
95
+ -L "https://api.app.shortcut.com/api/v3/search" \
96
+ | jq ".stories.data[].id"
97
+ )
98
+
99
+ TAGS=""
100
+ while read -r id; do
101
+ if [ ! -z "$id" ]; then
102
+ TAGS+="sc-$id,"
103
+ fi
104
+ done <<< "$ids"
105
+
106
+ # Remove the trailing comma.
107
+ TAGS="${TAGS::-1}"
108
+
109
+ echo "Story tags are '$TAGS'."
110
+ echo "tags=$TAGS" >> $GITHUB_OUTPUT
111
+
112
+ - name : Get Tags for Branch
113
+ id : get-branch-tags
114
+ shell : bash
115
+ run : |
116
+ BRANCH="${{ steps.get-branch.outputs.branch }}"
117
+ SHA="${{ steps.get-sha.outputs.sha }}"
55
118
56
119
if [ "$BRANCH" == "main" ] || [ "$BRANCH" == "master" ]
57
120
then
58
- TAGS="latest,$BRANCH,$COMMIT_SHA "
121
+ TAGS="latest,$BRANCH,$SHA "
59
122
else
60
123
SLASHLESS_BRANCH=`echo $BRANCH | tr / -`
61
- TAGS="${{ inputs.prefix }}.$SLASHLESS_BRANCH,${{ inputs.prefix }}.$COMMIT_SHA "
124
+ TAGS="${{ inputs.prefix }}.$SLASHLESS_BRANCH,${{ inputs.prefix }}.$SHA "
62
125
fi
63
126
64
- if [ "$GITHUB_EVENT_NAME" == "pull_request" ]
65
- then
66
- PR_NUMBER="${{ github.event.number }}"
67
- elif [ "$GITHUB_EVENT_NAME" == "push" ]
68
- then
69
- # Workflow was triggered by a "push" event.
70
- PR_NUMBER=$( \
71
- (echo '${{ github.event.head_commit.message }}' \
72
- | grep -o "\(#[0-9]\+\)" || true) \
73
- | sed "s/[#()]//g" \
74
- )
75
- else
76
- PR_NUMBER=""
77
- fi
127
+ echo "Branch tags are '$TAGS'."
128
+ echo "tags=$TAGS" >> $GITHUB_OUTPUT
78
129
79
- # Add additional tags for each Story ID associated with this PR.
80
- if [ ! -z "$PR_NUMBER" ] && [ ! -z "${{ inputs.shortcut-api-token }}" ]; then
81
- url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$PR_NUMBER"
82
-
83
- ids=$(
84
- curl --silent -X GET \
85
- -H "Content-Type: application/json" \
86
- -H "Shortcut-Token: ${{ inputs.shortcut-api-token }}" \
87
- -d "{ \"page_size\": 20, \"query\": \"is:story and pr:$url\" }" \
88
- -L "https://api.app.shortcut.com/api/v3/search" \
89
- | jq ".stories.data[].id"
90
- )
91
-
92
- while read -r id; do
93
- if [ ! -z "$id" ]; then
94
- TAGS="$TAGS,sc-$id"
95
- fi
96
- done <<< "$ids"
97
- fi
130
+ - name : Get All Tags
131
+ id : get-all-tags
132
+ shell : bash
133
+ run : |
134
+ BRANCH_TAGS="${{ steps.get-branch-tags.outputs.tags }}"
135
+ STORY_TAGS="${{ steps.get-story-tags.outputs.tags }}"
136
+
137
+ TAGS="$BRANCH_TAGS,$STORY_TAGS"
138
+ TAGS="${TAGS/%,/}"
139
+ TAGS="${TAGS/#,/}"
98
140
141
+ echo "Tags are '$TAGS'."
99
142
echo "tags=$TAGS" >> $GITHUB_OUTPUT
0 commit comments