Is there any way to break long command into multi lines in GitlabCI
Since .gitlab-ci.yml is a Yaml file, then just use it's syntax.
For example, you may use &gt;:
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;

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:
                      &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       |

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