Copy a changed or new files using GitLab-CI

less than 1 minute read

When using GitLab-CI, it is needed that finding changed or new files, and then copy & paste the files to server sometimes. The job is pretty simple.

Environment

Windows Server

git

Fisrt, listing all changed, new files using git. And write a text file.

git diff --name-only --diff-filter=d HEAD~1 > copy-target.txt # exclude delete

Copy

Second, Copying a list of files. In ‘copy-target.txt’, there are lines of full path & name. Using xcopy, write a file named auto-copy.bat

@echo off
set src_folder=.\
set dst_folder=o:\target
for /f "tokens=*" %%i in (copy-target.txt) DO (
    xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)

GitLab-CI

Using bat files written above, set GitLab-CI script.

stages:
  - deploy

after_script:
  - 'net use /delete o:'

deploy-to-prod:
  stage: deploy
  environment:
    name: production server
  only:
    - master
  script:
    - echo 'deploy to production server'
    - 'net use o: \\server_ip\folder password /user:id'
    - 'git diff --name-only --diff-filter=d HEAD~1 > copy-target.txt'
    - 'auto-copy.bat'

Reference

Copy a list (txt) of files: https://stackoverflow.com/questions/6257948/copy-a-list-txt-of-files

Leave a comment