patternModerate
Is there any way to break long command into multi lines in GitlabCI
Viewed 0 times
multiintoanygitlabcilongwaycommandtherebreaklines
Problem
I have a very long command in
Here is my
The command here is so long:
Let me explain it:
I just wonder if there is any way to break long command above to multi lines command?
.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
sshtohostthen installrsyncif it does not exist, then it ssh to my private server192.168.1.2and then sync my file named.gitlab-ci.yml(the file from my repo in my Gitlab server) to foldertest
I just wonder if there is any way to break long command above to multi lines command?
Solution
Since
For example, you may use
Sample usage of
There is a great resource about
Also, there is a great SO answer about
So let's speculate about pipeline based on these materials.
Since we have specific context (script for
Assumptions are:
P.s. I'd suggest to lint your
.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 multilinesThere is a great resource about
yaml multilines: YAML Multiline StringsAlso, 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 configurationCode Snippets
> | " ' >- >+ |- |+
-------------------------|------|-----|-----|-----|------|------|------|------
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 |Context
StackExchange DevOps Q#11056, answer score: 12
Revisions (0)
No revisions yet.