1

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?

3

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"

P.s. there is a great resource about yaml multilines: YAML Multiline Strings

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

  • 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.