Webux Lab

By Studio Webux

Github Deployment integrated with Jira using Codebuild/Codepipeline

TG
Tommy Gingras Studio Webux 2022-02-08

Connect Jira with Github deployment using CodeBuild and Codepipeline

This article will cover only the github API and Codebuild.

It requires you to configure the Jira connect with github in order to have the full featureset.


Prerequisites

In order to use the Github API you need to generate a Github token,

you can do that here: SettingsDeveloper Settings > Personal access tokens (https://github.com/settings/tokens) The only permission required is : repo_deployment


Introduction

Here are shown 2 api calls that can simply be integrated in a codebuild project to update the github status for a specific environment and deployment.


Setup

How to use the API to create the different environment

The following command will create an environment within your repository, this environment will hold the status and Jira will be synced with it.

using bash,

username=""
token=""
organisation=""
repository=""
ref="main"
stage="dev"
production_environment="false" # should be true when set to production stage

curl \
  -f -X POST \
  -u ${username}:${token} \
  -H "Accept: application/vnd.github.v3+json" \
  https://api.github.com/repos/${organisation}/${repository}/deployments \
  -d '{"ref":"'${ref}'","environment":"'${stage}'","required_contexts":[],"production_environment":'${production_environment}'}'

This command will return you an ID, this ID is important for the next step

How to change the deployment statuses for each environment

Using the ID received before,

The valid statuses to use are : [error, failure, inactive, in_progress, queued, pending, or success]

username=""
token=""
organisation=""
repository=""
deployment_id=""
state="in_progress"
environment_url="https://dev.mydomain.com"
log_url="https://{region}.console.aws.amazon.com/codesuite/codebuild/{id}}/projects/{project_name}/build/{project_name}%3A{UUID}/?region={region}#"

curl \
  -f -X POST \
  -u ${username}:${token} \
  -H "Accept: application/vnd.github.v3+json" \
  https://api.github.com/repos/${organisation}/${repository}/deployments/${deployment_id}/statuses \
  -d '{"state":"'${state}'","environment":"'${stage}'","environment_url":"'${environment_url}'","log_url":"'${log_url}'"}'

The deployed link open the log_url & The View deployment button open the environment_url you’ve provided


Others

Delete a deployment completely

username=""
token=""
organisation=""
repository=""
deployment_id=""

curl \
  -f -X DELETE \
  -u ${username}:${token} \
  -H "Accept: application/vnd.github.v3+json" \
  https://api.github.com/repos/${organisation}/${repository}/deployments/${deployment_id}

Conclusion

These 2 api calls allow you to quickly and simply play with the deployment and sync the jira tickets with github and AWS without using external tools or solution.


Sources


Search