简体   繁体   English

GitLab CI/CD - 如何在命令中使用变量?

[英]GitLab CI/CD - how to use variable in command?

I wrote following pipeline:我写了以下管道:

image: maven:3-openjdk-11

variables:
    TARGET_LOCATION: "/tmp/uploads/"

stages:
    - deploy

deploy-job:
    stage: deploy
    before_script:
    - export MAVEN_ARTIFACT_VERSION=$(mvn --non-recursive help:evaluate -Dexpression=project.version | grep -v '\[.*'| tail -1)
    - export MAVEN_ARTIFACT=app-${MAVEN_ARTIFACT_VERSION:+$MAVEN_ARTIFACT_VERSION.jar} 
    script:
    - eval $(ssh-agent -s)
    (SSH STUFF HERE...)
    - scp -o HostKeyAlgorithms=ssh-rsa -p /builds/xxxxx/app/target/$MAVEN_ARTIFACT user@host:${TARGET_LOCATION}

I expected the $MAVEN_ARTIFACT in scp command change to something like app-BETA-0.1.jar and TARGET_NAME change it's value but it's not parsing and I got variable name in both places.我希望 scp 命令中的 $MAVEN_ARTIFACT 更改为app-BETA-0.1.jar和 TARGET_NAME 更改它的值,但它没有解析,我在两个地方都有变量名。 I tried with brackets as well but I can't achieve what I want.我也尝试过使用括号,但我无法实现我想要的。 Is there any way to pass variables generated during script execution as arguments to other programs executed in the same script section?有没有办法将脚本执行期间生成的变量作为参数传递给在同一脚本部分中执行的其他程序?

Below is a piece of logs from pipeline execution:以下是管道执行的一段日志:

$ scp -o HostKeyAlgorithms=ssh-rsa -p /builds/xxxxx/app/target/$MAVEN_ARTIFACT user@host:${TARGET_LOCATION}

You're using these correctly, and it is working.您正确使用了这些,并且它正在工作。

The GitLab pipeline logs show the command exactly as you write it in your script. GitLab 管道日志完全按照您在脚本中编写的方式显示命令。 It will not replace variables with their values.它不会用它们的值替换变量。 If you need to verify the value or confirm it's set, use standard debugging techniques like printing the value with something like echo $MAVIN_ARTIFACT .如果您需要验证该值或确认它已设置,请使用标准调试技术,例如使用echo $MAVIN_ARTIFACT之类的打印值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM