简体   繁体   中英

Gitlab CI how to deploy the latest to a specific directory

I have two projects in Gitlab where one is a submodule (let's call repo "frontend-templates") of the other (let's call this repo "main"). I have set up a Gitlab CI build for the "frontend-templates" repo. The thing is that I don't need testing or building. I only need deploying for this CI in the needed directory. So, I registered a runner for "frontend-templates" project and added .gitlab-ci.yml to the root directory:

job_main:
   type: deploy
   script: echo "Do nothing"

When I push to my repository, the runner fetches the latest commit to the following directory:

/home/gitlab-runner/builds/6231b425/0/Gasimzada/frontend-templates

and just runs echo "Do nothing" .

Now, I want the runner to deploy the "tested" commit to the dev server, which is located in:

/var/www/myapp/submodules/frontend-templates

EDIT: I changed the script to

script: cd /var/www/myapp/submodules/frontend-templates && git pull

but I got an error saying:

cannot open /var/www/myapp/.git/modules/submodules/frontend-templates/FETCH_HEAD: Permission denied

This makes sense because gitlab-runner user does not have access to any directory in /var/www/myapp but it a problem for me because I want to run gulp after deploy, so it compiles necessary scripts after it pulls from the remote repository.

Should I give permission to the root directory of dev environment? Or is there another way to do this?

You can simply perform some form of deployment using the directory you are in. You can rename/delete the directory of the currently deployed code and copy the checkout code there ( rm -rf /var/www/myapp/submodules/frontend-templates && cp -r . /var/www/myapp/submodules/frontend-templates ) or you can use rsync to do the synchronization.

However, these are not atomic operations - they will leave your deployed code in an uncertain state while they are being performed and in a mess if they fail. I'd suggest your /var/www/myapp/submodules/frontend-templates is just a symlink to a directory containing the code:

/var/www/myapp/submodules
  | - 7348110b
  | - a03ed59a
  | - frontend-templates -> ./a03ed59a

You can name the code directories according to the commit hash. The job itself could then look something like this:

job_main:
   type: deploy
   script:
     - cp -r . /var/www/myapp/submodules/$CI_BUILD_REF
     - ln -s ./$CI_BUILD_REF /var/www/myapp/submodules/templink
     - mv -Tf /var/www/myapp/submodules/templink /var/www/myapp/submodules/frontend-templates

NOTE: Obviously, the runner will need necessary file permissions to perform the tasks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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