简体   繁体   中英

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. 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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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