how to pass value from commit to GitLab CI pipeline as variable?

前提是你 提交于 2019-12-11 12:50:23

问题


I need to dynamically pass value to GitLab CI pipeline to pass the value further to jobs. The problem is: the value cannot be stored in the code and no pipeline reconfiguration should be needed (e.g. I can pass the value in "variables" section of .gitlab-ci.yml but it means store value in the code, or changes in "Environment variables" section of "CI / CD Settings" means manual reconfiguration). Also, branch name cannot be used for that purpose too.

It is not a secret string but a keyword which modifies pipeline execution. So, how can I do it?


回答1:


You didn't specify the source of this value.

You say "pass value from commit to ..."
If it's some meta information about the commit itself, look at the list of Predefined environment variables
There's quite a lot of vars named CI_COMMIT_* which might work for you.

However, if it's some value that you generate in the pipeline in one job and want to pass to another job - it's a different case. There is a long-living request to Pass variables between jobs, which is still not implemented.

The workaround for this moment is to use artifacts - files to pass information between jobs in stages.
Our use case is to extract Java app version from pom.xml and pass it to some packaging job later.
Here is how we do it in our .gitlab-ci.yml:

...
variables:
  VARIABLES_FILE: ./variables.txt  # "." is required for image that have sh not bash

...

get-version:
  stage: prepare
  image: ...
  script:
    - APP_VERSION=...
    - echo "export APP_VERSION=$APP_VERSION" > $VARIABLES_FILE
  artifacts:
    paths:
      - $VARIABLES_FILE
...
package:
  stage: package
  image: ...
  script:
    - source $VARIABLES_FILE
    - echo "Use env var APP_VERSION here as you like ..."


来源:https://stackoverflow.com/questions/56817867/how-to-pass-value-from-commit-to-gitlab-ci-pipeline-as-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!