Search code examples
bashgitlabyamlscp

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}

Solution

  • You're using these correctly, and it is working.

    The GitLab pipeline logs shows commands as written in the script - without expanding/replacing variables. It will, however, expand/replace variables before running commands. To check if a variable is correctly set, you may need to add an echo command, e.g. echo $MAVEN_ARTIFACT