Skip to content

Add mechanism to pass env vars from host into docker #1617

@mcintyre321

Description

@mcintyre321

I am running into a problem with gitlab-ci-local's behaviour around env vars.

When you run a pipeline like

stages:
  - debug
debug:
  stage: debug
  script:
    - echo ${SOME_ENV_VAR}
debug_in_pyimage:
  stage: debug
  image: ghcr.io/astral-sh/uv:0.8.3-python3.11-alpine
  script:
    - echo ${SOME_ENV_VAR}

the SOME_ENV_VAR from the host is available in the first job, but not in the dockerised job:

$ export SOME_ENV_VAR=foo; gitlab-ci-local
parsing and downloads finished in 87 ms.
json schema validated in 185 ms
debug            starting shell (debug)
debug_in_pyimage starting ghcr.io/astral-sh/uv:0.8.3-python3.11-alpine (debug)
debug            $ echo ${SOME_ENV_VAR}
debug            > foo
debug            finished in 54 ms
debug_in_pyimage copied to docker volumes in 1.44 s
debug_in_pyimage $ echo ${SOME_ENV_VAR}
debug_in_pyimage >
debug_in_pyimage finished in 2.14 s

This creates a difference with gitlab executed pipelines- in gitlab , variables are always available to all jobs, as they have been defined as predefined variables. However, my users are defining variables locally, and, as they work locally in non-docker jobs they are surprised when they modify their pipelines to use docker, and the var is no longer available.

I understand that the correct way to pass in the vars is .gitlab-ci-local-variables.yml which I can do, but I am limited to static values - I can't check in secrets, and I can't have developers set their own env vars locally.

Describe the solution you'd like

I would like a mechanism to set variables from local env var values e.g.

An additional

.gitlab-ci-local-variables.host.yml

SOME_ENV_VAR
SOME_OTHER_VAR

These should get copied from the host into the context for all jobs, including the start and docker jobs.

Possibly I would like to restrict host env vars being passed to non-docker jobs so that the variables file is required, as this would eliminate the discrepancy in behaviour, but I think the ship has sailed

other solutions

Pass all host env vars to the docker stages without requiring additional config

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureFeature missing according to gitlab ci standard

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions