From 81890b657004ebfed45db139c15a22282d1a683b Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Mon, 20 Aug 2018 23:21:02 +0200 Subject: Build docker image with gitlab --- .gitlab-ci.yml | 141 ++++++++++++++++++++++++++--------------------- contrib/Dockerfile | 14 +++-- contrib/nginx/nginx.conf | 2 +- deploy.sh | 11 ++-- 4 files changed, 92 insertions(+), 76 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b5bdba41..a3463cc2 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,81 +1,93 @@ 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 + - 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" -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 - # MySQL DB - - &before_setup_mysql |- - apt install -yqq 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 - # Install Composer - - &before_install_composer |- - curl -sS https://getcomposer.org/installer | php -- --no-ansi --install-dir /usr/local/bin/ --filename composer - composer --no-ansi install - # Install Node.js and Yarn - - &before_install_yarn |- - apt -yqq install gnupg2 apt-transport-https - curl -sL https://deb.nodesource.com/setup_8.x | bash - - curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - - echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list - apt -yqq update && apt -yqq install nodejs yarn - yarn install - yarn build +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_template: &test_definition +test: + image: $TEST_IMAGE stage: test + services: + - mariadb:10.2 artifacts: name: "${CI_JOB_NAME}_${CI_JOB_ID}" expire_in: 1 week paths: - ./coverage/ 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 + - 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/" -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 - - *before_install_yarn + - apk add bash rsync openssh-client .deploy_template_script: # Configure SSH @@ -87,25 +99,26 @@ test:7.1: # Install project and dependencies - &deployment_dependencies |- chmod +x ./deploy.sh - apt update && apt install -yqq rsync openssh-client - composer --no-ansi install --no-dev - composer --no-ansi dump-autoload --optimize + cp ./deploy.sh "${DOCROOT}/deploy.sh" + cd "${DOCROOT}" -build_release_file: +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: - - *deployment_dependencies - - rsync -vAax --exclude '.git*' --exclude .composer/ --exclude coverage/ --exclude node_modules/ --exclude release/ ./ release/ + - rsync -vAax "${DOCROOT}" release/ -deploy_staging: +deploy-staging: <<: *deploy_definition environment: name: staging + only: + - master script: # Check if deployment variables where set - |- @@ -118,11 +131,13 @@ deploy_staging: # Deploy to server - ./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 - |- diff --git a/contrib/Dockerfile b/contrib/Dockerfile index 9b218cf8..268f9218 100644 --- a/contrib/Dockerfile +++ b/contrib/Dockerfile @@ -1,7 +1,7 @@ FROM composer AS composer COPY composer.json /app/ -RUN composer install --no-dev -RUN composer dump-autoload --optimize +RUN composer --no-ansi install --no-dev +RUN composer --no-ansi dump-autoload --optimize FROM node:8-alpine as themes WORKDIR /app @@ -17,22 +17,26 @@ COPY config/ /app/config COPY db/ /app/db COPY includes/ /app/includes COPY locale/ /app/locale -COPY public/ /app/html +COPY public/ /app/public COPY src/ /app/src COPY templates/ /app/templates +COPY composer.json LICENSE package.json README.md /app/ + COPY --from=composer /app/vendor/ /app/vendor COPY --from=composer /app/composer.lock /app/ -COPY --from=themes /app/public/assets /app/html/assets +COPY --from=themes /app/public/assets /app/public/assets COPY --from=themes /app/yarn.lock /app/ RUN rm -f /app/config/config.php FROM php:7-fpm-alpine +WORKDIR /var/www COPY --from=data /app/ /var/www RUN apk add --no-cache icu-dev gettext-dev && \ - docker-php-ext-install intl gettext pdo_mysql + docker-php-ext-install intl gettext pdo_mysql && \ + rm -r /var/www/html ENV TRUSTED_PROXIES 10.0.0.0/8,::ffff:10.0.0.0/8,\ 127.0.0.0/8,::ffff:127.0.0.0/8,\ diff --git a/contrib/nginx/nginx.conf b/contrib/nginx/nginx.conf index 96e4688f..d95c18e2 100644 --- a/contrib/nginx/nginx.conf +++ b/contrib/nginx/nginx.conf @@ -26,7 +26,7 @@ http { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $forwarded_proto; index index.php; - root /var/www/html; + root /var/www/public; location / { try_files $uri $uri/ /index.php?$args; diff --git a/deploy.sh b/deploy.sh index b731e36a..15795011 100755 --- a/deploy.sh +++ b/deploy.sh @@ -60,14 +60,11 @@ ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "${remote_hos if [[ -f \"${remote_path}/current/config/config.php\" ]]; then echo \"Config backup\" cp \"${remote_path}/current/config/config.php\" \"${deploy_id}-config.php\" - fi - - echo \"Changing symlink\" - unlink_cmd=\$(command -v unlink || command -v rm) - \$unlink_cmd \"${remote_path}/current\" && ln -s \"${remote_path}/${deploy_id}\" \"${remote_path}/current\" - if [[ -f \"${deploy_id}-config.php\" ]]; then echo \"Restoring config\" - cp \"${deploy_id}-config.php\" \"${remote_path}/current/config/config.php\" + cp \"${deploy_id}-config.php\" \"${remote_path}/${deploy_id}/config/config.php\" fi + + echo \"Changing symlink\" + ln -nsf \"${remote_path}/${deploy_id}\" \"${remote_path}/current\" " -- cgit v1.2.3-70-g09d2 From ffd81f22b636e77a3c7e2428c0df043e8fb53870 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Thu, 30 Aug 2018 13:33:16 +0200 Subject: CI: Always add artifacts --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a3463cc2..e3468273 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -48,6 +48,7 @@ test: artifacts: name: "${CI_JOB_NAME}_${CI_JOB_ID}" expire_in: 1 week + when: always paths: - ./coverage/ coverage: '/^\s*Lines:\s*(\d+(?:\.\d+)?%)/' -- cgit v1.2.3-70-g09d2