Skip to content

chore: [FX-4058] update jira deployments GH Action #29

chore: [FX-4058] update jira deployments GH Action

chore: [FX-4058] update jira deployments GH Action #29

on:
# push:
# branches:
# - main
pull_request:
branches:
- master # triggers the flow for every PR to master
- 'feature/**' # triggers the flow for a PR to a branch like feature/v9
types:
- synchronize # PR was updated
- opened # PR was open
- reopened # PR was closed and is now open again
- ready_for_review # PR was converted from draft to open
jobs:
create-jira-deployment:
runs-on: ubuntu-latest
name: Create JIRA deployment
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OWNER: toptal
REPO: picasso
ENV_NAME: temploy
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install Dependencies
uses: toptal/davinci-github-actions/[email protected]
- id: branch
uses: xt0rted/[email protected]
- name: Check ENV Variables
run: |
echo "BASE_REF=${{ steps.branch.outputs.base_ref }}" >> $GITHUB_ENV
echo "HEAD_REF=${{ steps.branch.outputs.head_ref }}" >> $GITHUB_ENV
# - name: Check ENV Variables
# run: |
# requiredVars=("GITHUB_TOKEN" "OWNER" "REPO" "ENV_NAME" "BASE_REF" "HEAD_REF")
# for req in ${requiredVars[@]}; do
# if [ -z "${!req}" ]; then
# echo "Reqired variable '${req}' is blank or not defined"
# allSet=false
# fi
# done
# [[ "${allSet}" == false ]] && exit 1
- name: Get JIRA tickets
id: get-jira-tickets
run: |
NUMCOMMITS=$(curl --silent -H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
https://api.github.com/repos/${OWNER}/${REPO}/compare/${BASE_REF}...${HEAD_REF} |
jq '.commits | length')
echo "Number of commits: ${NUMCOMMITS}"
if [ ${NUMCOMMITS} -eq 0 ]; then
echo ""
exit 0
fi
JSON_PAYLOAD=$(jq -cn '
{
query: $query,
variables: {
"repoOwner": $repoOwnerVar,
"repoName": $repoNameVar,
"numCommits": $numCommitsVar,
"headRef": $headRefVar
}
}' \
--arg query '
query ($repoOwner: String!, $repoName: String!, $headRef: String!, $numCommits: Int!) {
repository(name: $repoName, owner: $repoOwner) {
commit: object(expression: $headRef) {
... on Commit {
history(first: $numCommits) {
pageInfo {
hasNextPage
startCursor
endCursor
}
nodes {
associatedPullRequests(first: 100) {
nodes {
title
body
}
}
}
}
}
}
}
}' \
--arg repoOwnerVar "$OWNER" \
--arg repoNameVar "$REPO" \
--argjson numCommitsVar ${NUMCOMMITS} \
--arg headRefVar "$HEAD_REF"
)
RESPONSE=$(curl --silent -H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "Content-Type: application/json" \
https://api.github.com/graphql \
-d "$JSON_PAYLOAD"
)
JIRA_KEYS=$(echo "$RESPONSE" | jq -c '[ .data.repository.commit.history.nodes[].associatedPullRequests.nodes[] | [.title, .body] ]?
| flatten
| map(capture("^\\[(?<jirakey>[A-Z]{2,}-[0-9]+)\\]"))
| map(.jirakey)
| unique
| join(",")' | tr -d '"'
)
echo "JIRA_KEYS=$JIRA_KEYS" >> $GITHUB_ENV
if [ -n "$JIRA_KEYS" ]; then
echo "Found JIRA tickets: $JIRA_KEYS"
else
echo "No JIRA tickets found, skipping deployment reporting"
fi
- name: Send Deployment Info
if: ${{ env.JIRA_KEYS != '' }}
uses: ./.github/actions/send-deployment-info
# run: node ./.github/actions/send-deployment-info.js
with:
display-name: "Deployment #${{ github.run_number }} from branch: ${{ github.head_ref }}"
description: "Deployment ${{ github.workflow }} (Run: ${{ github.run_number }}) for env: '${{ env.ENV_NAME }}' from branch: ${{ github.head_ref }}"
environment: ${{ env.ENV_NAME }}
jira-keys: ${{ env.JIRA_KEYS }}
label: "deployment-${{ env.ENV_NAME }}"
state: success