Gitlab allows you to create a CI/CD configuration to test your application or tool.
To enable the pipelines you'll need to create a file .gitlab-ci.yml
in your project root directory.
Here is a simple example on how a basic configuration would look like for a php project:
stage_name:
image: registry.gitlab.com/kisphp/ci-registry/php7.4:latest
before_script:
- composer install --no-interaction
script:
- vendor/bin/phpunit
The above configuration will use the docker image provided by the image
keyword, will download the project, then will run composer install
without to require any interaction from user. After that, the scripts are executed. Both in before_scripts
and in scripts
you can add multiple items.
Define pipeline stages:
stages:
- build
- test
- code
- deploy
Enable vendor
and node_modules
cache that will be distributed between pipeline stages
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
- vendor/
For this to work, you need that the first stage to generate both directories. If one fails to be created, for example a composer or a npm error, the cache will not be created
Run tests with codeception and a MySQL database:
image: registry.gitlab.com/kisphp/ci-registry/php7.4:latest
variables:
MYSQL_DATABASE: database_name
MYSQL_ROOT_PASSWORD: root_user_password
GIT_DEPTH: "1"
codeception:
stage: test
services:
- alias: "mysql"
name: "registry.gitlab.com/kisphp/ci-registry/mysql8.0:latest"
before_script:
- bash .gitlab-ci.sh
- php bin/console setup:install
- php bin/console demo:data
script:
- vendor/bin/codecept run --fail-fast
Run code fixer
code-fixer:
stage: code
before_script:
- rm composer*
- composer require friendsofphp/php-cs-fixer
script:
- vendor/bin/php-cs-fixer fix -v --dry-run
deploy_live:
stage: deploy
script:
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
- eval $(ssh-agent -s)
# this key must be set as variable in gitlab config page for the project
- ssh-add <(echo "$SSH_PRIVATE_KEY")
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
- ssh web@www.example.com -t "cd /home/server && ./dep.sh example.com $CI_COMMIT_SHA"
environment:
name: production
url: http://www.example.com # this url will be available in environments page in gitlab
variables:
# this will not clone the repository itself (useful in big repositories)
GIT_STRATEGY: none
# when: manual
only:
- master