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

Problem to solve

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?

How does your Dockerfile look?

I think you need something like

ENV CUR_GIT_COMMIT=$CUR_GIT_COMMIT

I wouldn’t mind the delete message from the runner. I think I got that a few times too and it was fine. I think it’s some caching stuff going on, but you could imo ignore that.

or do you get no output here?

echo "Current git version is $CUR_GIT_COMMIT"