Search code examples
gitlabgitlab-cisemantic-versioning

Gitlab-ci lost environment variables


I'm developing a pipeline on GitLab-ci, in the first job I use gittools/gitversion the obtain the semantic version of my software.

Here a small piece of code of /gitversion-ci-cd-plugin-extension.gitlab-ci.yml (Full documentation here https://gitversion.net/docs/reference/build-servers/gitlab)

.gitversion_function:
  image:
    name: gittools/gitversion
    entrypoint: ['']
  stage: .pre
  .....
  .....
  artifacts:
    reports:
      #propagates variables into the pipeline level
      dotenv: thisversion.env

Then a simplified version of my pipeline is as follows

stages:
  - .pre
  - install_dependencies
  - build
  - deploy


include:
  - local: '/gitversion-ci-cd-plugin-extension.gitlab-ci.yml'


determineversion:
  extends: .gitversion_function

  
install_dependencies:
  image: node:16.14
  stage: install_dependencies
  script:
    - echo ${PACKAGE_VERSION}


build:
  image: node:16.14
  stage: build
  script:
    - echo $PACKAGE_VERSION


deploy:
  image: bitnami/kubectl
  stage: deploy
  needs: ['build']
  script:
    - echo $PACKAGE_VERSION

The problem is that the environment variable $PACKAGE_VERSION works in the first two jobs install_dependencies and build.

echo $PACKAGE_NAME;  //0.0.1 

But when the jobs deploy is executed the environment variable is not expanded by pipeline and I obtain literally this

echo $PACKAGE_NAME;  //$PACKAGE_NAME 

Solution

  • I found the problem. In the last job of my pipeline, I use needs (https://docs.gitlab.com/ee/ci/yaml/#needs) to establish dependencies between jobs. The problem is that artifact is not automatically passed because there is no a dependency between determineversion and deploy, to fix I do this:

    ...
    deploy:
      image: bitnami/kubectl
      stage: deploy
      needs: ['determineversion', 'build']  # <------
      script:
        - echo $PACKAGE_VERSION
    ...
    

    I added determineversion as a dependency of deploy, in this way $PACKAGE_VERSION is printed correctly