Santhosh
Santhosh

Reputation: 11788

how to store multiline string in a variable in gitlab yaml

I am trying to store some multiline strings like ssh private key in a variable in .gitlab-ci.yml file

I have a ssh private key

-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAriS0+ayniFDo+y7GcNb62mupuM4EFBX+gdCEr/K8wZd/mFxY
mSGoVX2CdCDy6k+LSWbBBFtus575187fHNy045gJzI5D0PSGmPsWYqu/Rst43Qgr
R6CAOBqapM9LVBopH0pjPTv5/8iJA/G+Mw6MXGzQAAuA0ZyJoDy7d3GSVKG+k/k0
smZ+FKjDxZ3gn/e3nc5XABSywZlT91RHT6BImrxizvgHkkks1J8RBl9QoIvMSiwD
7sEElAABAoGBAN7kSADAK5vtqvFQsMAPEJLXdv5mP0IEe3IamL3MNlUYpb7yGqBr
6Jmopg+vpbkXZeMpC6uYu9F5Y6etV8yslTERxhP3yE+Fg2FFXc53jDOzkkCu+aey
PypGWE9hZXK5Wx9rqi83b9d59bCcMY5kouDSUNTmYVC3H+OxuX+2hgnJAoGBAMgC
toxjVLp+gDhRUqxe1MuTznq4GGzXB9/LuGYI078pHlYjHmWIKfHtKv5O6HqYz2pi
8mMNqt0vZC+Wbt5kZnE3Om1CgEvAPsxMUaAWq7iXSfEhqRvfwE5uJwkSxiTnRRWH
4tzSjGCP3+GqVYxUliELvwMdS1Qbo103lwVgoR4iLSrr/vWTb5NG3ms=...
-----END RSA PRIVATE KEY-----

and the output of ssh-keyscan domain.com

# xx.xx.xx.xx:22 SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.5
xx.xx.xx.xx ssh-rsa AAAX20sVRmUkgOn8Db46ikEhC2zBZYZ7Lv2AsaHV+isNB3QYfNKGDEMj9CvWByezJR/3DqCgK/IGLH0JfMZK9H9HR7/P1aeY7bVAg07Gdt/vYrgQtaNtkdK3qD/C0oUSQAE1a2vOzBGx1HZgtORTh8eN/h5bKW0/2GVlS+9K1MR7peECpgOBo3fu2RPwB/KFlUcrtZkdE8Em/thbXFWE/VGBwh/eoNB1
# xx.xx.xx.xx:22 SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.5
xx.xx.xx.xx ecdsa-sha2-nistp256 LhpT93b8rE/exS/M9QyoyjoeEuioXhffu62Ayxxl4UewOuASHaF4ylQIJSNlK+groH6Iv1QvPVKFe/n1uF1Xvk=
# xx.xx.xx.xx:22 SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.5
xx.xx.xx.xx ssh-ed25519 AAAAC3Nz/O/Dod2CitLhIqnePT51H

I know these can be stored in the gitlab CI/CD variables

but how do i store then in the yml file.

like

image: creatiwww/docker-compose:latest

services:
  - docker:dind

variables:
  ssh_private_key: <private key text here but its multiline>
  ssh_known_hosts: <out put of ssh-keyscan which is also multiline>

Upvotes: 14

Views: 16583

Answers (3)

NeVraX
NeVraX

Reputation: 492

Using > works but does not keep newlines.

| is probably what anyone coming here is searching for.

Here is an example for both :

multi-lines-variables-test:
  variables:
    MULTI_WITHOUT_CR: >-
      Here is the first line
      And here is the second
    MULTI_WITH_CR: |-
      Here is the first line
      And here is the second
  script:
    - echo "$MULTI_WITHOUT_CR"
    - echo "$MULTI_WITH_CR"

which prints :

$ echo "$MULTI_WITHOUT_CR"
Here is the first line And here is the second
$ echo "$MULTI_WITH_CR"
Here is the first line
And here is the second

Notice the double quotes around the variables in the echo lines. If you omit them, the result will be on a single line, because lines will be considered multiple arguments for echo.

Edit : better, >- and |- strip the trailing new line. Taken from here : https://stackoverflow.com/a/21699210/10239040

Upvotes: 29

youyang yu
youyang yu

Reputation: 1

CAUTION: Caution: Variables with multiline values are not currently supported due to limitations with the current Auto DevOps scripting environment.

this way resolve my problem.

script:
    - |
      cat >  .golangci.yml <<- 'EOF'
      run: 
        timeout: 5m
      EOF

script suport multiline string.

Upvotes: -2

MrTux
MrTux

Reputation: 34003

Don’t put an RSA private key or host keys that might change into the .gitlab-ci.XML file.

Use GitLab variables instead that can be configured on the project or group level.

https://docs.gitlab.com/ee/ci/variables/

Upvotes: 2

Related Questions