TExcelTAD
Home › CI/CD Pipelines & Pipeline Security › Mini Project: One Pipeline, Two Platforms

Mini Project: One Pipeline, Two Platforms

The project brings the course together: one service, one set of make targets, and the same pipeline expressed for GitLab CI and for GitHub Actions: lint, unit tests, Semgrep, secret detection, image build, Trivy gate and push by digest. The GitLab version runs for real below with gitlab-ci-local; the GitHub version is the same steps in Actions syntax.

The plan

Stage Make target Tool Gate
check lint py_compile, git diff --check syntax errors, whitespace damage
check test unittest failing tests
check sast Semgrep with the repository's rules (SAST with Semgrep) any ERROR finding
check secrets gitleaks over the history (Secret Detection) any leak
build image scan crane (assemble the image), Trivy (Container Scanning) fixable HIGH/CRITICAL
publish push crane copy to the release repository, record the digest only on the default branch

Design rules from the course, applied:

  • Logic in make, YAML thin (Pipeline as Code): both CI files only call make.
  • Cheap checks first, in parallel: the four check jobs have no dependencies on each other.
  • Gate before publish: the image is built into a CI-only repository, scanned, and only then copied to the release repository (the build → scan → push workflow).
  • The digest is the output: push writes IMAGE_REF=…@sha256:… as a dotenv report, ready for a deploy job.

The GitLab pipeline, run

The session creates the repository, runs the pipeline on main, then commits a change that introduces shell=True and runs it again:

bash · try it yourself
mkdir shop && cd shop && git init -q
mkdir -p app tests .semgrep
cat > app/__init__.py <<'PY'
PY
cat > app/pricing.py <<'PY'
def total(prices, discount=0.0):
    if not 0 <= discount < 1:
        raise ValueError("discount must be in [0, 1)")
    return round(sum(prices) * (1 - discount), 2)
PY
cat > tests/test_pricing.py <<'PY'
import unittest
from app.pricing import total

class TotalTest(unittest.TestCase):
    def test_sum(self):
        self.assertEqual(total([1.5, 2.5]), 4.0)
    def test_discount(self):
        self.assertEqual(total([10, 10], 0.1), 18.0)
    def test_bad_discount(self):
        with self.assertRaises(ValueError):
            total([1], 1.5)
PY
cat > .semgrep/rules.yml <<'YML'
rules:
  - id: subprocess-shell-true
    languages: [python]
    severity: ERROR
    message: subprocess with shell=True; pass a list of arguments
    pattern: subprocess.$F(..., shell=True, ...)
YML
cat > Makefile <<'MK'
REGISTRY ?= localhost:5000
IMAGE    ?= $(REGISTRY)/shop
VERSION  ?= dev
export PYTHONDONTWRITEBYTECODE=1

.PHONY: lint test sast secrets image scan push
lint:
	python3 -m py_compile app/*.py tests/*.py
	git diff --check $(git hash-object -t tree /dev/null) HEAD --
test:
	python3 -m unittest discover -s tests -q
sast:
	semgrep scan --config .semgrep/ --metrics=off --quiet --error app
secrets:
	gitleaks git . --no-banner --redact --log-level warn && echo "gitleaks: no leaks found"
image:
	mkdir -p dist/rootfs/srv && cp -r app dist/rootfs/srv/
	tar --sort=name --mtime=@$SOURCE_DATE_EPOCH --owner=0 --group=0 --numeric-owner -C dist/rootfs -cf dist/layer.tar .
	crane append -f dist/layer.tar -t $(REGISTRY)/ci/shop:$(VERSION) 2>/dev/null >/dev/null
	echo "built $(REGISTRY)/ci/shop:$(VERSION)"
scan:
	trivy image -q --insecure $(REGISTRY)/ci/shop:$(VERSION) --severity HIGH,CRITICAL --ignore-unfixed --exit-code 1 -f json -o dist/trivy.json
	echo "trivy gate passed: $(jq '[.Results[]?.Vulnerabilities[]?] | length' dist/trivy.json) findings"
push:
	crane copy $(REGISTRY)/ci/shop:$(VERSION) $(IMAGE):$(VERSION) 2>/dev/null
	echo "IMAGE_REF=$(IMAGE)@$(crane digest $(IMAGE):$(VERSION))" > dist/image.env
	cut -c1-40 dist/image.env
MK
cat > .gitlab-ci.yml <<'YML'
stages: [check, build, publish]
variables:
  VERSION: $CI_COMMIT_SHORT_SHA
lint:
  stage: check
  script: [make -s lint]
test:
  stage: check
  script: [make -s test]
sast:
  stage: check
  script: [make -s sast]
secrets:
  stage: check
  script: [make -s secrets]
build-image:
  stage: build
  script: [make -s image scan]
  artifacts:
    when: always
    paths: [dist/trivy.json]
push:
  stage: publish
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  script: [make -s push]
  artifacts:
    reports:
      dotenv: dist/image.env
YML
git add . && git commit -qm "feat: pricing service with pipeline"
git remote add origin https://gitlab.example.com/acme/shop.git
git update-ref refs/remotes/origin/main HEAD && git remote set-head origin main

echo "########## pipeline 1: main"
gitlab-ci-local --concurrency 1 2>&1 | grep -E ' > |^ (PASS|FAIL|WARN) '
echo "exit ${PIPESTATUS[0]}"

cat > app/ping.py <<'PY'
import subprocess

def ping(host):
    return subprocess.run("ping -c 1 " + host, shell=True)
PY
git add . && git commit -qm "feat: ping endpoint"
echo "########## pipeline 2: a risky change"
gitlab-ci-local --concurrency 1 2>&1 | grep -E ' > |^ (PASS|FAIL|WARN) '
echo "exit ${PIPESTATUS[0]}"
########## pipeline 1: main
test        > ----------------------------------------------------------------------
test        > Ran 3 tests in 0.000s
test        >
test        > OK
secrets     > gitleaks: no leaks found
build-image > built localhost:5000/ci/shop:76b56d7d
build-image > trivy gate passed: 0 findings
push        > IMAGE_REF=localhost:5000/shop@sha256:c00
 PASS  lint
 PASS  test
 PASS  sast
 PASS  secrets
 PASS  build-image
 PASS  push
exit 0
########## pipeline 2: a risky change
test        > ----------------------------------------------------------------------
test        > Ran 3 tests in 0.000s
test        >
test        > OK
sast        >
sast        >
sast        > ┌────────────────┐
sast        > │ 1 Code Finding │
sast        > └────────────────┘
sast        >
sast        >     app/ping.py
sast        >    ❯❯❱ semgrep.subprocess-shell-true
sast        >           ❰❰ Blocking ❱❱
sast        >           subprocess with shell=True; pass a list of arguments
sast        >
sast        >             4┆ return subprocess.run("ping -c 1 " + host,
sast        >                shell=True)
sast        >
sast        > make: *** [Makefile:13: sast] Error 1
secrets     > gitleaks: no leaks found
 PASS  lint
 PASS  test
 PASS  secrets
 FAIL  sast
  >             4┆ return subprocess.run("ping -c 1 " + host,
  >                shell=True)
  > make: *** [Makefile:13: sast] Error 1
exit 1

Walkthrough

  • Pipeline 1 passed every gate. The image was assembled, scanned (a minimal image with only the application files has nothing for Trivy to report), copied to the release repository, and its digest recorded in IMAGE_REF. A deploy job would take that dotenv variable, as in Environments & Manual Deployments.
  • Pipeline 2 stopped in the check stage: Semgrep flagged the new shell=True call, so build-image and push never ran and nothing new reached the registry. The other check jobs still ran to completion, so the author sees every problem in one pipeline.
  • The job is called build-image, not image: image is a reserved top-level keyword in .gitlab-ci.yml (the default container image), and a job with that name breaks the configuration.

The same pipeline in GitHub Actions

The steps are the same make targets; only the wiring changes. On GitHub-hosted runners the tools are installed by a pinned composite action (Reusable Workflows & Composite Actions), and the registry is GHCR:

yaml
# .github/workflows/pipeline.yml
name: pipeline
on:
  push:
    branches: [main]
  pull_request:

permissions:
  contents: read

concurrency:
  group: pipeline-${{ github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

env:
  REGISTRY: ghcr.io/${{ github.repository_owner }}
  IMAGE: ghcr.io/${{ github.repository }}
  VERSION: ${{ github.sha }}

jobs:
  check:
    runs-on: ubuntu-24.04
    timeout-minutes: 10
    strategy:
      fail-fast: false                       # report every failing check, like the GitLab stage
      matrix:
        target: [lint, test, sast, secrets]
    steps:
      - uses: actions/checkout@v7
        with:
          fetch-depth: 0                     # gitleaks and git diff need history
          persist-credentials: false
      - uses: acme/pipelines/actions/setup-tools@v3    # pinned semgrep, gitleaks, trivy, crane
      - run: make -s ${{ matrix.target }}

  build-image:
    needs: check
    runs-on: ubuntu-24.04
    timeout-minutes: 15
    permissions:
      contents: read
      packages: write
    outputs:
      image-ref: ${{ steps.push.outputs.IMAGE_REF }}
    steps:
      - uses: actions/checkout@v7
        with:
          persist-credentials: false
      - uses: acme/pipelines/actions/setup-tools@v3
      - name: Log in to GHCR
        run: echo "$GH_TOKEN" | crane auth login ghcr.io -u "$GITHUB_ACTOR" --password-stdin
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - run: make -s image scan
      - uses: actions/upload-artifact@v7
        if: always()
        with:
          name: trivy-report
          path: dist/trivy.json
      - id: push
        if: github.ref == 'refs/heads/main'
        run: |
          make -s push
          cat dist/image.env >> "$GITHUB_OUTPUT"

The mapping, element by element:

GitLab GitHub Actions
stages: [check, build, publish] + four check jobs a check job with a 4-way matrix, then needs: check
rules: if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH if: github.ref == 'refs/heads/main' on the push step
artifacts: when: always upload-artifact with if: always()
artifacts: reports: dotenv step output written to $GITHUB_OUTPUT, exposed as a job output
VERSION: $CI_COMMIT_SHORT_SHA VERSION: ${{ github.sha }}
protected variables for the registry permissions: packages: write on one job, the automatic GITHUB_TOKEN

Extend it

  1. Add a deploy-staging job on both platforms that consumes IMAGE_REF and refuses anything not pinned by digest; then a manual/approved deploy-production (GitHub environments, GitLab environments).
  2. Add syft to make image and attach the SBOM as an artifact; post an SBOM diff on pull requests (SBOMs in the Pipeline).
  3. Make sast diff-aware on merge requests with --baseline-commit (Writing Semgrep Rules), while main keeps the full scan.
  4. Run the pipeline doctor over the GitHub workflow and fix what it reports, then add actionlint.
  5. Rebuild the image on the old Alpine base from the workflow lesson and watch the Trivy gate stop push; add an expiring exception and watch it pass, then expire.

Test yourself

In pipeline 2, why did build-image and push not run?

  1. They were manual
  2. The sast job in the earlier stage failed, so later stages were skipped
  3. The registry was down
  4. gitlab-ci-local skips them

Why are all four checks reported even though only Semgrep failed?

  1. Retries
  2. They are independent jobs in one stage (a matrix with fail-fast: false on GitHub), so each runs to completion
  3. allow_failure is set
  4. They ran twice

Why can't the GitLab job be called image?

  1. Names must contain a dash
  2. image is a reserved top-level keyword (the default container image)
  3. It's too short
  4. Jobs can't be named after make targets