简体   繁体   English

将文件从 gitlab ci 复制到远程服务器

[英]Copy file to remote server from gitlab ci

I need to copy one file from the project to the server where the application will be deployed.我需要将一个文件从项目复制到将部署应用程序的服务器。 This must be done before deploying the application.这必须在部署应用程序之前完成。

At the moment, I can connect to the server and create the folder I need there.目前,我可以连接到服务器并在那里创建我需要的文件夹。 But how to put the necessary file there?但是如何把必要的文件放在那里呢?

The stage in which this should be done.应该执行此操作的阶段。

deploy_image:
  stage: deploy_image
  image: alpine:latest
  services:
    - docker:20.10.14-dind
  before_script:
    - chmod og= $ID_RSA
    - apk update && apk add openssh-client
    - ssh -i $ID_RSA -o StrictHostKeyChecking=no root@$SERVER_IP \
      docker login -u $REGISTER_USER -p $REGISTER_PASSWORD $REGISTER
  script:
    - ssh -i $ID_RSA -o StrictHostKeyChecking=no root@$SERVER_IP \
      mkdir $PROJECT_NAME || true
    - ssh -i $ID_RSA -o StrictHostKeyChecking=no root@$SERVER_IP \
      cd $PROJECT_NAME
    # here you need to somehow send the file to the server
  after_script:
    - ssh -i $ID_RSA -o StrictHostKeyChecking=no root@$SERVER_IP docker logout
    - ssh -i $ID_RSA -o StrictHostKeyChecking=no root@$SERVER_IP exit
  only:
    - main

Help me please.请帮帮我。

Use rsync :使用rsync

# you can install it in the before_script as well
apk update && apk add rsync

rsync -avx <local files> root@${SERVER_IP}/${PROJECT_NAME}/

Alternatively (to rsync ), use scp (which, since ssh package is installed, should come with it)或者(对于rsync ),使用scp (由于ssh package 已安装,应该附带它)

scp <local files> root@${SERVER_IP}/${PROJECT_NAME}/

That way, no additional apk update/add needed.这样,不需要额外的apk update/add

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM