Abhishek Jain
Abhishek Jain

Reputation: 195

How to create Jira task when github action get failed in github

I am new to github actions and planning to implement something like whenever someone pushes the code to github, github actions should start running and if any of action fails, github should raise Jira request to Jira tool. Can anybody suggest How can I implement this.

I found one link on google but its not helping much. url given here : https://github.com/marketplace/actions/jira-create-issue

Upvotes: 0

Views: 1359

Answers (1)

Marcin Kłopotek
Marcin Kłopotek

Reputation: 5931

Let's make some introduction first to have a common understanding of the terms used below describing the solution. If you look at the documentation. Each GitHub actions workflow consists of one or more jobs. Jobs, in turn, have a sequence of steps executed in the following order. Each step can be executed on certain conditions. By default, all steps within a particular job are executed one after another unless the previous step failed. Once the previous step failed then all the following job steps are skipped by default and the job is marked as failed. We can change the defaults for the given step, however. We can tell the step to execute only if the job run failed (one of its steps failed). Taking the above, we can use gajira-login action followed by the mentioned gajira-create action step to execute itself when the workflow job failed. This way we'll get the goal of creating a Jira issue on failed workflow job run. We can execute the step on failing one of the previous steps by making the step depend on a failure() function. The function:

Returns true when any previous step of a job fails.

Connecting all the dots together we can write an example workflow:

jobs:
  a-failing-job:
    runs-on: ubuntu-latest
    steps:
    - name: Failing step
      run: exit 1
    - name: Login to Jira
      uses: atlassian/gajira-login@master
      if: failure()
      env:
        JIRA_BASE_URL: ${{ secrets.JIRA_BASE_URL }}
        JIRA_USER_EMAIL: ${{ secrets.JIRA_USER_EMAIL }}
        JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }}
    - name: Create Jira issue on job failure
      uses: atlassian/gajira-create@master
      if: failure()
      with:
        project: GA
        issuetype: Build
        summary: Build failed for ${{ github.repository }}

Please note that the gajira actions supports only Jira in the cloud and do not support self-hosted Jira instances.

Upvotes: 2

Related Questions