Automate Jira Tasks with GitHub Actions
Building a GitHub Action that automatically creates and transitions Jira issues based on pull request events, improving developer workflow.
Overview
Do you use Jira as your issue tracker and want to ensure every GitHub PR contains a linked Jira issue key? This GitHub Action validates that requirement automatically.
The "Jira Issue Key Checker" action verifies that each pull request includes a Jira issue key matching a specified prefix in both the title and description. Complete source code is available at github.com/fgiuliani/jira-issue-key-checker.
Init Project and Install Packages
Begin with Node.js by running npm init to create a package.json file. Install required dependencies:
npm install @actions/core
npm install @actions/github
The @actions/core package interfaces with workflow commands and variables, while @actions/github provides access to GitHub Actions contexts.
Create an Action Metadata File
Create action.yml to define inputs and configuration:
name: "Jira Issue Key Checker"
description: "Checks If a PR contains its linked Jira issue key in both the title and the description."
on: [pull_request]
inputs:
GITHUB_TOKEN:
required: true
jira-prefix:
required: true
description: "Jira issue key prefix. Ex. ABC-1111, prefix would be ABC"
runs:
using: "node12"
main: "index.js"
Write the Action Code
Create index.js containing the validation logic:
const core = require("@actions/core");
const github = require("@actions/github");
const jiraPrefix = core.getInput("jira-prefix");
async function run() {
try {
const prTitle = github.context.payload.pull_request.title;
const prBody = github.context.payload.pull_request.body;
let regex = new RegExp(`${jiraPrefix}-[0-9]+`);
if (!regex.test(prTitle) || !regex.test(prBody)) {
core.setFailed("Jira Issue Key missing in PR title or description.");
return;
}
} catch (error) {
core.info(error);
}
}
run();
Key Implementation Details
- GitHub Actions provide context via the
githubpackage for webhook event information core.getInput()retrieves the configured issue prefixgithub.context.payload.pull_requestexposes PR metadatacore.setFailed()marks validation failures when the regex doesn't match PR title or body
Commit, Tag, and Push
Prepare your action for release:
git add .
git commit -m "My GitHub Action"
git tag -a -m "My GitHub Action" v1
git push --follow-tags
Include the node_modules folder in your commit.
Test and Use the Action
In any GitHub repository, create .github/workflows/jira-issue-key-checker.yml:
name: "Jira Issue Key Checker"
on:
pull_request:
types:
- opened
- edited
- synchronize
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: [your-user]/[your-action-repository]@v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jira-prefix: "ABC"
Once added, the action automatically runs on pull request events. Monitor execution details in the repository's Actions tab.