Run some commands inside of remote machine - Gitlab CI

Hello everybody.
I’ve created this .gitlab-ci.yaml for deploy.

stages:
  - deploy

image: docker:19.03.12

variables:
  DOCKER_HOST: tcp://docker:2375
  DOCKER_TLS_CERTDIR: ""

deploy:
  stage: deploy
  image: alpine
  before_script:
    - 'command -v ssh-agent >/dev/null || ( apk add --update openssh-client )'
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY_STAGING" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan -p $SSH_PORT_STAGING $SSH_IP_STAGING >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts

  script:
    - ssh -p "$SSH_PORT_STAGING" deploy@$SSH_IP_STAGING
    - ls -al
    #- echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER $CI_REGISTRY --password-stdin

All works fine, however, the second command in the “script” section, that’s returned the directory list inside of the container. It does not show the directory list of the user of the remote machine.

How I can run some commands in the remote machine without starting calling each time ssh command?

I think you probably want to change the first line to something like:

script:
    - ssh -p "$SSH_PORT_STAGING" deploy@$SSH_IP_STAGING 'ls -al'

and run the command directly via SSH.

1 Like

Hi @snim2,
Thanks for your reply, I thought that was so.

1 Like