Connect to Open VPN during Gitlab Pipeline
Gitlab CI/CD offers the possibility to create a pipeline, which runs when something changes in the repository. A pipeline consists of one or more stages that run in order and in these stages, for example, it is possible to build the project, run the tests, create the artifacts, etc. For more information about Gitlab CI/CD, I suggest you look over the documentation.
These out-of-the-box solutions really simplify the work to be done to have a CI up and running. For example, this is the configuration file (a file called
.gitlab-ci.yml placed at the repository’s root) needed for running all the tests of a Kotlin project.
But, let’s assume that the project is using some libraries that are published on a private Maven repository behind a VPN. The pipeline will fail because it can’t download the dependencies.
To connect to a VPN, it is necessary to do some tweaks before starting the stages of the pipeline. And it is possible to do it by writing the commands inside the
For this example, I will use OpenVPN but the script can be adapted for whatever type of VPN.
Before writing any code, it is necessary to write some secret variables (the menu is available under Settings > CI/CD > Variables - here for more info). Three variables are necessary:
- CLIENT_OVPN -> the content of the .ovpn file
- VPN_USER -> the VPN user
- VPN_PWD -> the VPN password
First of all, some dependencies are needed
Then the secrets need to be loaded:
Now, the connection can be performed:
To check that everything is ok I make a 30 seconds sleep (yes, it’s brutal but it works) and then I ping the server:
And that’s it! Now the pipeline can download all the dependencies, even the ones under VPN.
For reference, here’s the complete