gitlab-ci

env-file with variables is not retained in following stage in gitlab-ci


I'm trying to implement a stage for building a docker-image in my gitlab-ci-pipeline using kaniko. During the build-process I would need the current git-hash, but the kaniko-image does not contain git. Thus, I wanted to have a separate stage in my pipeline for saving the current git-hash in an environment variable and forwarding it using a dotenv-artifact:

prepare_docker_build_job:
  stage: prepare_docker
  extends:
    - .rules:new_push_pipelines
  <<: *before_script_template
  needs: [test_job]
  script:
    - echo "CUR_GIT_VERSION=$(git describe --always --dirty)" >> git_variables.env
  artifacts:
    reports:
      dotenv: git_variables.env

Afterwards, I should be able to access it in my build job:

docker_build_job:
  stage: docker_build
  needs:
    - job: prepare_docker_build_job
      optional: false
      artifacts: true
  extends:
    - .rules:new_push_pipelines
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  script:
    - echo "Current git version is $CUR_GIT_COMMIT"
    - echo "Current project path is $CI_PROJECT_DIR"
    - /kaniko/executor --context "$CI_PROJECT_DIR"
      --cache=true
      --dockerfile "${CI_PROJECT_DIR}/docker/docker_setup"
      --destination "${CI_REGISTRY_LOCATION}:${CI_COMMIT_TAG}"
      --build-arg CUR_GIT_COMMIT=$CUR_GIT_COMMIT

However, during the setup-part of docker_build I can see that the existing file git_variables.env gets deleted, and subsequently $CUR_GIT_COMMIT is undefined. Did I forget anything here to allow docker_build to retain the env-file?


Solution

  • As mentioned by @kofemann, this will work, as long as the variable (in this case $CUR_GIT_VERSION) is referenced in the subsequent job.

    Full Example:

    first_job:
      stage: prepare_docker
      script:
        - echo "CUR_GIT_VERSION=$(git describe --always --dirty)" >> git_variables.env
      artifacts:
        reports:
          dotenv: git_variables.env
    
    second_job:
      stage: docker_build
      needs:
        - job: prepare_docker_build_job
          optional: false
          artifacts: true
      script:
        - echo "Variable is $CUR_GIT_VERSION"