2

Colleagues! Such question, whether it is possible to use if conditions via shell-runner on a remote server? Here is a test gitlab-ci.yml:

world_branch:
  variables: 
    dir: "/home/TATATA"
  environment: staging
  script:
  - ssh-keyscan -H 192.168.0.12 >> ~/.ssh/known_hosts
  - ssh [email protected] "
      'if [[ -d "$dir" ]]; then
          rm -r $dir && mkdir $dir
          fi'
       "
  - rsync -avz --delete-after --exclude=".git" --exclude=".gitlab-ci.yml" . [email protected]:/home/TATATA
  only: 
   - world

After running the job, get the next error:

bash: if [[ -d /home/TATATA ]] rm -r /home/TATATA && mkdir /home/TATATA fi: No such file or directory

1 Answer 1

2

It was fixed only in this way:

- ssh [email protected] "
       if [[ -d "$dir" ]]; then
         rm -r $dir && mkdir $dir;
       fi
       "

I mean, than you need to put after the shell command ;

For constructions where the shell commands are two or more, you just need to add to the end of each one again ;

For example:

- ssh [email protected] "
     if [[ ! -d "$dir" ]]; then
        mkdir $dir;
        mkdir $dir/test;
     fi
     "

Not the answer you're looking for? Browse other questions tagged or ask your own question.