From d7c68749bc9164d098bb35f75eb89fb4454776ef Mon Sep 17 00:00:00 2001 From: Mikhail Andrenkov Date: Fri, 27 Oct 2023 15:45:59 -0400 Subject: [PATCH] Modularize the GDIT Action --- generate-docker-image-tags/action.yml | 85 ++++++++++++++++++--------- 1 file changed, 56 insertions(+), 29 deletions(-) diff --git a/generate-docker-image-tags/action.yml b/generate-docker-image-tags/action.yml index a9b4a3b..03db565 100644 --- a/generate-docker-image-tags/action.yml +++ b/generate-docker-image-tags/action.yml @@ -47,20 +47,10 @@ runs: echo "branch=$BRANCH" >> $GITHUB_OUTPUT - - id: get-tags + - id: get-pr-number shell: bash + if: inputs.shortcut-api-token run: | - BRANCH=${{ steps.get-branch.outputs.branch }} - COMMIT_SHA=${GITHUB_SHA:0:7} - - if [ "$BRANCH" == "main" ] || [ "$BRANCH" == "master" ] - then - TAGS="latest,$BRANCH,$COMMIT_SHA" - else - SLASHLESS_BRANCH=`echo $BRANCH | tr / -` - TAGS="${{ inputs.prefix }}.$SLASHLESS_BRANCH,${{ inputs.prefix }}.$COMMIT_SHA" - fi - if [ "$GITHUB_EVENT_NAME" == "pull_request" ] then PR_NUMBER="${{ github.event.number }}" @@ -76,24 +66,61 @@ runs: PR_NUMBER="" fi - # Add additional tags for each Story ID associated with this PR. - if [ ! -z "$PR_NUMBER" ] && [ ! -z "${{ inputs.shortcut-api-token }}" ]; then - url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$PR_NUMBER" - - ids=$( - curl --silent -X GET \ - -H "Content-Type: application/json" \ - -H "Shortcut-Token: ${{ inputs.shortcut-api-token }}" \ - -d "{ \"page_size\": 20, \"query\": \"is:story and pr:$url\" }" \ - -L "https://api.app.shortcut.com/api/v3/search" \ - | jq ".stories.data[].id" - ) + echo "PR number is '$PR_NUMBER'." + echo "pr-number=$PR_NUMBER" >> $GITHUB_OUTPUT + + - id: get-story-tags + shell: bash + if: inputs.shortcut-api-token + run: | + PR_NUMBER=${{ steps.get-pr-number.outputs.pr-number }} + url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$PR_NUMBER" + + ids=$( + curl --silent -X GET \ + -H "Content-Type: application/json" \ + -H "Shortcut-Token: ${{ inputs.shortcut-api-token }}" \ + -d "{ \"page_size\": 20, \"query\": \"is:story and pr:$url\" }" \ + -L "https://api.app.shortcut.com/api/v3/search" \ + | jq ".stories.data[].id" + ) + + TAGS=() + while read -r id; do + if [ ! -z "$id" ]; then + TAGS+=( "sc-$id" ) + fi + done <<< "$ids" + + echo "Story tags are '$TAGS'." + echo "tags=$TAGS" >> $GITHUB_OUTPUT - while read -r id; do - if [ ! -z "$id" ]; then - TAGS="$TAGS,sc-$id" - fi - done <<< "$ids" + - id: get-branch-tags + shell: bash + if: inputs.shortcut-api-token + run: | + BRANCH=${{ steps.get-branch.outputs.branch }} + COMMIT_SHA=${GITHUB_SHA:0:7} + + if [ "$BRANCH" == "main" ] || [ "$BRANCH" == "master" ] + then + TAGS=("latest" "$BRANCH" "$COMMIT_SHA") + else + SLASHLESS_BRANCH=`echo $BRANCH | tr / -` + TAGS=("${{ inputs.prefix }}.$SLASHLESS_BRANCH" "${{ inputs.prefix }}.$COMMIT_SHA") fi + echo "Branch tags are '$TAGS'." + echo "tags=$TAGS" >> $GITHUB_OUTPUT + + - id: get-tags + shell: bash + run: | + BRANCH_TAGS=${{ steps.get-branch-tags.outputs.branch }} + STORY_TAGS=${{ steps.get-story-tags.outputs.branch }} + + TAGS=("${BRANCH_TAGS[@]}" "${STORY_TAGS[@]}") + TAGS=$(IFS=, ; echo "${TAGS[@]}") + + echo "Tags are '$TAGS'." echo "tags=$TAGS" >> $GITHUB_OUTPUT