luigigi
luigigi

Reputation: 4215

How to set variable value over ssh in gitlab-ci.yml file

I am trying to set an environment variable for my GitLab Runner which value I retrieve using a ssh command. I tired to set the variable in the workflow like below. The problem is that the ssh command is not executed and is treated like a string.

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"
      when: always
      variables:
        TABLE_STATUS: ssh gitlab-runner@$TARGET_HOST "source $DRIVE_PATH/config/.config && mysql -u $USER -p$PASSWORD -e \"SELECT IF(MONTH(datetime) = MONTH(NOW()), IF(status = 1, 'TRUE', 'FALSE'), 'FALSE') FROM schema.table WHERE table_name = 'tbl_name';\" "
        BRANCH: prod
        DRIVE_PATH: /path/to/prod
    - when: always

The ssh command works in script but I dont know how to assigne the value to a variable before script.

Upvotes: 3

Views: 3103

Answers (1)

VonC
VonC

Reputation: 1323803

Directly using a string command in your variable is indeed likely to result in... a string value.

Executing the command should involve, as seen here, the subshell command $(xxx).

In your case, from your comment, using the before_script directive:

before_script:
   TABLE_STATUS=$(ssh gitlab-runner@$TARGET_HOST "source $DRIVE_PATH/config/.config && mysql ...)

Upvotes: 2

Related Questions