Ravindra BagaleCourses & study guides मराठी

Chapter 81: DevOps Projects Part 11: GitHub Actions GitLab CI Node Matrix Docker OIDC Terraform Runner Releases

81.3 Project 79: Docker build and push with Actions

Friends, Actions can build and push images too. Today we log in to a registry, build with docker/build-push-action, tag with the git SHA, and push only from main.

What you will build in this project

  1. Write a Dockerfile.
  2. Add build-push workflow.
  3. Login with a registry secret.
  4. Push SHA tags from main only.

1. Start from a real company story

At Razorpay in Bengaluru, a PR workflow pushed :latest and overwrote a release tag mid-day.

Checkout flakes followed. Rough cost: about ₹19 lakh in retries.

Mehul limited pushes to main and required SHA tags.

Today we push that way.

Dockerfileapploginregistry secretbuildSHA tagpushmain only
Picture 1: main push → Actions → build-push-action → registry :SHA

Ravindra Bagale's Tip

Students store registry passwords in the workflow file. Use Actions secrets. Keep that habit.

2. Start and prepare

Step 1: Why in Actions

Why: Same commit builds the image. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: echo why. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 1: a1

Type this:

echo 'Build the image from the same SHA that passed tests'

You will see:

Build the image from the same SHA that passed tests

What it means: Why.

Example 2: a2

Type this:

mkdir -p /tmp/gha79/app/.github/workflows && echo 'Today: Dockerfile, login, build-push, main only'

You will see:

Today: Dockerfile, login, build-push, main only

What it means: Scope.

Example 3: a3

Type this:

echo 'PR builds verify Dockerfile; main pushes'

You will see:

PR builds verify Dockerfile; main pushes

What it means: Split.

Step 2: Dockerfile

Why: Tiny app image. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: printf. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 4: b1

Type this:

printf '%s\n' 'FROM public.ecr.aws/docker/library/node:20-alpine' 'WORKDIR /app' 'COPY package.json ./' 'RUN npm install --omit=dev' 'COPY . .' 'CMD ["node", "server.js"]' > /tmp/gha79/app/Dockerfile && cat /tmp/gha79/app/Dockerfile

You will see:

FROM public.ecr.aws/docker/library/node:20-alpine

What it means: Dockerfile.

Example 5: b2

Type this:

printf '%s\n' 'console.log("shop api");' > /tmp/gha79/app/server.js && echo 'shop api' > /dev/null; cat /tmp/gha79/app/server.js

You will see:

console.log("shop api");

What it means: server.

Example 6: b3

Type this:

printf '%s\n' '{"name":"shop-api","version":"1.0.0"}' > /tmp/gha79/app/package.json && cat /tmp/gha79/app/package.json

You will see:

{"name":"shop-api","version":"1.0.0"}

What it means: pkg.

Step 3: Login step

Why: Registry auth. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: docker/login-action. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 7: c1

Type this:

printf '%s\n' '      - uses: docker/login-action@v3' '        with:' '          registry: registry.example.com' '          username: \${{ secrets.REGISTRY_USER }}' '          password: \${{ secrets.REGISTRY_TOKEN }}' > /tmp/gha79/LOGIN.txt && cat /tmp/gha79/LOGIN.txt

You will see:

- uses: docker/login-action@v3

What it means: Login.

Example 8: c2

Type this:

echo 'Store REGISTRY_USER and REGISTRY_TOKEN in Actions secrets'

You will see:

Store REGISTRY_USER and REGISTRY_TOKEN in Actions secrets

What it means: Secrets.

Example 9: c3

Type this:

echo 'For Amazon ECR prefer OIDC in project 80 instead of long keys'

You will see:

For Amazon ECR prefer OIDC in project 80 instead of long keys

What it means: Bridge.

3. Build the main pieces

Step 4: build-push

Why: Main workflow. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: printf workflow. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 10: d1

Type this:

printf '%s\n' 'name: image' 'on:' '  push:' '    branches: [ main ]' '  pull_request:' '    branches: [ main ]' 'jobs:' '  docker:' '    runs-on: ubuntu-latest' '    steps:' '      - uses: actions/checkout@v4' '      - uses: docker/setup-buildx-action@v3' '      - name: Login' '        if: github.event_name == \'push\'' '        uses: docker/login-action@v3' '        with:' '          registry: registry.example.com' '          username: \${{ secrets.REGISTRY_USER }}' '          password: \${{ secrets.REGISTRY_TOKEN }}' '      - uses: docker/build-push-action@v6' '        with:' '          context: .' '          push: \${{ github.event_name == \'push\' }}' '          tags: registry.example.com/shop/api:\${{ github.sha }}' > /tmp/gha79/app/.github/workflows/image.yml && grep -E 'push:|tags:' /tmp/gha79/app/.github/workflows/image.yml

You will see:

push: ${{ github.event_name == 'push' }}\n          tags: registry.example.com/shop/api:${{ github.sha }}

What it means: Push+tag.

Example 11: d2

Type this:

echo 'github.sha is the full commit — unique per build'

You will see:

github.sha is the full commit — unique per build

What it means: SHA.

Example 12: d3

Type this:

echo 'push false on pull_request builds the image without uploading'

You will see:

push false on pull_request builds the image without uploading

What it means: PR.

Step 5: Short SHA tip

Why: Readable tags. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: optional. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 13: e1

Type this:

echo 'tags: registry.example.com/shop/api:\${{ github.sha }}'

You will see:

tags: registry.example.com/shop/api:${{ github.sha }}

What it means: Full.

Example 14: e2

Type this:

echo 'Or compute short SHA in a prior step and use that tag'

You will see:

Or compute short SHA in a prior step and use that tag

What it means: Short.

Example 15: e3

Type this:

echo 'Never retag latest for production deploys'

You will see:

Never retag latest for production deploys

What it means: latest.

Step 6: Cache layers

Why: Speed builds. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: gha cache. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 16: f1

Type this:

printf '%s\n' 'cache-from: type=gha' 'cache-to: type=gha,mode=max' > /tmp/gha79/CACHE.txt && cat /tmp/gha79/CACHE.txt

You will see:

cache-from: type=gha

What it means: Cache.

Example 17: f2

Type this:

echo 'Add those under build-push-action with:'

You will see:

Add those under build-push-action with:

What it means: Where.

Example 18: f3

Type this:

echo 'First build is slow; later builds reuse layers'

You will see:

First build is slow; later builds reuse layers

What it means: Expect.

4. Automate and verify

Step 7: Permissions

Why: packages write. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: GITHUB_TOKEN tip. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 19: g1

Type this:

printf '%s\n' 'permissions:' '  contents: read' '  packages: write' > /tmp/gha79/PERMS.txt && cat /tmp/gha79/PERMS.txt

You will see:

packages: write

What it means: Perms.

Example 20: g2

Type this:

echo 'Least privilege: only the jobs that push need packages write'

You will see:

Least privilege: only the jobs that push need packages write

What it means: Least.

Example 21: g3

Type this:

echo 'External registries use your own secrets instead'

You will see:

External registries use your own secrets instead

What it means: External.

Step 8: Verify

Why: Pull the SHA. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: proof. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 22: h1

Type this:

echo 'docker pull registry.example.com/shop/api:$GITHUB_SHA'

You will see:

docker pull registry.example.com/shop/api:$GITHUB_SHA

What it means: Pull.

Example 23: h2

Type this:

echo 'Confirm the digest matches the Actions log'

You will see:

Confirm the digest matches the Actions log

What it means: Digest.

Example 24: h3

Type this:

printf '%s\n' 'Acceptance: main push creates a SHA tag; PR does not' > /tmp/gha79/ACCEPT.txt && cat /tmp/gha79/ACCEPT.txt

You will see:

Acceptance: main push creates a SHA tag; PR does not

What it means: Gate.

Step 9: Security

Why: Scan tip. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: trivy later. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 25: i1

Type this:

echo 'Add a Trivy scan step before push in DevSecOps projects'

You will see:

Add a Trivy scan step before push in DevSecOps projects

What it means: Scan.

Example 26: i2

Type this:

echo 'Fail the job on CRITICAL findings'

You will see:

Fail the job on CRITICAL findings

What it means: Fail.

Example 27: i3

Type this:

echo 'Do not push unscanned images to prod registries'

You will see:

Do not push unscanned images to prod registries

What it means: Rule.

5. Harden and accept

Step 10: Story Razorpay

Why: latest overwrite. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: story. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 28: j1

Type this:

printf '%s\n' 'At Razorpay in Bengaluru, a PR workflow pushed latest and overwrote a release mid-day.' 'Mehul limited pushes to main and required SHA tags.' > /tmp/gha79/STORY.txt && wc -l /tmp/gha79/STORY.txt

You will see:

2 /tmp/gha79/STORY.txt

What it means: Story.

Example 29: j2

Type this:

echo 'Rough cost: about ₹19 lakh in retries'

You will see:

Rough cost: about ₹19 lakh in retries

What it means: Cost.

Example 30: j3

Type this:

echo 'Mehul: PASS'

You will see:

Mehul: PASS

What it means: Sign-off.

Step 11: Proof

Why: Workflow lines. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: grep. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 31: k1

Type this:

grep build-push-action /tmp/gha79/app/.github/workflows/image.yml

You will see:

- uses: docker/build-push-action@v6

What it means: Action.

Example 32: k2

Type this:

test -f /tmp/gha79/ACCEPT.txt && echo 'accept ok'

You will see:

accept ok

What it means: accept.

Example 33: k3

Type this:

echo 'Next: Deploy to AWS with OIDC project 80'

You will see:

Next: Deploy to AWS with OIDC project 80

What it means: Bridge.

Step 12: Acceptance

Why: SHA image from main. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: final. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 34: z1

Type this:

echo 'build-push with SHA tag'

You will see:

build-push with SHA tag

What it means: Build.

Example 35: z2

Type this:

echo 'PR builds without push'

You will see:

PR builds without push

What it means: PR.

Example 36: z3

Type this:

echo 'Secrets used for login'

You will see:

Secrets used for login

What it means: Secrets.

Check yourself

Q1. Who fixed Razorpay latest overwrite?

Mehul

Q2. Action name?

build-push-action

Q3. Tag style?

git SHA

Q4. Push from?

main

Q5. PR should push?

no

Q6. Secrets where?

Actions secrets

Friends, you built and pushed an image from Actions. Did you understand? Let us try it once more. Add a PR job that builds without push and prove no new registry tag appears.

Lab: try at home

  1. Dockerfile.
  2. build-push workflow.
  3. Secrets login.
  4. Main-only push proof.

Recap

  • SHA tags.
  • Secrets for login.
  • Build on PR without push.
  • Pin action versions.
  • latest banned in prod.