3

I want to execute a command like below in the gitlabl CI/CD

ssh $DEPLOY_USER@$DEPLOY_HOST <<'ENDSSH'                                                                    
set -x -o verbose;
execute some command here
set +x
ENDSSH

How to add such commands in script

deploy-to-stage:
  stage: deploy
  before_script:
    - "which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )"
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN registry.gitlab.com
  script:
    -   *** HERE I WANT TO RUN THAT COMMAND ***

How can i do that

Santhosh
  • 9,965
  • 20
  • 103
  • 243

2 Answers2

7

you can do it like that:

script:
  - |
    ssh $DEPLOY_USER@$DEPLOY_HOST <<'ENDSSH'                                                                    
    set -x -o verbose;
    execute some command here
    set +x
    ENDSSH
Makariy
  • 725
  • 7
  • 16
0

END_TEXT works as well. As documented by Gitlab

https://docs.gitlab.com/ee/ci/yaml/script.html#split-long-commands

  script:
    - |
      ssh user@server.com << END_TEXT
      echo "connected to `hostname` as $USER";
      cd ~/project_folder &&
      git fetch &&
      git log -1 --oneline &&
      git reset --hard origin/master &&
      pnpm install --frozen-lockfile &&
      pnpm run build &&
      sudo service nginx reload
Gianfranco P.
  • 10,049
  • 6
  • 51
  • 68