CI/CD — Jenkins, GitHub Actions, GitLab CI

Cheatsheet

Dense reference for GitHub Actions, Jenkins, GitLab CI, Docker builds, scanning, ECR/OIDC and Kubernetes deploys.


GitHub Actions: syntax

KeyPurposeExample
onTriggerson: [push, pull_request]
on.push.branches / tags / pathsFilterbranches: [main], tags: ['v*'], paths: ['src/**']
on.workflow_dispatch.inputsManual run with inputsinputs: { env: { type: choice, options: [dev, prod] } }
on.scheduleCron (UTC, default branch only)- cron: '0 2 * * 1-5'
on.workflow_callReusable workflowinputs:, secrets:, outputs:
permissionsGITHUB_TOKEN scopescontents: read, id-token: write, packages: write
concurrencySerialize or cancelgroup: ${{ github.workflow }}-${{ github.ref }}
jobs.<id>.needsDependenciesneeds: [build, test]
jobs.<id>.ifConditionalif: github.ref == 'refs/heads/main'
jobs.<id>.environmentProtection rules and env secretsenvironment: prod
jobs.<id>.strategy.matrixFan-outmatrix: { node: [20, 22] }
jobs.<id>.outputsPass data to later jobsoutputs: { tag: ${{ steps.v.outputs.tag }} }
jobs.<id>.timeout-minutesKill hung jobstimeout-minutes: 15
jobs.<id>.container / servicesRun job in container, sidecarsservices: { db: { image: postgres:16 } }
defaults.run.shellStrict shellshell: bash (adds -eo pipefail)

Set outputs and env between steps:

bash
echo "tag=${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"   # step output
echo "VERSION=1.2.3"        >> "$GITHUB_ENV"      # env for later steps
echo "$HOME/bin"            >> "$GITHUB_PATH"     # PATH addition
echo "### Summary"          >> "$GITHUB_STEP_SUMMARY"

GitHub Actions: expressions and contexts

ExpressionMeaning
${{ github.sha }} / github.ref / github.ref_nameCommit SHA, full ref, short ref
${{ github.event_name }}push, pull_request, ...
${{ github.actor }} / github.repositoryUser, owner/repo
${{ secrets.NAME }} / vars.NAMESecret, configuration variable
${{ needs.job.outputs.x }}Output from earlier job
${{ matrix.node }}Current matrix value
${{ runner.os }}Linux, Windows, macOS
${{ hashFiles('**/package-lock.json') }}Cache key from lockfile
contains(), startsWith(), format(), toJSON()Built-in functions
success(), failure(), always(), cancelled()Status check functions for if:

Security reminder: never interpolate untrusted values (github.event.pull_request.title, branch names) directly into run:. Pass them through env: and quote the shell variable.

GitHub Actions: recipes

Cache and Node setup:

yaml
- uses: actions/setup-node@v4
  with: { node-version: 22, cache: npm }

OIDC to AWS:

yaml
permissions: { id-token: write, contents: read }
steps:
  - uses: aws-actions/configure-aws-credentials@v4
    with:
      role-to-assume: arn:aws:iam::123456789012:role/gha-ecr-push
      aws-region: ap-south-1

Build with cache and push:

yaml
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
  with:
    context: .
    push: true
    tags: ${{ env.IMAGE }}:${{ github.sha }}
    cache-from: type=gha
    cache-to: type=gha,mode=max

Only run when a path changed, cancel old PR runs, and gate with if:

yaml
on:
  pull_request:
    paths: ['services/api/**']
concurrency:
  group: api-${{ github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

Reusable workflow call:

yaml
jobs:
  test:
    uses: org/ci/.github/workflows/test.yml@v1
    with: { node-version: 22 }
    secrets: inherit

Pin actions by SHA (add the tag as a comment, let Dependabot update it):

yaml
- uses: actions/checkout@<full-40-char-commit-sha> # v4.2.2

GitHub CLI for pipelines

bash
gh workflow list
gh workflow run ci-cd.yml -f env=dev        # dispatch with input
gh run list --workflow ci-cd.yml --limit 5
gh run watch                                # follow the latest run
gh run view <id> --log-failed               # only failing step logs
gh run rerun <id> --failed                  # re-run failed jobs
gh run cancel <id>
gh secret set NAME < file                   # set a secret
gh variable set NAME --body value
gh pr checks --watch                        # PR checks live
gh api repos/ORG/REPO/actions/caches        # inspect caches

Jenkins

Declarative skeleton:

groovy
pipeline {
  agent { docker { image 'node:22-alpine' } }
  options { timeout(time: 30, unit: 'MINUTES'); disableConcurrentBuilds(abortPrevious: true) }
  environment { REGISTRY = 'registry.example.com' }
  stages {
    stage('Test') {
      parallel {
        stage('Unit') { steps { sh 'npm test' } }
        stage('Lint') { steps { sh 'npm run lint' } }
      }
    }
    stage('Deploy') {
      when { branch 'main' }
      steps {
        input message: 'Deploy to prod?', submitter: 'release-managers'
        withCredentials([string(credentialsId: 'api-token', variable: 'TOKEN')]) {
          sh './deploy.sh'
        }
      }
    }
  }
  post {
    always  { junit 'reports/*.xml' }
    failure { echo 'notify the team' }
  }
}
ItemNotes
agent any / label 'x' / docker / kubernetesWhere to run
when { branch 'main'; changeset 'src/**'; expression { ... } }Conditional stages
post { always, success, failure, unstable, cleanup }Post conditions
stash / unstashMove files between agents
archiveArtifacts, junitKeep artifacts, publish tests
@Library('shared@v1') _Load a shared library
withCredentials([usernamePassword(...), string(...), file(...)])Credentials, masked in logs
waitForQualityGate abortPipeline: trueSonarQube gate
Replay buttonEdit and re-run a pipeline without a commit

Useful URLs and CLI:

bash
# Syntax check without running
curl -X POST -u user:token -F "jenkinsfile=<Jenkinsfile" http://jenkins:8080/pipeline-model-converter/validate
java -jar jenkins-cli.jar -s http://jenkins:8080 -auth user:token list-jobs
java -jar jenkins-cli.jar -s http://jenkins:8080 -auth user:token build my-job -s -v
# Configuration as Code reload: Manage Jenkins -> Configuration as Code

GitLab CI

yaml
stages: [lint, test, build, deploy]

default:
  image: node:22-alpine
  cache:
    key:
      files: [package-lock.json]
    paths: [node_modules/]

variables:
  IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA

lint:
  stage: lint
  script: [npm ci, npm run lint]

test:
  stage: test
  script: [npm ci, npm test]
  coverage: '/All files.*?\|\s*([\d.]+)/'
  artifacts:
    when: always
    reports:
      junit: junit.xml

build:
  stage: build
  image: docker:27
  services: [docker:27-dind]
  needs: [test]
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build -t "$IMAGE" .
    - docker push "$IMAGE"
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

deploy-prod:
  stage: deploy
  environment: { name: production, url: https://app.example.com }
  when: manual
  needs: [build]
  script: ./deploy.sh "$IMAGE"
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
KeywordPurpose
rules: if / changes / exists / whenConditional jobs (replaces only/except)
needs: [job]DAG, start without waiting for stage
extends: .templateInherit hidden job config
include: local / project / template / componentReuse config
artifacts: paths, expire_in, reportsPass files, show test reports
cache: key, paths, policySpeed up installs
environment: name, url, action: stopDeployments and review apps
id_tokens: { AWS_TOKEN: { aud: sts.amazonaws.com } }OIDC JWT for cloud auth
parallel: matrix:Fan-out
resource_group: prodSerialize deploys
trigger:Child and multi-project pipelines
retry, timeout, allow_failure, interruptibleResilience and cost

Predefined variables: CI_COMMIT_SHA, CI_COMMIT_SHORT_SHA, CI_COMMIT_REF_NAME, CI_DEFAULT_BRANCH, CI_PIPELINE_SOURCE, CI_REGISTRY_IMAGE, CI_MERGE_REQUEST_IID, CI_JOB_TOKEN.

Docker build and cache

bash
docker buildx build --platform linux/amd64,linux/arm64 -t app:1.0 --push .
docker buildx build --cache-from type=registry,ref=reg/app:cache \
                    --cache-to   type=registry,ref=reg/app:cache,mode=max -t app:sha .
docker buildx build --cache-to type=gha,mode=max --cache-from type=gha .   # GitHub Actions
docker buildx build --sbom=true --provenance=true --push -t reg/app:sha .
DOCKER_BUILDKIT=1 docker build --secret id=npmrc,src=$HOME/.npmrc .         # RUN --mount=type=secret
Dockerfile ruleWhy
Copy lockfile and install before copying sourceDependency layer stays cached
Multi-stage build, copy only runtime filesSmaller image, fewer CVEs
Pin base image tag (or digest)Reproducible builds
USER node / non-root userReduce blast radius
.dockerignore for .git, node_modulesSmaller context, no secret leaks
No secrets in ARG/ENVThey persist in image history

Scanning and quality gates

bash
trivy image --severity HIGH,CRITICAL --ignore-unfixed --exit-code 1 app:sha
trivy image --format sarif -o trivy.sarif app:sha
trivy fs --scanners vuln,secret,misconfig .
trivy config k8s/                        # Kubernetes and Terraform misconfig
trivy image --input image.tar            # scan a tarball (no registry needed)
syft app:sha -o spdx-json > sbom.json    # SBOM
grype sbom:sbom.json --fail-on high
cosign sign --yes reg/app@sha256:<digest>
cosign verify reg/app@sha256:<digest> \
  --certificate-identity-regexp 'https://github.com/ORG/REPO/.*' \
  --certificate-oidc-issuer https://token.actions.githubusercontent.com
hadolint Dockerfile
gitleaks detect --source .

SonarQube properties that matter:

properties
sonar.projectKey=my-app
sonar.sources=src
sonar.tests=test
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.python.coverage.reportPaths=coverage.xml
sonar.qualitygate.wait=true
Gate typeTypical rule
TestsAll pass, flaky tests quarantined with a ticket
Coverage on new codeAt least 80 percent
Static analysisNo new bugs or vulnerabilities
Image scanNo fixed HIGH or CRITICAL
IaC and policytrivy config, OPA or Kyverno checks pass
Secretsgitleaks finds nothing

AWS: ECR and OIDC

bash
aws ecr get-login-password --region ap-south-1 | \
  docker login --username AWS --password-stdin 123456789012.dkr.ecr.ap-south-1.amazonaws.com
aws ecr create-repository --repository-name app --image-tag-mutability IMMUTABLE \
  --image-scanning-configuration scanOnPush=true
aws ecr describe-images --repository-name app --query 'sort_by(imageDetails,&imagePushedAt)[-5:].imageTags'
aws ecr put-lifecycle-policy --repository-name app --lifecycle-policy-text file://lifecycle.json
aws ecr batch-delete-image --repository-name app --image-ids imageTag=old
aws eks update-kubeconfig --name my-cluster --region ap-south-1
aws sts get-caller-identity                     # who am I right now?

OIDC trust policy conditions:

Token claim sub formatWhen
repo:ORG/REPO:ref:refs/heads/mainJob on main, no environment
repo:ORG/REPO:environment:prodJob that declares environment: prod
repo:ORG/REPO:pull_requestPull request event
repo:ORG/REPO:ref:refs/tags/v1.0.0Tag push

Use StringEquals for exact matches and StringLike with wildcards sparingly. Always check aud equals sts.amazonaws.com.

EKS access entries (instead of editing aws-auth):

bash
aws eks create-access-entry --cluster-name c --principal-arn <role-arn> --type STANDARD
aws eks associate-access-policy --cluster-name c --principal-arn <role-arn> \
  --policy-arn arn:aws:eks::aws:cluster-access-policy/AmazonEKSEditPolicy \
  --access-scope type=namespace,namespaces=dev

Kubernetes deploy and rollback

bash
kubectl apply -f k8s/ -n dev
kubectl set image deployment/app app=reg/app:<sha> -n dev
kubectl rollout status deployment/app -n dev --timeout=120s
kubectl rollout history deployment/app -n dev
kubectl rollout undo deployment/app -n dev                  # previous revision
kubectl rollout undo deployment/app -n dev --to-revision=3
kubectl rollout pause|resume deployment/app -n dev
kubectl diff -f k8s/                                        # preview changes
kubectl apply --dry-run=server -f k8s/                      # server-side validation
kubectl auth can-i patch deployments -n prod --as system:serviceaccount:ci:deployer
helm upgrade --install app ./chart -n prod --atomic --timeout 5m --set image.tag=<sha>
helm rollback app 2 -n prod
helm diff upgrade app ./chart -n prod                       # helm-diff plugin
kubectl argo rollouts get rollout app --watch               # Argo Rollouts
Failure patternFirst command
Rollout stuckkubectl rollout status then kubectl describe pod
ImagePullBackOffkubectl describe pod (auth, tag typo, node role)
CrashLoopBackOffkubectl logs --previous
Readiness never passesCheck probe path and port, kubectl get endpoints
Forbidden from CIkubectl auth can-i, review Role and access entry

Deployment strategies

StrategyHowRollback speedCostUse when
RecreateStop old, start newRedeployLow, has downtimeDev, singleton apps
RollingReplace pods gradually (maxSurge, maxUnavailable)MinutesLowDefault for stateless services
Blue/greenTwo full stacks, switch trafficInstant (flip back)2x capacityNeed instant rollback
CanarySmall percent first, analyze metricsFast (abort)Low to mediumRisky changes, large user base
Feature flagsDeploy dark, enable per cohortInstant (toggle)LowDecouple deploy from release
ShadowMirror traffic to new versionn/aMediumValidate without user impact

Database changes: use expand and contract migrations so old and new code both work during the rollout.

Tool comparison

GitHub ActionsJenkinsGitLab CI
ConfigYAML in .github/workflowsGroovy JenkinsfileYAML .gitlab-ci.yml
RunnerHosted or self-hosted runnerAgent (static, Docker, K8s pod)Runner (shell, docker, k8s)
ReuseReusable workflows, composite actionsShared librariesinclude, extends, components
SecretsRepo, env, org secrets, OIDCCredentials store, pluginsCI/CD variables (masked, protected), OIDC
ApprovalEnvironment reviewersinput stepProtected environments, manual jobs
EcosystemMarketplace actions1800+ pluginsBuilt-in DevSecOps features
Weak spotThird-party action riskPlugin and upgrade burdenRunner management at scale

Top 15 interview one-liners

  1. CI vs CD: CI integrates and verifies every change automatically; continuous delivery keeps main always releasable behind a manual gate; continuous deployment removes the gate.
  2. Build once, deploy many: one immutable image promoted through environments, with only config differing.
  3. Why tag with the Git SHA: it ties a running container to an exact commit, so audits and rollbacks are exact; latest is mutable and ambiguous.
  4. OIDC beats stored keys: the runner exchanges a short-lived signed token for temporary credentials, so there is nothing long-lived to leak or rotate.
  5. OIDC sub claim gotcha: jobs with an environment get repo:ORG/REPO:environment:NAME, not the branch form, so trust policies must match what the job really presents.
  6. Least-privilege GITHUB_TOKEN: set permissions: contents: read at the top and grant extra scopes per job.
  7. Pin third-party actions by commit SHA: tags can be moved; a SHA cannot, and Dependabot can keep it current.
  8. Quality gate: an automated pass or fail on tests, coverage, static analysis and scans that blocks promotion, enforced by branch protection so it cannot be skipped.
  9. Speeding up pipelines: cache dependencies, order Dockerfile layers, use BuildKit cache, parallelize independent jobs, path-filter monorepos, and cancel superseded runs.
  10. Scan before push: a vulnerable image should never reach the registry; fail on fixed HIGH and CRITICAL and track exceptions with expiry dates.
  11. Rollback: redeploy the previous known-good SHA; kubectl rollout undo or helm rollback for emergencies; in GitOps, revert the commit.
  12. Blue/green vs canary: blue/green gives instant rollback at double capacity; canary limits blast radius with real traffic and metric analysis.
  13. Jenkins vs GitHub Actions: Jenkins is self-hosted and infinitely flexible but you operate controller, agents and plugins; Actions is managed and integrated but ties you to GitHub.
  14. Secrets in pipelines: inject at runtime from a secret store or OIDC, scope per environment, never bake into images, and remember masking is not a security boundary.
  15. Flaky tests: quarantine with a tracked ticket, fix root causes (timing, shared state, network), and never fix flakiness by blanket retries.
Feedback