Github Actions Guide : 5 Github Actions Hacks Every Developer Should Master


GitHub Actions have developed as a vital tool for developers in recent times. These automated workflows offer time-saving benefits and streamline the development process. Nonetheless, maximizing their effectiveness demands advanced expertise. Here, we explore 5 productivity hacks for GitHub Actions, aiming to simplify the lives of developers and eliminate unnecessary hassles. 

What is Github Actions?

GitHub Actions, a powerful automation tool offered by GitHub, allows users to create personalized workflows that automate various processes across the software development cycle. It allows you to effortlessly build, test, and deploy your code right within your GitHub repository, improving the efficiency of your development workflow.

Key GitHub Actions Components

Workflows

A workflow represents a customizable automated procedure designed to execute one or more tasks. These workflows are specified through YAML files stored in your repository and are activated either by repository events, manual triggers, or predefined schedules.



Stored within the .github/workflows directory, a repository can contain multiple workflows, each tailored to perform distinct operations. For instance, one workflow might focus on building and testing pull requests, while another could handle deployment tasks triggered by new releases. Additionally, you might have a separate workflow responsible for labeling newly opened issues.

Events

An event within a repository denotes a particular action that initiates a workflow run. These actions include events such as creating a pull request, opening an issue, or pushing a commit to a repository, often originating from GitHub. Additionally, workflows can be triggered based on scheduled intervals, through REST API requests, or manually initiated.


Source

Jobs

A job is a sequence of stages in a workflow that runs on the same runner. Each step either executes a shell script or performs an action. Steps are performed sequentially and are interdependent.




Actions

Within the GitHub Actions platform, an 'action' refers to a bespoke application designed to execute intricate, yet frequently recurring tasks. Leveraging actions aids in minimizing the redundancy within your workflow files by handling tasks such as pulling your Git repository from GitHub, configuring the appropriate toolchain for your development environment, or establishing authentication with your cloud provider.




Runners

A runner serves as the executing server for your workflows upon triggering. Each runner is capable of handling one job at a time. GitHub offers Ubuntu Linux, Microsoft Windows, and macOS runners for executing your workflows. Each workflow run occurs within a clean, newly-provisioned virtual machine.





5 GitHub Actions Hacks Every Developer Should Master

Actions secrets and variables

jobs:
 - name: Set up environment
  env:
    API_KEY: ${{ secrets.API_KEY }}

Secrets are encrypted variables for storing sensitive data in workflows, such as passwords and API keys. Create them in your repository settings under "Secrets." Use them in workflows by referencing them as environment variables.




Consider this straightforward illustration. 

Suppose you manage a Java project requiring third-party service authentication. While automating build and deployment via GitHub Actions, safeguarding your API key is crucial.

Here's the process:

Establish a secret: Navigate to repository settings and generate a new secret labeled "API_KEY," assigning it your API key.

Adapt your workflow: Within the workflow file, integrate a step configuring the "API_KEY" secret as an environment variable. For instance:

Using a Matrix for Your Jobs

jobs:
  example_matrix:
    strategy:
      matrix:
        version: [10, 12, 14]
        os: [ubuntu-latest, windows-latest]

To ensure code compatibility across various platforms, architectures, and versions, employing matrix jobs in your GitHub Actions workflow is a recommended approach.

Matrix jobs facilitate executing identical tasks across multiple configurations. For instance, you may wish to test your code on diverse Java versions or operating systems. Rather than crafting distinct jobs for each setup, you define the desired variables for testing via a matrix.

To incorporate matrix jobs into your GitHub Actions workflow, define a matrix in your workflow file using the matrix keyword, followed by a set of variables.

Create Docker Container Action

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

Docker containers offer a solution to package your code and dependencies into a portable, consistent environment. This ensures uniform execution of your code across various platforms, mitigating compatibility concerns and maintaining test consistency.

Incorporating Docker containers into your GitHub Actions workflow involves defining a Dockerfile, detailing the desired environment setup, including base image selection, dependency installation, and configuration adjustments.

To integrate the Docker image into your GitHub Actions workflow, use the container keyword within your job definition.

Github Custom Action

# ref: https://docs.github.com/en/actions/creating-actions/creating-a-docker-container-action
name: 'Hello World'
description: 'Greet someone and record the time'
inputs:
  who-to-greet:  # id of input
    description: 'Who to greet'
    required: true
    default: 'World'
outputs:
  time: # id of output
    description: 'The time we greeted you'
runs:
  using: 'docker'
  image: 'Dockerfile'
  args:
    - ${{ inputs.who-to-greet }}


Actions can be done by developing custom code to interact with your repository, enabling versatile integration with GitHub's APIs and publicly accessible third-party APIs. These actions can range from publishing npm modules to dispatching SMS alerts for critical issues or deploying production-grade code.

Whether for personal workflow enhancement or contribution to the GitHub community, you have the flexibility to create and share actions. However, to make your actions accessible to all, your repository must be public.

Actions are executable either directly on a machine or within a Docker container. They can be configured with specified inputs, outputs, and environment variables to suit diverse operational requirements.

GitHub Workflow Artifacts

- name: 'Upload Artifact'
    uses: actions/upload-artifact@v4
    with:
      name: my-artifact
      path: my_file.txt
      retention-days: 5


During workflow execution, saving outputs like binaries or test results for future reference is often necessary. GitHub Actions provides a solution through artifacts.

Artifacts encompass files or directories you wish to retain post-workflow completion. Leveraging the actions/upload-artifact action, you can upload these artifacts to the GitHub server and subsequently retrieve them as needed.

Schedule Jobs using Github Actions

name: GitHubActions Job Schedule

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
  schedule: ## Schedule the job to run at 12.am daily.
    - cron: '12 16 * 10 7'


Scheduled jobs represent a potent capability within GitHub Actions, enabling the automation of routine tasks such as database updates or report generation. By utilizing the scheduled event, workflows can be triggered at predetermined intervals, whether daily at a set time or weekly.

To incorporate scheduled jobs into your workflow, include the scheduled event within your workflow file definition.

Save your development time with these easy github actions tips and tricks.

Happy Coding✌

codegirl

Hello, I’m Sangita, person behind "codegirl", a dedicated web developer. Crafting digital experiences is not just my job; it’s my passion. Let’s build something exceptional together!

Post a Comment

Previous Post Next Post