Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading