Gasim Gasim - 1 year ago 294
Git Question

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:

type: deploy
script: echo "Do nothing"

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


and just runs
echo "Do nothing"

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


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
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?

Answer Source

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:

  | - 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:

   type: deploy
     - 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download