4

I have a very long command in .gitlab-ci.yml file to ssh to jump host then use rsync to sync files from my repo to destination host (it does not have public ip so I need to access it via jump host).

Here is my .gitlab-ci:

image: ubuntu
before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client rsync git -y )'
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - echo -e "$PRIVATE_KEY" > ~/.ssh/id_rsa
  - chmod 600 ~/.ssh/id_rsa
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'


SSH:
  tags:
    - mytag
  script:
  - ssh ubuntu@host "which ssh-agent || ( apt-get update -y && apt-get install openssh-client rsync git -y ) && ssh ubuntu@192.168.1.2 "mkdir -p /home/ubuntu/test" && rsync /home/ubuntu/test/.gitlab-ci.yml ubuntu@192.168.1.2:/home/ubuntu/test/.gitlab-ci.yml"

The command here is so long:

 ssh ubuntu@host "which ssh-agent || ( apt-get update -y && apt-get install openssh-client rsync git -y ) && ssh ubuntu@192.168.1.2 "mkdir -p /home/ubuntu/test" && rsync /home/ubuntu/test/.gitlab-ci.yml ubuntu@192.168.1.2:/home/ubuntu/test/.gitlab-ci.yml"

Let me explain it:

  • First it ssh to host then install rsync if it does not exist, then it ssh to my private server 192.168.1.2 and then sync my file named .gitlab-ci.yml (the file from my repo in my Gitlab server) to folder test

I just wonder if there is any way to break long command above to multi lines command?

6

Since .gitlab-ci.yml is a Yaml file, then just use it's syntax.

For example, you may use >:

image: ubuntu
before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client rsync git -y )'
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - echo -e "$PRIVATE_KEY" > ~/.ssh/id_rsa
  - chmod 600 ~/.ssh/id_rsa
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'


SSH:
  tags:
    - mytag
  script: > 
  - ssh ubuntu@host "which ssh-agent || 
    ( apt-get update -y && apt-get install openssh-client rsync git -y ) && 
    ssh ubuntu@192.168.1.2 "mkdir -p /home/ubuntu/test" && 
    rsync /home/ubuntu/test/.gitlab-ci.yml ubuntu@192.168.1.2:/home/ubuntu/test/.gitlab-ci.yml"

Sample usage of yaml multilines

There is a great resource about yaml multilines: YAML Multiline Strings Also, there is a great SO answer about yaml multilines.

So let's speculate about pipeline based on these materials. Since we have specific context (script for Gitlab CI) then we don't need all of theese 63 cases:

                      >     |            "     '     >-     >+     |-     |+
-------------------------|------|-----|-----|-----|------|------|------|------  
Trailing spaces   | Kept | Kept |     |     |     | Kept | Kept | Kept | Kept
Single newline => | _    | \n   | _   | _   | _   | _    |  _   | \n   | \n
Double newline => | \n   | \n\n | \n  | \n  | \n  | \n   |  \n  | \n\n | \n\n
Final newline  => | \n   | \n   |     |     |     |      |  \n  |      | \n
Final dbl nl's => |      |      |     |     |     |      | Kept |      | Kept  
In-line newlines  | No   | No   | No  | \n  | No  | No   | No   | No   | No
Spaceless newlines| No   | No   | No  | \   | No  | No   | No   | No   | No 
Single quote      | '    | '    | '   | '   | ''  | '    | '    | '    | '
Double quote      | "    | "    | "   | \"  | "   | "    | "    | "    | "
Backslash         | \    | \    | \   | \\  | \   | \    | \    | \    | \
" #", ": "        | Ok   | Ok   | No  | Ok  | Ok  | Ok   | Ok   | Ok   | Ok
Can start on same | No   | No   | Yes | Yes | Yes | No   | No   | No   | No
line as key       |

Assumptions are:

  • we don't need trailing spaces at all
  • we don't need final newlines at all
  • we'll try to avoid quote escaping mess
  • Backslash is intended to combine multi-line scripts
  • almost doesn't matter how many spaces before the script

P.s. I'd suggest to lint your .gitlab-ci.yml using GitLab UI

2
  • 1
    Awesome, let me try this! Thank you a lot!
    – Chau Giang
    Mar 12 '20 at 7:18
  • 1
    My pipeline says: Job succeeded, thanks you for your help!
    – Chau Giang
    Mar 12 '20 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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