EDIT: this blog is now deployed and hosted via Firebase.
Long time no see!
Let’s see how I configured Gitlab CI to be able to deploy this blog on my private server. As already explained, I use deployer for deployments. Under the hood, it performs a simple rsync thanks to the upload task.
The basics
My CI workflow is quite simple. A test stage allows me to merge the PR, and a deploy stage is here to release a new version of this blog. The deploy stage is launched only on master, when a pull request is merged. It looks basically like this:
Install deployer on Gitlab CI
This is quite straightforward:
Access the server via SSH
This is the trickiest part. To be able to rsync the files from Gitlab CI to my server, the CI needs to be able to access this server.
For that, I use a dedicated SSH key. Its role is only to deploy on this server, and it has no passphrase. The SSH private key will be added to Gitlab CI’s SSH agent.
The env vars $DEPLOY_SSH_PRIVATE_KEY, $DEPLOY_PORT and $DEPLOY_HOSTNAME must be added to Gitlab CI (section Settings >> CI/CD >> Variables of your project). Whenever they can, those variables must be masked, so that they don’t appear in the jobs’ logs.
As we want to deploy only on master, all those variables can be protected. To protect the master branch, go to the section Settings >> Repository >> Protected Branches of your project. For your first tests, maybe you’ll want to deploy from your pull request, to see if everything goes well. In that case, do not protect those variables right now, do it once your deploy workflow is in place.
Use deployer to release
At the root of my repository, I put a .deployer.yml.dist file that looks like this:
I use this file as a template for the CI where I’ll replace all the _FOO_ patterns by the associated env var that I defined in my Gitlab project. Once done I can launch the deployment via php deployer.phar deploy production.
The env vars $DEPLOY_STAGE,$DEPLOY_PATH and $DEPLOY_USER are added, masked and protected to Gitlab CI exactly like explained previously for $DEPLOY_SSH_PRIVATE_KEY, $DEPLOY_PORT and $DEPLOY_HOSTNAME.
Install PHP dependencies
The last missing part of my deployment pipeline is to install PHP dependencies. On purpose, I have no JS or CSS to build.
To wrap up
My final .gitlab-ci.yml looks like this:
It works like a charm. Everytime a pull request is performed, this blog is automatically updated. Maybe this will help me to write more 😛