What You Need Before You Start

  • Turn on Actions in your GitHub repo settings

    TIP

    Go to Repo Settings > Action > General > Then pick ‘Allow enterprise, and select non-enterprise, actions and reusable workflows’

  • Get admin access to the repo so you can set up Secrets and Variables.

GitHub Actions: The Important Bits

  • Workflow file: This is the blueprint for your automation. It’s a YAML file that lives in the .github/workflows folder of your repo. It spells out what jobs and steps should run when your workflow kicks off.

  • Workflow Triggers: These are like the ignition switch for your workflow. They can be things like commits, pull requests, a set schedule, or even a manual button press. You control this with the on setting in your workflow.

  • Reusable Workflow: Think of this as a template workflow that you can use over and over in different parts of your repo or even across your whole GitHub org. To make a workflow reusable, you set workflow_call as the trigger type.

  • GitHub Environments: These are like different stages for your code (think dev, staging, prod). You can set them up in your repo to control what secrets and variables your workflows can use. > [!TIP] > To set up an environment, go to Repo Settings > Environments > New Environment

  • GitHub Secrets/Variables:

    • Variables: These are for non-secret stuff that you want to use in your workflows.
    • Secrets: This is where you put the sensitive stuff like tokens and passwords. You can’t change secrets or variables inside the workflow itself. To use them, you write things like secrets.SECRET_NAME or vars.VARIABLE_NAME.
    • Environment Secrets/Variables: These are tied to a specific environment. This way, you can use the same name for different values in different environments.
    • Repo Secrets/Variables: These are available to every workflow in your repo.

      TIP

      To set these up, go to Settings > Secrets and Variables > Actions

  • env keyword: Use this to set environment variables right in your workflow file. You can set them for the whole workflow, a specific job, or even just one step. You can override them anywhere in the workflow and use them by writing env.VARIABLE_NAME.

  • Workflow Secrets: These are secrets that reusable workflows need. GitHub hides them when printing stuff out.

  • Workflow Inputs: These are like settings you can tweak when you run the workflow. They can be strings or choices. You can use them in reusable workflows or workflows you trigger manually. You set them up in the on part of your workflow.

  • Workflow Steps: These are the individual tasks in your workflow job.

    • You can use a reusable workflow in a step with the uses keyword.
    • If you need to pass info to the reusable workflow, use the with keyword.
    • For any secrets the reusable workflow needs, use the secrets keyword.
  • Workflow Job: This is a set of steps that all run on one machine (or node) and in one GitHub environment. You pick which machine to use with the runs-on setting. If you want to use variables from different environments in one workflow, you’ll need to set up multiple jobs.

  • run keyword: This is how you run shell commands in your workflow. It’s kind of like sh in Jenkins pipelines.

A Few More Handy Tips

  • Use Conditional Execution: You can make your workflows smarter by using if statements. This lets you run steps or jobs only when certain conditions are met. For example:
  steps:
    - name: Run only on main branch
      if: github.ref == 'refs/heads/main'
      run: echo "This step only runs on the main branch"
  • Take Advantage of Matrix Builds: If you need to test across multiple versions or configurations, matrix builds are your friend. They let you run the same job with different variables:

    strategy:
      matrix:
        node-version: [12.x, 14.x, 16.x]
    steps:
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}
    
  • Set Up Status Badges: These little badges in your README show the current status of your workflows. They’re great for giving a quick visual cue about your project’s health:

![CI](https://github.com/<username>/<repo>/actions/workflows/<workflow_file>/badge.svg)
  • Use GitHub-hosted Runners Wisely: Remember, you get a limited number of free minutes with GitHub-hosted runners. For long-running jobs or specialized environments, consider setting up self-hosted runners.

  • Leverage Composite Actions: If you find yourself repeating the same steps across different workflows, consider creating a composite action. It’s like making your own reusable mini-workflow.