Ravindra BagaleCourses & study guides मराठी

DevOps

Chapter 9: GitHub Actions Hands-On

Friends, today we learn GitHub Actions — every push is an automatic inspection. Workflow YAML, events (push, pull_request), jobs, steps, runners, cache, artifacts, image push (GHCR/Docker Hub). Do not fear: one small workflow today.

What you will learn in this chapter

  • Workflow YAML and trigger events
  • Jobs, steps and runners
  • Caching and artifacts
  • Build and push images with secrets
  • Sketch an SSH deploy step safely

Concepts in this chapter

  1. 9.1Why Actions — checks on every push
  2. 9.2Minimal workflow — test on pull request
  3. 9.3Jobs, steps, needs and permissions
  4. 9.4Caching and artifacts
  5. 9.5Build and push an image (GHCR sketch)
  6. 9.6Secrets and OIDC (awareness)
  7. 9.7Deploy sketch — SSH to one lab VM
  8. 9.8Branch protection and required checks
  9. 9.9Troubleshooting table
  10. 9.10Environment-specific configs in CI
  11. 9.11Lab — PR tests and main image push

Review and practice

  1. ★9.5b Local act / dry thinking before push
  2. ★9.7b Matrix and reuse (awareness only)
  3. ★9.7c Reading a failed Actions log like a pro
  4. ★9.9b Pinning action versions

The chapter recap is at the end of the last concept page.

Practice Tasks

About this scenario

Suppose we are on the DevOps team at Maruti Suzuki, the automobile company, working from Gurugram. Deepak Chauhan leads the team, and we look after the service booking portal. Costs are counted in INR. In the tasks below we do real DevOps jobs on this project using what this lesson teaches. Maruti Suzuki is a real company, but this project, the people, servers, IP addresses and numbers are made up for practice.

0 / 12

Easy GitHub Actions: test on every push

Why: Running tests on every push catches bugs before they reach main.

What to do: Write .github/workflows/ci.yml named CI that runs on push. One job test on ubuntu-latest with steps: actions/checkout@v4, actions/setup-node@v4 with node-version 20, run npm ci, run npm test.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Use "- " for list items and fill each value from the task.
  4. Check the colons and spacing, then click Check answer.

Medium Run only for main and pull requests

Why: Limiting triggers saves CI minutes and noise.

What to do: Write a workflow named Build whose on: runs for push to branch main and for pull_request to branch main. One job build on ubuntu-latest with a single step run: make build.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Use "- " for list items and fill each value from the task.
  4. Check the colons and spacing, then click Check answer.

Medium Nightly scheduled workflow

Why: Nightly runs catch slow-breaking things like expired certificates.

What to do: Workflow that runs on schedule with cron "0 1 * * *". Job scan on ubuntu-latest, step run: ./scan.sh.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Use "- " for list items and fill each value from the task.
  4. Check the colons and spacing, then click Check answer.

Medium Use a secret as an env variable

Why: Secrets are masked in logs and never stored in the repo.

What to do: Workflow on workflow_dispatch. Job notify on ubuntu-latest with one step named Notify that runs ./notify.sh and has env SLACK_URL set from secrets.SLACK_URL.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Use "- " for list items and fill each value from the task.
  4. Check the colons and spacing, then click Check answer.

Medium Deploy only after tests pass

Why: needs makes the deploy job wait for, and depend on, the test job.

What to do: Workflow on push with two jobs on ubuntu-latest: test (run: npm test) and deploy (run: ./deploy.sh ap-south-1) that needs test.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Use "- " for list items and fill each value from the task.
  4. Check the colons and spacing, then click Check answer.

Medium Run the build inside a Docker agent

Why: A Docker agent gives a clean, versioned toolchain for every build.

What to do: Declarative pipeline with agent docker { image 'maven:3.9-eclipse-temurin-21' } and stage Build running sh 'mvn -B verify'.

How:
  1. Start with pipeline { and close it with } at the end.
  2. Add agent, then stages { } with one stage('Name') { steps { } } per step.
  3. Put the commands inside steps using sh '...'.
  4. Count your { and } so every block is closed, then click Check answer.

Medium Delivery or deployment?

Why: The two words mean different levels of automation.

What to do: At Maruti Suzuki every change that passes tests goes to production automatically, with no human approval. Is this continuous delivery or continuous deployment? Type one word.

How:
  1. Write down the facts or numbers the task gives you.
  2. Work it out step by step.
  3. Type only the final answer (one word or number) in the box.
  4. Click Check answer.

Hard Matrix build on three Python versions

Why: A matrix tests every supported version in parallel.

What to do: Write a workflow (on: push) with job test on ubuntu-latest using strategy.matrix.python with "3.10", "3.11", "3.12". Steps: actions/checkout@v4, actions/setup-python@v5 with python-version ${{ matrix.python }}, run pytest.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Use "- " for list items and fill each value from the task.
  4. Check the colons and spacing, then click Check answer.

Hard Build and push an image from CI

Why: Every merge to main gives a fresh image in the registry.

What to do: Workflow on push to main. Job image on ubuntu-latest. Steps: actions/checkout@v4; docker/login-action@v3 with registry ghcr.io, username ${{ github.actor }}, password ${{ secrets.GITHUB_TOKEN }}; docker/build-push-action@v6 with push true and tags ghcr.io/maruti-service-booking:${{ github.sha }}.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Use "- " for list items and fill each value from the task.
  4. Check the colons and spacing, then click Check answer.

Hard Cache dependencies

Why: Caching npm downloads cuts minutes off every run.

What to do: In job build (ubuntu-latest, on push) add steps: actions/checkout@v4; actions/cache@v4 with path ~/.npm and key npm-${{ hashFiles('package-lock.json') }}; run npm ci.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Use "- " for list items and fill each value from the task.
  4. Check the colons and spacing, then click Check answer.

Hard Use Jenkins credentials safely

Why: withCredentials masks the password and removes it after the block.

What to do: Inside stage Push steps, use withCredentials with usernamePassword(credentialsId: 'reg-cred', usernameVariable: 'U', passwordVariable: 'P') and run sh 'echo $P | docker login -u $U --password-stdin registry.maruti.example.com'. Wrap it in a full declarative pipeline with agent any.

How:
  1. Start with pipeline { and close it with } at the end.
  2. Add agent, then stages { } with one stage('Name') { steps { } } per step.
  3. Put the commands inside steps using sh '...'.
  4. Count your { and } so every block is closed, then click Check answer.

Hard Parallel test stages

Why: Running unit and lint at the same time halves feedback time.

What to do: Declarative pipeline, agent any, stage Checks containing parallel with stage Unit (sh 'npm test') and stage Lint (sh 'npm run lint').

How:
  1. Start with pipeline { and close it with } at the end.
  2. Add agent, then stages { } with one stage('Name') { steps { } } per step.
  3. Put the commands inside steps using sh '...'.
  4. Count your { and } so every block is closed, then click Check answer.

Ravi Sir's Cards: 33 revision, interview and comparison cards for this chapter · मराठी cards