diff options
Diffstat (limited to '.gitlab-ci.yml')
-rw-r--r-- | .gitlab-ci.yml | 153 |
1 files changed, 94 insertions, 59 deletions
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 8ae3d8ce..a388a82a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,89 +1,124 @@ image: php -cache: - paths: - - .composer - -services: - - mariadb:10.2 - 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" - COMPOSER_HOME: .composer + DOCROOT: /var/www/ + +stages: + - build-frontend + - 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.nginx: + <<: *docker_definition + stage: build-frontend + script: + - docker build --pull -t "${TEST_IMAGE}-nginx" -f contrib/nginx/Dockerfile . + - docker push "${TEST_IMAGE}-nginx" -before_script: - # Fix permissions after gitlab messed them up - - &before_fix_permissions |- - find . -type f -exec chmod 644 {} \; - find . -type d -exec chmod 755 {} \; - # Install required Packages - - &before_install_packages |- - apt update -yqq - apt install -yqq git unzip - docker-php-ext-install pdo pdo_mysql gettext - # Install xdebug - - &before_install_xdebug |- - pecl install xdebug - docker-php-ext-enable xdebug - # Install Composer - - &before_install_composer |- - curl -sS https://getcomposer.org/installer | php -- --no-ansi --install-dir /usr/local/bin/ --filename composer - /usr/local/bin/composer --no-ansi install - # MySQL DB - - &before_setup_mysql |- - chmod +x ./bin/migrate - ./bin/migrate +build-image: + <<: *docker_definition + stage: build + script: + - docker build --pull --build-arg NGINX_IMAGE="${TEST_IMAGE}-nginx" -t "${TEST_IMAGE}" -f contrib/Dockerfile . + - docker push "${TEST_IMAGE}" -.test_template: &test_definition +test: + image: ${TEST_IMAGE} stage: test + services: + - mariadb:10.2 artifacts: - name: "${CI_JOB_NAME}_${CI_PROJECT_ID}" + name: "${CI_JOB_NAME}_${CI_JOB_ID}" expire_in: 1 week + when: always paths: - ./coverage/ + - ./unittests.xml + reports: + junit: ./unittests.xml coverage: '/^\s*Lines:\s*(\d+(?:\.\d+)?%)/' - script: vendor/bin/phpunit --colors=never --coverage-text --coverage-html ./coverage/ + before_script: + - apk add ${PHPIZE_DEPS} && pecl install xdebug && docker-php-ext-enable xdebug + - 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 + - ./bin/migrate + script: + - vendor/bin/phpunit -v --colors=never --coverage-text --coverage-html "${HOMEDIR}/coverage/" --log-junit "${HOMEDIR}/unittests.xml" + - bin/migrate down -test:7.0: - <<: *test_definition - image: php:7.0 +release-image: + <<: *docker_definition + stage: release + script: + - docker pull "${TEST_IMAGE}" + - docker tag "${TEST_IMAGE}" "${RELEASE_IMAGE}" + - docker push "${RELEASE_IMAGE}" + only: + - master -test:7.1: - <<: *test_definition - image: php:7.1 +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 - services: [] stage: deploy - only: - - master + image: ${TEST_IMAGE} before_script: - - *before_fix_permissions - - *before_install_packages - - *before_install_composer + - apk add bash rsync openssh-client .deploy_template_script: # Configure SSH - - &deployment_ssh |- + - &deploy_template_script |- mkdir -p ~/.ssh - echo "$SSH_PRIVATE_KEY" | sed -e 's/\r//g' > ~/.ssh/id_ed25519 + echo "${SSH_PRIVATE_KEY}" | sed -e 's/\r//g' > ~/.ssh/id_ed25519 chmod 600 ~/.ssh/id_ed25519 + cd "${DOCROOT}" - # Install project and dependencies - - &deployment_dependencies |- - chmod +x ./bin/deploy.sh - apt update && apt install -yqq rsync openssh-client - /usr/local/bin/composer --no-ansi install --no-dev - /usr/local/bin/composer --no-ansi dump-autoload --optimize +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-staging: <<: *deploy_definition environment: name: staging + only: + - master script: # Check if deployment variables where set - |- @@ -91,16 +126,17 @@ deploy_staging: echo "Skipping deployment"; exit fi - - *deployment_ssh - - *deployment_dependencies + - *deploy_template_script # Deploy to server - ./bin/deploy.sh -r "${STAGING_REMOTE}" -p "${STAGING_REMOTE_PATH}" -i "${CI_JOB_ID}-${CI_COMMIT_SHA}" -deploy_production: +deploy-production: <<: *deploy_definition environment: name: production when: manual + only: + - master script: # Check if deployment variables where set - |- @@ -108,7 +144,6 @@ deploy_production: echo "Skipping deployment"; exit fi - - *deployment_ssh - - *deployment_dependencies + - *deploy_template_script # Deploy to server - ./bin/deploy.sh -r "${PRODUCTION_REMOTE}" -p "${PRODUCTION_REMOTE_PATH}" -i "${CI_JOB_ID}-${CI_COMMIT_SHA}" |