<p>Since <code>.gitlab-ci.yml</code> is a <code>Yaml</code> file, then just use it's syntax.</p>
<p>For example, you may use <code>&gt;</code>:</p>
<pre class="lang-yaml prettyprint-override"><code>image: ubuntu
before_script:
  - 'which ssh-agent || ( apt-get update -y &amp;&amp; apt-get install openssh-client rsync git -y )'
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - echo -e &quot;$PRIVATE_KEY&quot; &gt; ~/.ssh/id_rsa
  - chmod 600 ~/.ssh/id_rsa
  - '[[ -f /.dockerenv ]] &amp;&amp; echo -e &quot;Host *\n\tStrictHostKeyChecking no\n\n&quot; &gt; ~/.ssh/config'


SSH:
  tags:
    - mytag
  script: &gt; 
  - ssh ubuntu@host &quot;which ssh-agent || 
    ( apt-get update -y &amp;&amp; apt-get install openssh-client rsync git -y ) &amp;&amp; 
    ssh ubuntu@192.168.1.2 &quot;mkdir -p /home/ubuntu/test&quot; &amp;&amp; 
    rsync /home/ubuntu/test/.gitlab-ci.yml ubuntu@192.168.1.2:/home/ubuntu/test/.gitlab-ci.yml&quot;
</code></pre>
<h1>Sample usage of <code>yaml</code> multilines</h1>
<p>There is a great resource about <code>yaml</code> multilines: <a href="https://yaml-multiline.info/" rel="nofollow noreferrer">YAML Multiline Strings</a>
Also, there is a <a href="https://stackoverflow.com/a/21699210/5720818">great SO answer</a> about <code>yaml</code> multilines.</p>
<p>So let's speculate about pipeline based on these materials.
Since we have specific context (script for <code>Gitlab CI</code>) then we don't need all of theese 63 cases:</p>
<pre><code>                      &gt;     |            &quot;     '     &gt;-     &gt;+     |-     |+
-------------------------|------|-----|-----|-----|------|------|------|------  
Trailing spaces   | Kept | Kept |     |     |     | Kept | Kept | Kept | Kept
Single newline =&gt; | _    | \n   | _   | _   | _   | _    |  _   | \n   | \n
Double newline =&gt; | \n   | \n\n | \n  | \n  | \n  | \n   |  \n  | \n\n | \n\n
Final newline  =&gt; | \n   | \n   |     |     |     |      |  \n  |      | \n
Final dbl nl's =&gt; |      |      |     |     |     |      | 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      | &quot;    | &quot;    | &quot;   | \&quot;  | &quot;   | &quot;    | &quot;    | &quot;    | &quot;
Backslash         | \    | \    | \   | \\  | \   | \    | \    | \    | \
&quot; #&quot;, &quot;: &quot;        | 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       |
</code></pre>
<p>Assumptions are:</p>
<ul>
<li>we don't need trailing spaces at all</li>
<li>we don't need final newlines at all</li>
<li>we'll try to avoid quote escaping mess</li>
<li>Backslash is intended to combine multi-line scripts</li>
<li>almost doesn't matter how many spaces before the script</li>
</ul>
<p>P.s. I'd suggest to lint your <code>.gitlab-ci.yml</code> using GitLab UI: <a href="https://docs.gitlab.com/ee/ci/lint.html" rel="nofollow noreferrer">Validate GitLab CI/CD configuration</a></p>

CC BY-SA 4.0
2020-03-12T07:18:07.413Z
Awesome, let me try this! Thank you a lot!
Chau Giang
CC BY-SA 4.0
2020-03-12T07:22:17.463Z
My pipeline says: Job succeeded, thanks you for your help!
Chau Giang
CC BY-SA 4.0
2020-03-12T07:01:56.513Z
2022-10-04T21:51:56.410Z
2022-10-04T21:51:56.410Z
Yasen
<p>I have a very long command in <code>.gitlab-ci.yml</code> file to ssh to jump host then use <code>rsync</code> to sync files from my repo to destination host (it does not have public ip so I need to access it via jump host).</p>

<p>Here is my <code>.gitlab-ci</code>:</p>

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


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

<p>The command here is so long:</p>

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

<p>Let me explain it:</p>

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

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

<gitlab>
Is there any way to break long command into multi lines in GitlabCI
Chau Giang
# Is there any way to break long command into multi lines in GitlabCI

(Asked by: username_0 on 2020-03-12)

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?

**Tags:** gitlab


---

## 1 answers

### Answer by username_1 on 2020-03-12. Score: 12 | Accepted answer

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: Validate GitLab CI/CD configuration

> **Comments:**
> **username_0**: Awesome, let me try this! Thank you a lot!  
> **username_0**: My pipeline says: Job succeeded, thanks you for your help!  

---

