QA Interview Hub · Knowledge base · Quizzes · Українська

CI/CD › GitHub Actions for a test framework

Secrets, service containers, GitLab CI and Jenkins

Workflows read secrets through secrets, and the pull_request event does not pass them to PRs from forks. pull_request_target runs in the context of the base repository's default branch with access to secrets, so running PR code from it is dangerous. Untrusted values go through env, and a database is added as a service container with a healthcheck. GitLab CI and Jenkins have equivalents of these, with different syntax and details.

CI/CD · GitHub Actions

In a fork PR a test fails on "no token", and a PR title runs as a command. What is wrong?

API tests need a token and a database. On a colleague's PR everything is green, while on a fork PR tests fail: there is no token in the run. Someone suggests "just turn on pull_request_target", because then the secrets show up. That advice opens a door to secret theft. This lesson is about secrets, service containers and how the same ideas look in GitLab CI and Jenkins.

How it works and why. Secrets are stored in Settings → Secrets and variables (repository, environment or organization level) and read as ${{ secrets.API_TOKEN }}. They are masked in logs, but the mask looks for the exact string, so a modified or split secret can leak. Non-sensitive settings (an environment URL, flags) are vars, not secrets. For PRs from forks the pull_request event gets no secrets and a read-only token: that protects against theft. The pull_request_target event runs in the context of the base branch with access to secrets, so checking out and running foreign code from it is dangerous.

The second trap: an expression ${{ … }} is substituted into the command text before the shell sees it. If it holds text the PR author controls (a title, a branch), the title can contain "; curl … and it will run as code. So untrusted values are passed through env and read as a shell variable.

A value in a command

Wrong

An expression in run text

The PR title is pasted into the command line and can become part of the script. That is an injection.

Right

The value through env

The value sits in an environment variable and is read as a quoted $PR_TITLE: it is data, not code.

Figure 1. Secrets, too, are better passed through env only to the step that needs them.
- name: Notify
  env:
    API_TOKEN: ${{ secrets.API_TOKEN }}
    PR_TITLE: ${{ github.event.pull_request.title }}
    API_URL: ${{ vars.API_URL }}
  run: |
    curl -fsS -H "Authorization: Bearer $API_TOKEN" \
      --data-urlencode "title=$PR_TITLE" "$API_URL/notify"

Service containers. For integration tests with a database, add services: to a job: the container starts before the steps and stops after the job.

jobs:
  api-tests:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:17
        env:
          POSTGRES_PASSWORD: test
          POSTGRES_DB: app_test
        ports: ['5432:5432']
        options: >-
          --health-cmd "pg_isready -U postgres"
          --health-interval 5s
          --health-timeout 5s
          --health-retries 10
    env:
      DATABASE_URL: postgres://postgres:test@localhost:5432/app_test
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with: { node-version: lts/*, cache: npm }
      - run: npm ci
      - run: npm test

The --health-* options make the job wait until the database is actually ready, not just started. If the job runs on the runner, the service is reached through localhost and a mapped port. If the job itself runs in a container (container:), the service address is its name (postgres) and the internal port. The same principle with docker compose is in the lesson “docker compose for e2e: app + db + tests”.

Other systems: the same ideas. Syntax differs, not the approach: triggers, cache, parallelism, artifacts, secrets and service containers.

  • GitLab CI: a .gitlab-ci.yml file, stages, parallel: 4 for shards and parallel:matrix for parameter combinations, cache:, artifacts: when: always, services:, CI/CD variables.
  • Jenkins: a Jenkinsfile with stages and parallel, a post { always { ... } } block for reports, agents instead of runners, credentials instead of secrets.
# .gitlab-ci.yml
stages: [test]
e2e:
  stage: test
  image: mcr.microsoft.com/playwright:v1.65.1-noble
  parallel: 4
  script:
    - npm ci
    - npx playwright test --shard=$CI_NODE_INDEX/$CI_NODE_TOTAL
  artifacts:
    when: always
    paths: [playwright-report/]

Limits and alternatives. For cloud access, short-lived tokens through OIDC are better than long-lived keys in secrets. Environment secrets can be guarded by a required approval before a run. With the Playwright Docker image on GitLab, the tag version is bumped together with @playwright/test. Most tests need no real secrets: for API tests on a PR a test token of an ephemeral environment is often enough.

Typical mistakes.

  • pull_request_target with a checkout of fork code: secrets in the hands of foreign code.
  • A PR title or branch directly in run.
  • A secret in the env of a whole job instead of one step.
  • A service without a healthcheck: tests start before the database is ready.
  • Sensitive and non-sensitive values mixed in secrets.

Check yourself

Why are secrets unavailable in a fork PR, and why is pull_request_target not a safe cure?

Answer

Code in a fork PR is foreign and with secrets it could exfiltrate them. So pull_request does not pass them. pull_request_target gives secrets but runs in the base branch context: if it checks out and runs PR code, foreign code gets access to secrets.

A job runs on the runner and the database sits in a service container. What is the database address in DATABASE_URL?

Answer

localhost and the mapped port from ports. If the job itself ran in a container, the address would be the service name with the internal port.

In an interview. Junior: Secrets are stored in repository settings and read through secrets in a workflow, and masked in logs. For a database in tests I use a service container. Middle/Senior: I pass secrets through env only to the step that needs them and never put untrusted values straight into run. I do not use pull_request_target with foreign code and replace long-lived keys with OIDC. I add a healthcheck to the service container. The ideas carry over between GitHub Actions, GitLab CI and Jenkins, only the syntax changes.

More articles in this chapter