Continuous Integration in Gitlab CI - CD With Drupal 8
Continuous Integration in Gitlab CI - CD With Drupal 8
ADCI Solutions Follow
Nov 6, 2018 · 5 min read
By ADCI Solutions
https://codeburst.io/continuous-integration-in-gitlab-ci-cd-with-drupal-8-66d28a6fc3fc 1/10
28/01/2019 Continuous Integration in Gitlab CI/CD with Drupal 8
If you are using the Gitlab platform for your project, it already has a
built-in CI/CD support. Together with free private repositories, a bug
tracker, and other features, it makes the cloud-based service useful for
both storing code and also for testing and deploying. You can also learn
how to use the Gitlab pages service for your project documentation in
one of our previous articles.
In this article, we will look at the con guration of the pipeline using
Gitlab, con gure necessary les, learn how to build application and run
unit tests on a separate docker image, as well as automatically send the
built application to a staging or production server. And all of this we
can do just using a couple of lines in the con guration le! Sounds
interesting? Let’s start!
. . .
Con guring GitLab continuous delivery
First of all, you need 3 things to start using Gitlab CI:
Gitlab Runner is the project that is used for execution of jobs described
in the .gitlab-ci.yml le and also it communicates with Gitlab using
its own API.
After we’ve nished with the runner, let’s take a closer look at the
deployment process. The pipeline of Gitlab consists of several stages, by
default it is build, test, and deploy, each of them consists of 1 or more
CI jobs. Jobs in Gitlab are independent tasks that can be performed in
parallel. After completing all the jobs at one stage, the runner starts to
execute the next stage.
. . .
https://codeburst.io/continuous-integration-in-gitlab-ci-cd-with-drupal-8-66d28a6fc3fc 2/10
28/01/2019 Continuous Integration in Gitlab CI/CD with Drupal 8
Build and test stage
Let’s create the .gitlab-ci.yml le in the root of the project and
specify our custom stages:
1 stages:
2 - build
3 - deploy
For the build stage, we create a build job that installs our Drupal 8
project vendor dependencies using Сomposer and launch our custom
tests.
1 build:
2 stage: build
3 image: tetraweb/php:7.1
We can also specify several jobs with di erent versions of PHP and test
our application for di erent versions.
1 before_script:
2 - apt-get update
3
4 #install additional gd extension, that is required b
5 - apt-get install libpng-dev -y
6 - docker-php-ext-install gd
7
8 #install composer
9 - apt-get install zip unzip
10 - php -r "copy('https://getcomposer.org/installer',
11 php composer setup php
Now after we have installed Сomposer, in the script section we run the
building of our project.
https://codeburst.io/continuous-integration-in-gitlab-ci-cd-with-drupal-8-66d28a6fc3fc 3/10
28/01/2019 Continuous Integration in Gitlab CI/CD with Drupal 8
1 script:
2 - php composer.phar install
3
4 #remove unnecessary files
f i
After the successful completion of the job, we can attach resulting les
to it, this is called an artifact.
For the artifact, we can specify its name, duration of storage and other
settings, as well as a list of les to be added.
1 artifacts:
2 name: "test_project_{CI_COMMIT_SHA}"
3 expire_in: '1 week'
4 paths:
In case we want to run the job only for commits from a speci c branch,
you can specify it in the section directive ‘only’.
1 only:
2 - master
https://codeburst.io/continuous-integration-in-gitlab-ci-cd-with-drupal-8-66d28a6fc3fc 4/10
28/01/2019 Continuous Integration in Gitlab CI/CD with Drupal 8
1 build:
2 stage: build
3 image: tetraweb/php:7.1
4
5 before_script:
6 - apt-get update
7
8 #install additional gd extension, that is required b
9 - apt-get install libpng-dev -y
10 - docker-php-ext-install gd
11
12 #install composer
13 - apt-get install zip unzip
14 - php -r "copy('https://getcomposer.org/installer',
15 - php composer-setup.php
16 - php -r "unlink('composer-setup.php');"
17
18 #install composer package to run parallel tasks
19 - composer -n global require -n "hirak/prestissimo"
20
21 script:
22 - php composer.phar install
If we want to run a unit test for the built application, let’s add the
following line to the end of the script directive:
So, after pushing the con guration le into the master branch of your
repository, go to the CI/CD-pipelines at the project page and you will
see that our task runner automatically began to perform the pipeline. If
we do not want to run it automatically, we can add the following
directive.
1 when: manual
i i ❤ i view raw
https://codeburst.io/continuous-integration-in-gitlab-ci-cd-with-drupal-8-66d28a6fc3fc 5/10
28/01/2019 Continuous Integration in Gitlab CI/CD with Drupal 8
. . .
Deploy stage
Okay, we’ve installed dependencies for our application and have run
out some of the unit tests, let’s try to deploy it into the stage
environment, for example, for the user acceptance testing.
https://codeburst.io/continuous-integration-in-gitlab-ci-cd-with-drupal-8-66d28a6fc3fc 6/10
28/01/2019 Continuous Integration in Gitlab CI/CD with Drupal 8
1 deploy_dev:
2 stage: deploy
3 environment:
4 name: dev
5 only:
6 - master
7 image: tetraweb/php:7.1
8 before_script:
9 #install ssh agent
10 - 'which ssh-agent || ( apt-get update -y && apt-get
11 - mkdir -p ~/.ssh
12 - eval $(ssh-agent -s)
13 - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrict
14
15
16 script:
17 - ssh-add <(echo "$STAGING_KEY")
18 - ssh -p22 server_user@server_host " <<EOF
19 - cd /var/www/html/my-drupal-site/web/
20 - drush sql:dump --result-file=../../backups/${CI_B
21 - mkdir /var/www/html/_tmp
22 - EOF
In this script, we’ve installed the ssh-agent on the Docker container and
disabled StrictHostKeyChecking in the ssh con gs in the
before_script block. After that, we can log in to the stage
environment server via the ssh.
There are several ways to copy project les to the server, in this case, we
copy them to the tmp folder via SCP, then we replace the folders and
move the con g le settings.env.php to the working directory. We
also created a backup of a database, ran import of the Drupal con g
les and database updates.
. . .
Afterword
In this article, we’ve learned how to use Gitlab CI/CD in our project and
wrote the necessary con g les. Of course, for the real project, you will
want to add more stages and jobs, this is just a very basic example how
to use this service. I hope you will start using a continuous delivery
approach in your project and make your deploy process easier.
. . .
https://codeburst.io/continuous-integration-in-gitlab-ci-cd-with-drupal-8-66d28a6fc3fc 7/10
28/01/2019 Continuous Integration in Gitlab CI/CD with Drupal 8
. . .
. . .
How to conduct a website audit: a beginner’s
guide
Hello there! This little website audit checklist will
get you directly to the topic itself. Why audit you…
itnext.io
https://codeburst.io/continuous-integration-in-gitlab-ci-cd-with-drupal-8-66d28a6fc3fc 8/10
28/01/2019 Continuous Integration in Gitlab CI/CD with Drupal 8
SHOW EMBED
https://codeburst.io/continuous-integration-in-gitlab-ci-cd-with-drupal-8-66d28a6fc3fc 9/10
28/01/2019 Continuous Integration in Gitlab CI/CD with Drupal 8
https://codeburst.io/continuous-integration-in-gitlab-ci-cd-with-drupal-8-66d28a6fc3fc 10/10