Handy Github Actions

Handy Github Actions

Overview

In the CI/CD field, when GitHub throws its hat in the ring, you get GitHub Actions. A tool that runs within a repo on Github itself. You can set up YML files that run tasks based on specific triggers.

Its best part? Various Actions can be created and shared with others by community members. In this blog, we’ll highlight some custom ones that you’ll find helpful.

Note: You’ll need a basic understanding of CI to continue.

Jira Login

Jira Login is a custom Action from the creators of Jira, Atlassian, that allows you to use other Actions from Atlassian. Some of which you’ll see in this post.

- name: Login
      uses: atlassian/gajira-login@master
      env:
        # The following secrets have been setup on the Github repo
        JIRA_BASE_URL: ${{ secrets.JIRA_BASE_URL }}
        JIRA_USER_EMAIL: ${{ secrets.JIRA_USER_EMAIL }}
        JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }}

To get this Action up and running, you’ll need:

The base URL of your Jira board The email of the user (probably yours) An API token from Jira. There’s a guide by Atlassian on how to do this.

Once you provide these details, you’ll be able to log into Jira on your CI instance, allowing it to use other Jira Actions.

regex-action

regex-action is a general use Action that searches a string and returns subtext based on a Regex pattern. Things get interesting here. You can run a regex pattern against the branch name. Suppose you follow a naming convention where the branch name contains the Jira ticket it’s linked to; you can get the Jira ID.

- name: Extract Jira Id
      id: branch_name
      uses: AsasInnab/regex-action@v1
      with:
        regex_pattern: 'ABC-\d+'
        regex_flags: "gim"
        search_string: "${{ github.head_ref }}"

We’re filtering for a string that starts with ABC- followed by numbers of any length. The idea is, if you’re working on a branch like ABC-15-header, then this Action would return ABC-15, which is your Jira Issue Number!

You can just access this value in the following Actions and automate it. You’ll see what we mean in a bit.

Jira Issue Transition

https://github.com/atlassian/gajira-transition/raw/assets/example.gif?raw=true

Jira Issue Transition is a simple Action that transitions a Jira issue, so it’s in the right column on the Jira board.

Ticket management can sometimes be a pain, especially when moving between multiple tickets quickly. Automate the process, and you’ll have one less thing to worry about.

- name: Transition issue
      uses: atlassian/gajira-transition@master
      with:
        issue: "ABC-123"
        transition: "Code Review"

It’s easy to add this Action to a workflow. We call it uses and pass in two things:

  • The issue number of the ticket
  • The column you want it to move to

As with any CI/CD job or step, you can set conditions on when this should run so that it doesn’t fire every time you push.

If you used a naming convention that uses the Jira Issue Number in the branch name, you could use the regex-action Action to extract it, thus solving the hardcoded Jira Issue Number problem.

Jira Add Comment

Jira Add Comment is another handy Action.

Do you ever forget to write that standard comment on a Jira ticket that lets people know a routine task has occurred?

It could be something like, “I’ve pushed some code changes to XYZ repo. Please review.”

What if you could automate such messages?

- name: Comment on issue
    id: jira-comment
    uses: atlassian/gajira-comment@master
    with:
      issue: ${{ steps.step1.outputs.first_match }}
      comment: ${{ github.event.pusher.name }} pushed to repository: ${{ github.event.repository.full_name }}

All you need to provide is:

  • A Jira issue number
  • A string that’ll be posted as a comment

You can even leverage GitHub’s various contexts to derive information. Sadly, tagging other Jira users isn’t supported, and it’s not listed as an upcoming feature yet.

Hopefully, the following Action should solve that problem, depending on your setup.

Slack-Send

Slack Block.png

slack-send is a really cool find. It’s an Action that allows you to send messages to individuals and channels. You can even ping users by simply tagging them in your messages.

- name: Send custom JSON data to Slack workflow
        id: slack
        uses: slackapi/slack-github-action@v1.18.0
        with:
          # For posting a rich message using Block Kit
          payload: |
            {
              "text": "GitHub Action build result: ${{ job.status }}\n${{ github.event.pull_request.html_url || github.event.head_commit.url }}",
              "blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "GitHub Action build result: ${{ job.status }}\n${{ github.event.pull_request.html_url || github.event.head_commit.url }}"
                  }
                },
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "<@Uxxxxxxxxxx>, please review when free"
                  }
                },
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "${{ github.event.pull_request.user.login }} has pushed changes to <${{ github.event.pull_request.html_url }}|${{ github.event.pull_request.title }}>"
                  }
                }
              ]
            }
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
          SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK

This Action requires a payload. This can be a regular string, but it could also be a JSON. The format used here is from Slack’s Block Kit, which allows us to customize the sent messages.

Instead of plain text, you can make it a block with various sections for higher readability.

<@Uxxxxxxxxxx> is a user’s Slack Id. By passing their Id in, you can even tag users. And since there’s a similar Id for channels, you can even tag those.

The only caveat for this Action is:

It needs a paid Slack subscription It has a fair bit of setup before it can work

This setup definitely requires more than the other Actions on this post.

There are multiple ways to set up this Action. You can choose your preferred one through the guide on their page.

Conclusion

GitHub Actions has various custom actions created by the community, which can help you get started with CI/CD fairly quickly. Even companies have created Actions with functionality only they can provide.

You can choose to write Actions on your own, but why reinvent the wheel when there are multiple options.