CheatSheet | Github Actions


Introduction

  • A continuous integration and continuous delivery (CI/CD) platform integrated directly into GitHub.
    • It enables the automation of software development workflows, including building, testing, and deploying code directly from a GitHub repository.
  • Key Concepts:
    • Workflows: These are automated processes defined in YAML files within the .github/workflows directory of a repository. Workflows consist of one or more jobs and are triggered by specific events, such as pushing code, creating a pull request, or on a schedule.
    • Events: These are specific activities within a repository that can trigger a workflow. Examples include push, pull_request, schedule, and workflow_dispatch.
    • Jobs: A job is a set of steps that execute on the same runner. Jobs can run in parallel or sequentially, depending on their dependencies.
    • Steps: A step is an individual task within a job, which can be a shell command or an “action.”
    • Actions: Actions are reusable units of code that perform specific tasks. They can be custom-built, found on the GitHub Marketplace, or provided by third-party services.
    • Runners: These are servers that execute the jobs in a workflow. GitHub provides hosted runners for various operating systems (Ubuntu, Windows, macOS), or users can configure self-hosted runners for more control over the execution environment.
    • Capabilities: GitHub Actions can be used for a wide range of automation tasks beyond CI/CD, including:
      • Automating code quality checks and static analysis.
      • Publishing packages to registries.
      • Managing issues and pull requests, such as adding labels or assigning reviewers.
      • Deploying applications to various cloud platforms.
      • Creating custom notifications or reports.


1. Quick Start

  • You can use github actions by simply placing .yaml files into the ./github/workflows directory
name: GitHub Actions Demo
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
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/checkout@v4
      - 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 }}."


2. Docker-image

  • on :
# GitHub displays the names of your workflows under your repository's "Actions" tab.
name: GitHub Actions Demo

# To automatically trigger a workflow, use `on` to define which events can cause the workflow to run. 
on:
  push:
    branches: ["main"]
    tags: ["v*"]  # trigger workflows when new tag is created (release)
  pull_request:
    branches: ["main"]  # new/following commits also trigger the workflow

jobs:
  build:
    runs-on:
      group: < ORGANIZATION_NAME >

    steps:
      - uses: actions/checkout@v3 # download src to CI server from remote repo

      - name: Build the docker image
        run: sh bin/run.sh build-image ${{ github.ref_name }}
        if: github.ref_type == 'tag' # run this step only if the workflow is triggered by tag