7 GitHub Actions Tips To Boost Productivity

GitHub actions have become an essential tool for many developers in recent years. These automated workflows can help you save time and streamline your development process. However, using them effectively requires some advanced knowledge and skill. In this post, we'll be discussing 7 productivity hacks for GitHub actions, so your life as a developers get easier, and without hassles.

7 GitHub Actions Hacks Every Developer Should know 

Here are the 7 GitHub action tricks that we recommend for improving your development process:

1. Use Secrets for Sensitive Information

Secrets are encrypted environment variables that you can use to securely store sensitive data in your workflows. This can include passwords, API keys, and other sensitive information that you don't want the public or other project collaborators to see.

Secrets must be created in your repository settings before they can be used in your workflow. You can do this by going to your GitHub repository, clicking on the "Settings" tab, and then selecting "Secrets" from the sidebar. You can then create a new secret and give it a name and value.

You can use a secret in your workflow by referencing it as an environment variable after you've created it. For instance, you could have a secret called "API KEY" that contains an API key for an external service. This secret could then be used in your workflow by referencing it as:

  $ {{ secrets.API_KEY }}

Let's look at a simple example to see how this works. Assume you have a Java project that needs to be authenticated with a third-party service. You could automate the build and deployment process with GitHub Actions, but you don't want to expose your API key in plain text.

Here's how you can use secrets to securely store and use the API key:

  • Create a secret: In your repository settings, create a new secret called "API_KEY" and assign it the value of your API key.
  • Modify your workflow: In your workflow file, add a step that sets the "API_KEY" secret as an environment variable. For example: - name: Set up environment¬† env:¬† ¬† API_KEY: ${{ secrets.API_KEY }}
  • Use the secret: In a later step, you can use the "API_KEY" environment variable to authenticate with the third-party service. For example:
- name: Authenticate with service  
run:
curl -H
"Authorization:
Bearer $API_KEY"
https://api.example.com/

2. Use Matrix Jobs for Testing

When testing your code, you should ensure that it works across multiple platforms, architectures, and versions. One method is to use matrix jobs in your GitHub Actions workflow.

Matrix jobs let you run the same set of tasks in multiple configurations. For example, you might want to test your code on different Java versions or operating systems. Instead of creating separate jobs for each configuration, you can define the various variables you want to test against using a matrix.

You must define a matrix in your workflow file to use matrix jobs in your GitHub Actions workflow. This is accomplished by using the matrix keyword, followed by a set of variables.

For example:

strategy:  
matrix:   
os:
[ubuntu-latest, windows-latest] 
  java-version: [8, 11]

In this example, we define a matrix that tests our code on two different operating systems (Ubuntu and Windows) and two different versions of Java (8 and 11). We can then use this matrix to run a set of jobs that cover all the possible combinations.

3. Use Docker Containers for Consistent Environments

When it comes to building and testing software, it's important to have a consistent environment. One way to achieve this is by using Docker containers in your GitHub Actions workflow.

Docker containers allow you to package your code and its dependencies into a portable and consistent environment. This means that you can run your code in the same environment, regardless of where it's being executed. This can help you avoid compatibility issues and ensure that your tests are consistent across different platforms.

To use Docker containers in your GitHub Actions workflow, you need to define a Dockerfile that describes the environment you want to create. This can include the base image you want to use, any dependencies you need to install, and any other configuration you want to set up. 

To use the Docker image in our GitHub Actions workflow, we can use the container keyword in our job definition. For example:

jobs:  
build:   
runs-on:
ubuntu-latest   
container:   
  image: my-docker-image:latest   
steps:     
- name: Checkout code     
  uses: actions/checkout@v2   
  - name: Build code     
  run: make build

In this example, we define a job called "build" that runs on the latest version of Ubuntu, and uses our Docker image as the container. We then check out our code using the actions/checkout action and build it using the make build command.

4. Use Custom Actions for Reusability

GitHub Actions are a powerful way to automate your software development workflows, but you may find yourself repeating the same set of steps. You can create custom actions to avoid duplicating code and increase reusability.

Custom actions are reusable code blocks that can be used in a variety of workflows. Custom actions for tasks like building, testing, and deploying your code can be created and used in different workflows, making it easier to maintain and update your workflows.

To make a custom action, first define a set of steps that perform a specific task, then package those steps into an action. You can use any programming language or scripting language supported by GitHub Actions to define your action.

5. Use Artifacts for Storing Build Results

When you run a build or test in your workflow, you may want to save the output, such as binaries or test results, for later use or reference. Artifacts are a feature of GitHub Actions that allow you to store build outputs.

Artifacts are files or directories that you want to keep after a workflow is finished. You can upload your artifacts to the GitHub server using the actions/upload-artifact action, and then download them later.

6. Use Scheduled Jobs for Regular Tasks

Scheduled jobs are a powerful feature of GitHub Actions that allow you to schedule regular tasks like updating a database or generating a report. The scheduled event can be used to trigger your workflow at specific intervals, such as every day at a specific time or once a week.

To use scheduled jobs in your workflow, you need to define a workflow file that includes the scheduled event. For example:

name: Daily Report
on:
  schedule:
    - cron: '0 0 * * *'
jobs:
  generate-report:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Generate report
        run: python generate_report.py

In this example, we define a workflow that runs every day at midnight (0 0 * * *). The workflow consists of a single job that generates a report by running a Python script.

You can customize the cron expression to run your workflow at different intervals. For example, to run your workflow every Monday at 9 am, you can use 0 9 * * 1.

Scheduled jobs are an effective method for automating repetitive tasks and reducing the need for manual intervention. You can reduce the likelihood of human error by using scheduled jobs to ensure that your tasks are run at specific times and intervals.

7. Use Workflow Visualizations for Debugging

Workflow Visualizations is a powerful GitHub Actions feature that can help you understand and debug your workflows. When you define a workflow, you can view a graphical representation of it using the built-in visualization tool.

The workflow visualization displays all of the jobs in your workflow, along with the order in which they are executed. Each job is represented by a box, and the boxes are linked by arrows to show data flow and job dependencies.

Simply open your workflow file in the GitHub Actions editor and click the "Visualize" button to use the visualization tool. This will produce a graphical representation of your workflow, which you can use to debug and optimize it.

It should be noted that the visualization tool is not a substitute for proper testing and debugging. While the tool can assist you in identifying issues with your workflow, you should still perform thorough testing and debugging to ensure that it is working properly.

Conclusion

We hope these 7 GitHub action tricks help you improve your development process and make the most out of your workflows. By using these tips and tricks, you can save time, improve consistency, and ensure that your code is compatible with a wide range of environments.

To make the most of GitHub Actions, an engineering analytics tool is the need of the hour. Hatica offers metrics across 13 dashboards, powered by CI/CD tools, Jira, Asana, PagerDuty, OpsGenie, and GitHub. By collating tool activities at one place, Hatica helps teams streamline their workflow, cut through the clutter of unwanted alerts, and improve productivity.

Subscribe to the Hatica blog today to read more about unblocking developers, and boosting productivity with engineering analytics.

Subscribe to Hatica's blog

Get bi-weekly insights straight to your inbox

Share this article:
Table of Contents
  • 7 GitHub Actions Hacks Every Developer Should know¬†
  • 1. Use Secrets for Sensitive Information
  • 2. Use Matrix Jobs for Testing
  • 3. Use Docker Containers for Consistent Environments
  • 4. Use Custom Actions for Reusability
  • 5. Use Artifacts for Storing Build Results
  • 6. Use Scheduled Jobs for Regular Tasks
  • 7. Use Workflow Visualizations for Debugging
  • Conclusion