Skip to content

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

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

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

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 }}
CLIENT_ID: ${{ secrets.CLIENT_ID }}
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
JIRA_BASE_URL: ${{ secrets.CLOUD_INSTANCE_BASE_URL }}
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" "CLIENT_ID" "CLIENT_SECRET" "JIRA_BASE_URL" "OWNER" "REPO" "ENV_NAME" "BASE_REF" "HEAD_REF")
for req in "${requiredVars[@]}"; do
var_value=$(printenv $req)
if [ -z "$var_value" ]; then
echo "Required variable '${req}' is blank or not defined"
allSet=false
fi
done
if [ "$allSet" == "false" ]; then
exit 1
fi
- 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 -r '[ .data.repository.commit.history.nodes[].associatedPullRequests.nodes[] | [.title, .body] | join(" ") | gsub("\n"; " ") | split(" ") | .[] | capture("\\[(?<jirakey>[A-Z]{2,}-[0-9]+)\\]") | .jirakey ] | unique | join(",")')
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: Convert your JIRA base URL into a Cloud ID
id: get-cloud-id
if: ${{ env.JIRA_KEYS != '' }}
run: |
CLOUD_ID=$(\
curl "${JIRA_BASE_URL}/_edge/tenant_info" | \
jq --raw-output '.cloudId'
)
# Check if CLOUD_ID is empty
if [ -z "$CLOUD_ID" ]; then
echo "Error: CLOUD_ID is empty"
exit 1
fi
echo "CLOUD_ID=$CLOUD_ID" >> $GITHUB_OUTPUT
# - name: Fetch Data
# run: |
# AUTH=$(echo -n ${CLIENT_ID}:${CLIENT_SECRET} | base64)
# RESPONSE=$(\
# curl --silent \
# --request GET \
# --url "${JIRA_BASE_URL}/rest/agile/1.0/issue/FX-4058" \
# --header "Authorization: Basic ${AUTH}" \
# --header "Content-Type: application/json" \
# )
# echo "$RESPONSE"
# - name: Push Deployment Info to Jira
# if: ${{ env.JIRA_KEYS != '' && success() }}
# id: push_deployment_info_to_jira
# uses: HighwayThree/jira-upload-deployment-info@master
# with:
# cloud-instance-base-url: '${{ secrets.CLOUD_INSTANCE_BASE_URL }}'
# client-id: '${{ secrets.CLIENT_ID }}'
# client-secret: '${{ secrets.CLIENT_SECRET }}'
# deployment-sequence-number: '${{ github.run_id }}'
# update-sequence-number: '${{ github.run_id }}'
# issue-keys: "${{ env.JIRA_KEYS }}"
# display-name: "Deploy to ${{ env.ENV_NAME }}"
# # url: "${{ github.event.repository.url }}/actions/runs/${{ github.run_id }}"
# # description: "Prod Deployment"
# # last-updated: '${{ github.event.head_commit.timestamp }}'
# # label: 'Prod Deployment Label'
# state: 'successful'
# # pipeline-id: '${{ github.repository }} ${{ github.workflow }}'
# # pipeline-display-name: 'Workflow: ${{ github.workflow }} (#${{ github.run_number }})'
# # pipeline-url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
# environment-id: 'Temploy'
# environment-display-name: 'Temploy'
# environment-type: 'temploy'
# - name: Get Access Token
# id: get-access-token
# if: ${{ env.JIRA_KEYS != '' && success() }}
# run: |
# ACCESS_TOKEN=$(\
# curl --silent \
# --request POST \
# --url "https://api.atlassian.com/oauth/token" \
# --header "Content-Type: application/json" \
# --data '{
# "grant_type": "client_credentials",
# "client_id": "${CLIENT_ID}",
# "client_secret": "${CLIENT_SECRET}",
# "audience": "api.atlassian.com"
# }'
# )
# # | \
# # jq --raw-output '.access_token'
# echo "$ACCESS_TOKEN"
# # Check if ACCESS_TOKEN is empty
# if [ -z "$ACCESS_TOKEN" ]; then
# echo "Error: ACCESS_TOKEN is empty or null"
# exit 1
# fi
# echo "ACCESS_TOKEN=$ACCESS_TOKEN" >> $GITHUB_OUTPUT
- name: Send request to Create new JIRA deployment
if: ${{ env.JIRA_KEYS != '' && success() }}
run: |
JIRA_KEYS_QUOTED=$(echo "$JIRA_KEYS" | sed "s/,/\",\"/g")
JIRA_KEYS_QUOTED="\"$JIRA_KEYS_QUOTED\""
AUTH=$(echo -n ${CLIENT_ID}:${CLIENT_SECRET} | base64)
RESPONSE=$(curl \
--request POST \
--url "https://api.atlassian.com/jira/deployments/0.1/cloud/${{ steps.get-cloud-id.outputs.CLOUD_ID }}/bulk" \
--header "Authorization: Basic ${AUTH}" \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--http1.1 \
--data '{
"deployments": [
{
"deploymentSequenceNumber": "${{ github.run_number }}",
"updateSequenceNumber": "${{ github.run_number }}",
"displayName": "Deployment #${{ github.run_number }} from branch: ${{ github.head_ref }}",
"url": "http://picasso.toptal.net/${{ github.head_ref }}",
"description": "Deployment ${{ github.workflow }} (Run: ${{ github.run_number }}) for env: '${{ env.ENV_NAME }}' from branch: ${{ github.head_ref }}",
"lastUpdated": "${{ github.event.pull_request.updated_at || github.event.head_commit.timestamp }}",
"label": "Deployment",
"state": "successful",
"pipeline": {
"id": "${{ github.repository }}-${{ github.workflow }}",
"displayName": "${{ github.repository }}-${{ github.workflow }}",
"url": "${{ github.event.repository.url }}/actions/runs/${{ github.run_id }}"
},
"environment": {
"id": "${{ github.repository }}-${{ env.ENV_NAME }}",
"displayName": "${{ github.repository }}-${{ env.ENV_NAME }}",
"type": "${{ env.ENV_NAME }}"
},
"association": {
"type": "issueIdOrKeys",
"values": [
${JIRA_KEYS_QUOTED}
]
}
}
]
}'
)
# | \
# jq --raw-output '.'
echo "$RESPONSE"
# - name: Send Deployment Info
# if: ${{ env.JIRA_KEYS != '' }}
# uses: ./.github/actions/send-deployment-info
# # run: node ./.github/actions/send-deployment-info.js
# with:
# client-id: ${{ secrets.CLIENT_ID }}
# client-secret: ${{ secrets.CLIENT_SECRET }}
# cloud-instance-base-url: ${{ secrets.CLOUD_INSTANCE_BASE_URL }}
# 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