engelsystem/.gitlab-ci.yml

153 lines
3.9 KiB
YAML

image: php
variables:
DOCKER_DRIVER: overlay2
TEST_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
RELEASE_IMAGE: $CI_REGISTRY_IMAGE:latest
MYSQL_DATABASE: engelsystem
MYSQL_USER: engel
MYSQL_PASSWORD: engelsystem
MYSQL_HOST: mariadb
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
DOCROOT: /var/www/
stages:
- build
- test
- release
- deploy
.docker_template: &docker_definition
image: docker:latest
services:
- docker:dind
tags:
- dind
before_script:
- docker login -u gitlab-ci-token -p "$CI_JOB_TOKEN" "$CI_REGISTRY"
build-image:
<<: *docker_definition
stage: build
script:
- docker build --pull -t "$TEST_IMAGE" -f contrib/Dockerfile .
- docker push "$TEST_IMAGE"
build-image.nginx:
<<: *docker_definition
stage: build
script:
- docker build --pull -t "$TEST_IMAGE.nginx" -f contrib/nginx/Dockerfile .
- docker push "$TEST_IMAGE.nginx"
test:
image: $TEST_IMAGE
stage: test
services:
- mariadb:10.2
artifacts:
name: "${CI_JOB_NAME}_${CI_JOB_ID}"
expire_in: 1 week
when: always
paths:
- ./coverage/
coverage: '/^\s*Lines:\s*(\d+(?:\.\d+)?%)/'
before_script:
- apk add $PHPIZE_DEPS && pecl install xdebug && docker-php-ext-enable xdebug
- apk add mariadb-client
- mysql -h "$MYSQL_HOST" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE" < db/install.sql
- mysql -h "$MYSQL_HOST" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE" < db/update.sql
- curl -sS https://getcomposer.org/installer | php -- --no-ansi --install-dir /usr/local/bin/ --filename composer
- cp -R tests/ phpunit.xml "${DOCROOT}"
- HOMEDIR=$(pwd)
- cd "${DOCROOT}"
- composer --no-ansi install --dev
script: vendor/bin/phpunit -v --colors=never --coverage-text --coverage-html "${HOMEDIR}/coverage/"
release-image:
<<: *docker_definition
stage: release
script:
- docker pull "$TEST_IMAGE"
- docker tag "$TEST_IMAGE" "$RELEASE_IMAGE"
- docker push "$RELEASE_IMAGE"
only:
- master
release-image.nginx:
<<: *docker_definition
stage: release
script:
- docker pull "$TEST_IMAGE.nginx"
- docker tag "$TEST_IMAGE.nginx" "$RELEASE_IMAGE.nginx"
- docker push "$RELEASE_IMAGE.nginx"
only:
- master
.deploy_template: &deploy_definition
stage: deploy
image: $TEST_IMAGE
before_script:
- apk add bash rsync openssh-client
.deploy_template_script:
# Configure SSH
- &deployment_ssh |-
mkdir -p ~/.ssh
echo "$SSH_PRIVATE_KEY" | sed -e 's/\r//g' > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
# Install project and dependencies
- &deployment_dependencies |-
chmod +x ./deploy.sh
cp ./deploy.sh "${DOCROOT}/deploy.sh"
cd "${DOCROOT}"
build-release-file:
<<: *deploy_definition
stage: deploy
artifacts:
name: "release_${CI_COMMIT_REF_SLUG}_${CI_JOB_ID}_${CI_COMMIT_SHA}"
expire_in: 1 week
paths:
- ./release/
script:
- rsync -vAax "${DOCROOT}" release/
deploy-staging:
<<: *deploy_definition
environment:
name: staging
only:
- master
script:
# Check if deployment variables where set
- |-
if [ -z "${SSH_PRIVATE_KEY}" ] || [ -z "${STAGING_REMOTE}" ] || [ -z "${STAGING_REMOTE_PATH}" ]; then
echo "Skipping deployment";
exit
fi
- *deployment_ssh
- *deployment_dependencies
# Deploy to server
- ./deploy.sh -r "${STAGING_REMOTE}" -p "${STAGING_REMOTE_PATH}" -i "${CI_JOB_ID}-${CI_COMMIT_SHA}"
deploy-production:
<<: *deploy_definition
environment:
name: production
when: manual
only:
- master
script:
# Check if deployment variables where set
- |-
if [ -z "${SSH_PRIVATE_KEY}" ] || [ -z "${PRODUCTION_REMOTE}" ] || [ -z "${PRODUCTION_REMOTE_PATH}" ]; then
echo "Skipping deployment";
exit
fi
- *deployment_ssh
- *deployment_dependencies
# Deploy to server
- ./deploy.sh -r "${PRODUCTION_REMOTE}" -p "${PRODUCTION_REMOTE_PATH}" -i "${CI_JOB_ID}-${CI_COMMIT_SHA}"