GitHub Actions — Cheat Sheet

Tags: github, actions, ci, cd, devops, yaml, workflows

Level: intermediate

TL;DR: Native CI/CD and automation platform integrated into GitHub. Workflows are defined in YAML within .github/workflows/ and execute on specific repository events.

Key concepts

Workflows are automated procedures added to a repository, composed of one or more jobs.

Events trigger workflows (e.g., push, pull_request, schedule, workflow_dispatch).

Jobs are sets of steps that execute on the same runner. Jobs run in parallel by default.

Steps are individual tasks that can run commands or actions.

Actions are reusable, standalone commands combined into steps to create a job.

Runners are the servers that execute the workflow jobs (GitHub-hosted or self-hosted).

Secrets are encrypted environment variables used to store sensitive data like API keys.

Contexts provide access to information about workflow runs, runner environments, and jobs.

Typical questions and ready answers

Q: How do you ensure one job runs only after another completes?R: I use the needs keyword in the dependent job's configuration to specify the ID of the required job.

Q: How can you share files or data between different jobs in the same workflow?R: I use actions/upload-artifact to save the data in the first job and actions/download-artifact to retrieve it in the subsequent job.

Q: How do you run tests across multiple Node.js versions simultaneously?R: I configure a matrix strategy in the job definition to run multiple variations of the same job automatically.

Pitfalls and common mistakes

Forgetting to include the actions/checkout step, resulting in a runner with an empty directory.

Not specifying exact version tags or SHAs for third-party actions, risking sudden breakages from unintended updates.

Assuming environment variables set in one step are automatically available in the next without writing to $GITHUB_ENV.

Leaving secrets exposed in workflow logs by accidentally echoing them in shell commands.

Snippets and commandsYAML

```name: CI

on:

push:

branches: [ "main" ]

pull_request:

branches: [ "main" ]

jobs:

build:

runs-on: ubuntu-latest

steps:

  • name: Checkout repository

uses: actions/checkout@v4

  • name: Setup Node.js

uses: actions/setup-node@v4

with:

node-version: '20'

  • name: Install dependencies

run: npm ci

  • name: Run tests

run: npm test

env:

API_KEY: ${{ secrets.API_KEY }}

```

To review

[ ] Passing inputs to reusable workflows (workflow_call).

[ ] Managing environment protection rules and approvals.

[ ] Optimizing cache configurations with actions/cache to speed up builds.

Jarvis Interviews
Jarvis