如何从Gitlab CI自动在Docker中部署到VPS

时间:2019-04-02 13:32:02

标签: docker continuous-integration gitlab gitlab-ci continuous-deployment

每当我在Gitlab中的项目中进行新提交时,我都试图在VPS中部署到Docker。但是我在这样做时遇到了问题。

我尝试先安装sshpass,然后再安装scp文件夹和文件。但这是在说:

sshpass: Failed to run command: No such file or directory

我正在尝试从build阶段获得的文件夹和文件,因此我不必再次构建我的应用程序。

这是我的.gitlab-ci.yml文件:

image: node:9.6.1

cache:
  paths:
    - node_modules/
    - build/
    - docker-compose.yml
    - Dockerfile
    - nginx.conf

stages:
  - build
  - test
  - dockerize

build-stage:
  stage: build
  script:
    - npm install
    - CI=false npm run build
  artifacts:
    paths:
      - build/
      - docker-compose.yml
      - nginx.conf

test-stage:
  stage: test
  script:
    - npm install
    - CI=false npm run test

dockerize-stage:
  stage: dockerize
  image: tmaier/docker-compose:latest
  services:
    - docker:dind
  dependencies:
    - build-stage
  tags:
    - docker  
  script:
    - apk update && apk add sshpass
    - sshpass -V
    - export SSHPASS=$USER_PASS
    - ls -la
    - sshpass -e ssh -o stricthostkeychecking=no root@ip:/home mkdir new-super-viva
    - sshpass -e scp -o stricthostkeychecking=no -r build user@ip:/home/new-folder
    - sshpass -e scp -o stricthostkeychecking=no -r docker-compose.yml user@ip:/home/new-folder
    - sshpass -e scp -o stricthostkeychecking=no -r nginx.conf user@ip:/home/new-folder
    - sshpass -e ssh -o stricthostkeychecking=no user@ip:/home/new-folder docker-compose up --build

这是Gitlab CI的实际输出:

$ sshpass -V
sshpass 1.06
(C) 2006-2011 Lingnu Open Source Consulting Ltd.
(C) 2015-2016 Shachar Shemesh
This program is free software, and can be distributed under the terms of the GPL
See the COPYING file for more information.

Using "assword" as the default password prompt indicator.
$ export SSHPASS=$USER_PASS
$ ls -la
total 912
drwxrwxrwx    7 root     root          4096 Apr  2 13:24 .
drwxrwxrwx    4 root     root          4096 Apr  2 13:24 ..
-rw-rw-rw-    1 root     root           327 Apr  2 13:24 .env
drwxrwxrwx    6 root     root          4096 Apr  2 13:24 .git
-rw-rw-rw-    1 root     root           329 Apr  2 13:24 .gitignore
-rw-rw-rw-    1 root     root          1251 Apr  2 13:24 .gitlab-ci.yml
-rw-rw-rw-    1 root     root           311 Apr  2 11:57 Dockerfile
-rw-rw-rw-    1 root     root          2881 Apr  2 13:24 README.md
drwxrwxrwx    5 root     root          4096 Apr  2 13:20 build
-rw-rw-rw-    1 root     root           340 Apr  2 13:24 build.sh
-rw-rw-rw-    1 root     root           282 Apr  2 11:57 docker-compose.yml
-rw-rw-rw-    1 root     root          1385 Apr  2 11:57 nginx.conf
drwxr-xr-x 1191 root     root         36864 Apr  2 13:22 node_modules
-rw-rw-rw-    1 root     root        765929 Apr  2 13:24 package-lock.json
-rw-rw-rw-    1 root     root          1738 Apr  2 13:24 package.json
drwxrwxrwx    4 root     root          4096 Apr  2 13:24 public
drwxrwxrwx   10 root     root          4096 Apr  2 13:24 src
$ sshpass -e ssh -o stringhostkeychecking=no user@ip:/home mkdir new-folder
sshpass: Failed to run command: No such file or directory
ERROR: Job failed: exit code 3

有什么方法可以将build的文件夹docker-compose.ymlnginx.confbuild-stage复制到dockerize-stage,然后用{{ 1}}到VPS?甚至无法使用sshpass创建文件夹。我还尝试手动创建文件夹,然后从mkdir删除此命令,但输出仍然相同。

请注意,我在以下位置添加了.gitlab-ci.ymlUSER_PASS输入环境变量,然后将其https://gitlab.com/user/project/settings/ci_cd

0 个答案:

没有答案