Skip to main content

  • July 12, 2021

Getting Started with GitHub Actions

Decorative Illustration Using GitHub Actions for your CI/CD (Continuous Integration / Continuous Delivery or Deployment) process offers some great features and solves a number of pain points, such as automated testing and sending notifications for successes/failures.

What are Github Actions?

GitHub Actions are bits of code that are executed when triggered by an event. You can automate portions of your workflow by using GitHub Actions from the marketplace or writing your own. Some examples below (Event -> Action):

  • Commit pushed to a specific branch -> run automated tests
  • Pull Request opened -> send notification to Slack
  • Pull Request merged -> deploy code
  • Release is tagged -> send Tweet

There are many integrations available including Docker, AWS, Azure, Google Cloud, Cloudflare, JIRA, Jenkins, Sentry, and Slack.

Getting Started

  1. Create a .yml file under .github/workflows/
  2. Decide what event will trigger this workflow
  3. Include environment variables or secrets* as needed
  4. Specify conditions to be met if applicable (such as a specific branch or commit message)
  5. Create a job and specify the runner environment. This is the machine that will actually run the code - you can use Linux, macOS, or Windows.
  6. List out the steps of the job and any third-party actions used

* GitHub secrets can be used for API tokens or other sensitive data that is needed to execute the action. They are available to use in the workflow, but are not visible to the repository or logs.

Using conditionals, you can run different code based on which branch triggered the action.

The following is a sample workflow from the GitHub Quickstart Guide:

name: GitHub Actions Demo
on: [push]
jobs:
  Explore-GitHub-Actions:
    runs-on: ubuntu-latest
    steps:
      - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
      - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
      - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
      - name: Check out repository code
        uses: actions/[email protected]
      - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
      - run: echo "🖥️ The workflow is now ready to test your code on the runner."
      - name: List files in the repository
        run: |
          ls ${{ github.workspace }}
      - run: echo "🍏 This job's status is ${{ job.status }}."

You can monitor the workflow’s progress via the logs in github actions and see any data that gets logged out during the workflow (such as the versions of node, yarn, ruby, etc. that are logged out in the workflow). This helps track down potential problems if the action fails as error messages will be logged.

(Source: GitHub Quickstart Guide)

In addition, you can add events/actions triggered by success/errors - if the build/deploy action fails for any reason, a “failure” message could be sent via Slack notification, otherwise, a “success” message is sent.

- name: Send failed notification to Slack
  uses: voxmedia/[email protected]
  if: failure()
  with:
    message_id: ${{ steps.slack.outputs.message_id }}
    channel: my-channel-app-notify
    status: FAILED
    color: danger
  env:
    SLACK_BOT_TOKEN: ${{ secrets.SLACK_ACTIONS_NOTIFIER_BOT_TOKEN }}

- name: Send success notification to Slack
  uses: voxmedia/[email protected]
  if: success()
  with:
    message_id: ${{ steps.slack.outputs.message_id }}
    channel: my-channel-app-notify
    status: SUCCESS
    color: good
  env:
    SLACK_BOT_TOKEN: ${{ secrets.SLACK_ACTIONS_NOTIFIER_BOT_TOKEN }}

Summary

  • GitHub actions are included in your GitHub account (note: these are not included in legacy accounts that are priced per-repository) and can be run on several different platforms such as Mac OS, Linux or Windows.
  • Many actions are available in the Marketplace that you can use in your workflow.
  • You can manage sensitive data like API keys or tokens using Github secrets.

Back to Top

comments powered by Disqus