Home > Back-end >  Copy file to remote server from gitlab ci
Copy file to remote server from gitlab ci

Time:09-18

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.

CodePudding user response:

Use 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}/

CodePudding user response:

Alternatively (to rsync), use scp (which, since ssh package is installed, should come with it)

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

That way, no additional apk update/add needed.

  • Related