Home > Software engineering >  GitLab CI/CD - how to use variable in command?
GitLab CI/CD - how to use variable in command?

Time:07-13

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}

CodePudding user response:

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.

  • Related