2

Within a gitlab-ci job, I want to create a directory (on a remote server) with the job id and perform some actions more or less as follows:

- ssh user@server mkdir -p /user/$CI_JOB_ID
- ssh user@server <perform_some_action_that_creates_several_zip_files>
- LAST_MODIFIED_FILE=$(ssh user@server bash -c 'find /user/$CI_JOB_ID -iname "*.zip" | tail -n 1 | xargs readlink -f')

The directory does get created and the action that creates several zips works out.

However, the last command that I use for getting the last modified/created .zip does not work, because $CI_JOB_ID does not seem to get expanded.

Any suggestions?

5
  • 1
    Is CI_JOB_ID a environment variable in remote machine? Commented Nov 15, 2017 at 12:16
  • no, but this works: ssh user@server mkdir -p /tmp/$CI_JOB_ID Commented Nov 15, 2017 at 12:18
  • try using double quotation instead : "find /user/$CI_JOB_ID -iname '*.zip' | tail -n 1 | xargs readlink -f". Here, the single quotes may prevent variable expansion for $CI_JOB_ID Commented Nov 15, 2017 at 12:49
  • I have tried it ... does not work either ... I have also made a local copy of a variables file holding entries such as export CI_JOB_ID=$CI_JOB_ID, copying it to the remote server and sourcing it ... no luck either ... Commented Nov 15, 2017 at 12:56
  • come to think of it, it makes sense that (when sourcing the variables file) the variable is not available... these are distinct ssh sessions Commented Nov 15, 2017 at 13:00

1 Answer 1

5

This issue is due to your ssh call. The way you do it now, you are mixing contexts :

ssh user@server bash -c 'find /user/$CI_JOB_ID -iname "*.zip" | tail -n 1 | xargs readlink -f'
  • bash -c 'my_commands' : you use simple quotes, so ssh will execute exactly the instruction my_commands. In your case, it will try to find the remote value for $CI_JOB_ID, instead of using the local one.

  • ssh user@server bash -c "my_commands" : withssh, the commands to execute on the remote shell are sent as a single string. Thus, if you were to run ssh with this quotation, it would try to run "bash -c find ... | tail ... | xargs ...". Here, only find is run through bash -c.

In your case, simply writing directly the following statement should do the trick :

ssh user@server "find /user/$CI_JOB_ID -iname '*.zip' | tail -n 1 | xargs readlink -f"

Otherwise, if you want to keep using the bash -c syntax, you'd have to escape the quotation so that it is propagated to the remote machine :

ssh user@server bash -c \"find /user/$CI_JOB_ID -iname '*.zip' | tail -n 1 | xargs readlink -f\"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.