I have two jobs on a GitLab pipeline: In the first one I save a value into a txt file in order to share this value through an artifact to the next job. The second one is a dotnet job and I would like to get the value from the txt file I populated in the job1 an save this value into a variable.

stages:
  - stg1
  - stg2

job1:
  stage: stg1
  script:
    - echo ${NEW_VERSION} > build.txt
  artifacts:
    expire_in: 1 week
    paths:
      - build.txt
  variables:
    NEW_VERSION: "1.2.3"
      
job2:
  stage: stg2
  needs: [job1]
  tags:
    - dotnet
  script:
    # Here I need to get value from build.txt and set it into the $NEW_VERSION variable
    - '& "$NUGET_PATH" dotnet restore'
    - '& "$MSBUILD_PATH" /nologo /p:Configuration=Release'
    - 'mkdir target'
    - 'Add-Type -assembly "system.io.compression.filesystem"'
    - '[io.compression.zipfile]::CreateFromDirectory("$CI_PROJECT_DIR\$EXE_RELEASE_FOLDER\", "$CI_PROJECT_DIR\target\$PACKAGE_NAME-$NEW_VERSION.zip")'   
  artifacts:
    expire_in: 1 week
    paths:
      - build.txt
  dependencies:
    - job1 
  variables:
    NEW_VERSION: ""
1

There are 1 answers

0
mggSoft On

Finally I found the solution to the problem by using '$NEW_VERSION=type .\build.txt' so the final YAML would be:

stages:
  - stg1
  - stg2

job1:
  stage: stg1
  script:
    - echo ${NEW_VERSION} > build.txt
  artifacts:
    expire_in: 1 week
    paths:
      - build.txt
  variables:
    NEW_VERSION: "1.2.3"
      
job2:
  stage: stg2
  needs: [job1]
  tags:
    - dotnet
  script:
    - '$NEW_VERSION=type .\build.txt'
    - '& "$NUGET_PATH" dotnet restore'
    - '& "$MSBUILD_PATH" /nologo /p:Configuration=Release'
    - 'mkdir target'
    - 'Add-Type -assembly "system.io.compression.filesystem"'
    - '[io.compression.zipfile]::CreateFromDirectory("$CI_PROJECT_DIR\$EXE_RELEASE_FOLDER\", "$CI_PROJECT_DIR\target\$PACKAGE_NAME-$NEW_VERSION.zip")'   
  artifacts:
    expire_in: 1 week
    paths:
      - build.txt
  dependencies:
    - job1 
  variables:
    NEW_VERSION: ""