Objective
Enable users to understand and follow the necessary steps to utilize Gitlab Runner.
Prerequisites
- Gitlab account
- A project that is currently stored on Gitlab
Steps
Step 1: Create a Docker Installation File
NOTE: This is only applicable if the application is running in a Docker environment.
On your Gitlab project repository, select the ‘+’ icon.
Select New File.
Name the file as docker_install.sh
Add the following content: –
#!/bin/bash
# We need to install dependencies only for Docker
[[ ! -e /.dockerenv ]] && exit 0
set -xe
# Install git (the php image doesn’t have it) which is required by composer
apt-get update -yqq
apt-get install git -yqq
# Install phpunit, the tool that we will use for testing
curl –location –output /usr/local/bin/phpunit
https://phar.phpunit.de/phpunit.phar
chmod +x /usr/local/bin/phpunit
# Install mysql driver
# Here you can install any other extension that you need
docker-php-ext-install pdo_mysql
Enter a Commit message.
Specify a Target Branch
Select Commit changes
Task 2: Create a PHPUNIT File
On your Gitlab project repository, select the ‘+’ icon.
Select New File.
Name the file as phpunit_myapp.xml
Add the following content: –
<?xml version=”1.0″ encoding=”UTF-8″?>
<phpunit>
<testsuites>
<testsuite name=”mysuitetest”>
<directory suffix=”.php”>./</directory>
</testsuite>
</testsuites>
</phpunit>
Enter a Commit message.
Specify a Target Branch
Select Commit changes
Step 3: Create a .Gitlab-ci.yml File
On your Gitlab project repository, select the ‘+’ icon.
Select New File
Select .gitlab-ci.yml as the template
Name the file as .gitlab-ci.yml as the template
Add the following content: –
#Select image from https://hub.docker.com/_/php
image: php:latest
before_script:
# Install dependencies
– bash docker_install.sh > /dev/null
test:app:
script:
– /usr/local/bin/phpunit –configuration phpunit_myapp.xml
Enter a Commit message.
Specify a Target Branch
Specify a Target Branch
Step 4: Execute the Runner
Under CI/CD, select Pipelines.
Select Run Pipeline
Run for > target branch (e.g. master)
Select Run Pipeline
Wait for several minutes, you should be greeted with a ‘Job Succeeded’ message