2

I am using CD for deploying my code to a VPS. This VPS is running ubuntu 16.04 and has a user 'deployer'.

Now when I use ssh deployer@server I get shell access to the server and then when using cd /var/www I get into the /var/www directory.

When I do this from the deployment script, defined in .gitlab-ci.yml I get this error /bin/bash: line 101: cd: /var/www/data/: No such file or directory. I also did ls -al to view the directory structure of /var which turned out not to contain the www directory. So clearly now I have no permission to the www directory.

- rsync -avz --exclude=.env . deployer@devvers.work:/var/www/data/staging/home - ssh deployer@devvers.work - cd /var - ls -al - cd /var/www Tthis is the part of the script where it fails. Does anyone know why my user has different permissions when using ssh from the terminal then when using ssh in this script? Coping the files with rsync when fine and all the files were copied.

Wybren Kortstra
  • 123
  • 1
  • 8

3 Answers3

1

It is best to use an ssh executor, configured through a config.toml:

/etc/gitlab-runner/config.toml:

concurrent = 1

[[runners]]
  url = "http://your_gitlab/ci"
  token = "xxx..."
  name = "yourGitLabCI"
  executor = "ssh"
  [runners.ssh]
    user = "deployer"
    host = "devvers.work"
    port = "22"
    identity_file = "/home/user/.ssh/id_rsa"

Then you .gitlab.yml can simply include

job:
script:
    - "ls /var/www"
    - "cd /var/www"
    ...

See also this example.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

My guess is that the cd and ls commands that you are trying are actually executed in the runner's environment (be it the host or a docker container, depending on your setup), not on the machine you ssh into.

I'd suggest you rather execute those commands with ssh. An example of creating a file and checking that it has been created:

ssh deployer@devvers.work "touch /var/www/test_file && ls -al /var/www/"
tmt
  • 7,611
  • 4
  • 32
  • 46
  • We're using this solution, but a little bit different. `ssh "deployer@devvers.work" "cd /var/www/data/staging/; other_command; other_command;` – Wybren Kortstra Mar 14 '17 at 12:40
  • 1
    Yep, that's also possible. Just remember that when using `&&` any command is dependent on the success of the previous one which is what you might need in your case, e.g. with `ssh "deployer@devvers.work" "cd /var/www/data/staging/ && first_command && second_command"` the `second_command` would not be run in case the `first_command` fails. – tmt Mar 14 '17 at 13:13
0

If you encounter the line 101: cd: issue on a gitlab-runner that is configured as a shell executor there might actually be a .bash_logout file in the gitlab-runner users home directory that causes the issue together with https://gitlab.com/gitlab-org/gitlab-runner/issues/3849

bigbear3001
  • 522
  • 8
  • 19