首页 > 解决方案 > 如何在 gitlab Ci/CD 中运行远程命令?

问题描述

我有以下 .gitlab-ci.yml 文件,用于托管在 gitlab.com 上的 gitlab 存储库,以使用 gitlab CI/CD 构建和部署 vue 应用程序:

image: node:latest

cache:
  paths:
    - node_modules/

before_script:
 - apt-get update -qq && apt-get install -y -qq sshpass

build_deploy_stage:
  stage: build
  environment: Staging
  only:
    - master
  script:
    - rm ./package-lock.json
    - npm install
    - node -v
    - npm run build  
    - ls
    - sshpass -V
    - export SSHPASS=$USER_PASS 
    - sshpass -e scp -o stricthostkeychecking=no -r dist user@example.com:~/myapp

部署后,我想像远程登录一样在命令下运行 (ssh user@example.com):

$ pm2 重启 myapp

$ cp ~/myfiles/file1 ~/backup/myfiles/file1.bk

如何将上述命令添加到 .gitlab-ci.yml ?

推荐的方法是使用带有公钥的 ssh 而不是 sshpass。使用 ssh 密钥对并登录远程主机(example.com)并从 gitlab CI/CD 运行器中在远程主机上运行命令的解决方案是什么?

标签: vue.jsgitlabgitlab-ci-runner

解决方案


推荐阅读