3 minute read

GitLab CI/CD

Test and deploy an application using GitLab CI.

In this post we will setup GitLab CI/CD, so you can run your unit tests on every push to GitLab and automatically deploy them to DeltaBlue cloud.

Checkout the previous post if you want to learn more about adding PHPUnit Testing to your project.

Assuming you are using GitLab to store your repository and you have added PHPUnit tests, which you can run by executing vendor/bin/simple-phpunit, you are ready to setup an automated test and deployment flow. ( Continuous Integration / Continuous deployment )

To do this, add a .gitlab-ci.yaml file to your project root directory, with following contents:

  - testing

  stage: testing
    - vendor/bin/simple-phpunit

    - vendor/
    - var/

  - sudo apt-get install php-curl -y

  # Install composer dependencies
  - wget -O - -q | tr -d '\n' > installer.sig
  - php -r "copy('', 'composer-setup.php');"
  - php -r "if (hash_file('SHA384', 'composer-setup.php') === file_get_contents('installer.sig')) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
  - php composer-setup.php
  - php -r "unlink('composer-setup.php'); unlink('installer.sig');"
  - php composer.phar install

The presence of this file will trigger a pipeline in GitLab. Read all about setting up GitLab CI/CD in the documentation

Our config file above will trigger a pipeline with one stage (testing, we will add deploy later). Before the stages are executed, the before_script is executed, where we will not only add necessary packages with composer, but also add php-curl, as it is fairly commonly used in PHPUnit testing.

At this moment, when we commit to the current branch, PHPUnit testing should already be executed.

Next, go over to your project in DeltaBlue Cloud and setup a “Deploy Application” scenario. At its most basic form, this scenario should contain three actions:

  • Git Clone
  • Composer - Run composer install
  • Symlink release to current

Save your scenario, then click the HOOKS-button to generate a webhook URL. A scenario webhook is a URL that can be called to execute the scenario. In this case, when calling the freshly created hook, the deployment scenario will be executed. Copy paste the webhook URL and add it to the .gitlab-ci.yaml as follows:

  - testing
  - deploy

  stage: deploy
    - wget<-- your token -->/

And that’s it.

Every time you push commits to GitLab, your code will get PHPUnit tested. If your tests succeed the deployment scenario will deploy the tested code to DeltaBlue Cloud.

It is quite possible that you don’t want this to happen on the master branch, but only on development or staging: Easy, as the gitlab-ci file is commited and thus can be different in every branch, you can configure whether to deploy or not, or to deploy to another webhook URL.

Want to find out what we can do for you?