Merged build-docker-ci to master

This commit is contained in:
Igor Scheller 2018-09-02 17:56:19 +02:00
commit 8e0afd4d59
4 changed files with 107 additions and 86 deletions

View File

@ -1,110 +1,130 @@
image: php image: php
cache:
paths:
- .composer
services:
- mariadb:10.2
variables: variables:
DOCKER_DRIVER: overlay2
TEST_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
RELEASE_IMAGE: $CI_REGISTRY_IMAGE:latest
MYSQL_DATABASE: engelsystem MYSQL_DATABASE: engelsystem
MYSQL_USER: engel MYSQL_USER: engel
MYSQL_PASSWORD: engelsystem MYSQL_PASSWORD: engelsystem
MYSQL_HOST: mariadb MYSQL_HOST: mariadb
MYSQL_RANDOM_ROOT_PASSWORD: "yes" MYSQL_RANDOM_ROOT_PASSWORD: "yes"
COMPOSER_HOME: .composer DOCROOT: /var/www/
before_script: stages:
# Fix permissions after gitlab messed them up - build
- &before_fix_permissions |- - test
find . -type f -exec chmod 644 {} \; - release
find . -type d -exec chmod 755 {} \; - deploy
# 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
composer --no-ansi install
# MySQL DB
- &before_setup_mysql |-
chmod +x ./bin/migrate
./bin/migrate
# 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
.test_template: &test_definition .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 stage: test
services:
- mariadb:10.2
artifacts: artifacts:
name: "${CI_JOB_NAME}_${CI_JOB_ID}" name: "${CI_JOB_NAME}_${CI_JOB_ID}"
expire_in: 1 week expire_in: 1 week
when: always
paths: paths:
- ./coverage/ - ./coverage/
- ./unittests.xml
when: always
reports:
junit: ./unittests.xml
coverage: '/^\s*Lines:\s*(\d+(?:\.\d+)?%)/' 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
- chmod +x ./bin/migrate
- ./bin/migrate
script:
- set +e
- vendor/bin/phpunit -v --colors=never --coverage-text --coverage-html "${HOMEDIR}/coverage/" --log-junit "${HOMEDIR}/unittests.xml"
- status=$?
- set -e
- sed -i "s~${DOCROOT}~${HOMEDIR}/~g" "${HOMEDIR}/unittests.xml"
- exit $status
test:7.0: release-image:
<<: *test_definition <<: *docker_definition
image: php:7.0 stage: release
script:
test:7.1: - docker pull "$TEST_IMAGE"
<<: *test_definition - docker tag "$TEST_IMAGE" "$RELEASE_IMAGE"
image: php:7.1 - docker push "$RELEASE_IMAGE"
.deploy_template: &deploy_definition
services: []
stage: deploy
only: only:
- master - 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: before_script:
- *before_fix_permissions - apk add bash rsync openssh-client
- *before_install_packages
- *before_install_composer
- *before_install_yarn
.deploy_template_script: .deploy_template_script:
# Configure SSH # Configure SSH
- &deployment_ssh |- - &deploy_template_script |-
apt update && apt install -yqq rsync openssh-client
mkdir -p ~/.ssh 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 chmod 600 ~/.ssh/id_ed25519
cd "${DOCROOT}"
# Install project and dependencies build-release-file:
- &deployment_dependencies |-
chmod +x ./bin/deploy.sh
apt update && apt install -yqq rsync openssh-client
composer --no-ansi install --no-dev
composer --no-ansi dump-autoload --optimize
build_release_file:
<<: *deploy_definition <<: *deploy_definition
stage: deploy
artifacts: artifacts:
name: "release_${CI_COMMIT_REF_SLUG}_${CI_JOB_ID}_${CI_COMMIT_SHA}" name: "release_${CI_COMMIT_REF_SLUG}_${CI_JOB_ID}_${CI_COMMIT_SHA}"
expire_in: 1 week expire_in: 1 week
paths: paths:
- ./release/ - ./release/
script: script:
- *deployment_dependencies - rsync -vAax "${DOCROOT}" release/
- rsync -vAax --exclude '.git*' --exclude .composer/ --exclude coverage/ --exclude node_modules/ --exclude release/ ./ release/
deploy_staging: deploy-staging:
<<: *deploy_definition <<: *deploy_definition
environment: environment:
name: staging name: staging
only:
- master
script: script:
# Check if deployment variables where set # Check if deployment variables where set
- |- - |-
@ -112,16 +132,17 @@ deploy_staging:
echo "Skipping deployment"; echo "Skipping deployment";
exit exit
fi fi
- *deployment_ssh - *deploy_template_script
- *deployment_dependencies
# Deploy to server # Deploy to server
- ./bin/deploy.sh -r "${STAGING_REMOTE}" -p "${STAGING_REMOTE_PATH}" -i "${CI_JOB_ID}-${CI_COMMIT_SHA}" - ./bin/deploy.sh -r "${STAGING_REMOTE}" -p "${STAGING_REMOTE_PATH}" -i "${CI_JOB_ID}-${CI_COMMIT_SHA}"
deploy_production: deploy-production:
<<: *deploy_definition <<: *deploy_definition
environment: environment:
name: production name: production
when: manual when: manual
only:
- master
script: script:
# Check if deployment variables where set # Check if deployment variables where set
- |- - |-
@ -129,7 +150,6 @@ deploy_production:
echo "Skipping deployment"; echo "Skipping deployment";
exit exit
fi fi
- *deployment_ssh - *deploy_template_script
- *deployment_dependencies
# Deploy to server # Deploy to server
- ./bin/deploy.sh -r "${PRODUCTION_REMOTE}" -p "${PRODUCTION_REMOTE_PATH}" -i "${CI_JOB_ID}-${CI_COMMIT_SHA}" - ./bin/deploy.sh -r "${PRODUCTION_REMOTE}" -p "${PRODUCTION_REMOTE_PATH}" -i "${CI_JOB_ID}-${CI_COMMIT_SHA}"

View File

@ -60,14 +60,11 @@ ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "${remote_hos
if [[ -f \"${remote_path}/current/config/config.php\" ]]; then if [[ -f \"${remote_path}/current/config/config.php\" ]]; then
echo \"Config backup\" echo \"Config backup\"
cp \"${remote_path}/current/config/config.php\" \"${deploy_id}-config.php\" cp \"${remote_path}/current/config/config.php\" \"${deploy_id}-config.php\"
echo \"Restoring config\"
cp \"${deploy_id}-config.php\" \"${remote_path}/${deploy_id}/config/config.php\"
fi fi
echo \"Changing symlink\" echo \"Changing symlink\"
unlink_cmd=\$(command -v unlink || command -v rm) ln -nsf \"${remote_path}/${deploy_id}\" \"${remote_path}/current\"
\$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\"
fi
" "

View File

@ -1,7 +1,7 @@
FROM composer AS composer FROM composer AS composer
COPY composer.json /app/ COPY composer.json /app/
RUN composer install --no-dev RUN composer --no-ansi install --no-dev
RUN composer dump-autoload --optimize RUN composer --no-ansi dump-autoload --optimize
FROM node:8-alpine as themes FROM node:8-alpine as themes
WORKDIR /app WORKDIR /app
@ -17,22 +17,26 @@ COPY config/ /app/config
COPY db/ /app/db COPY db/ /app/db
COPY includes/ /app/includes COPY includes/ /app/includes
COPY locale/ /app/locale COPY locale/ /app/locale
COPY public/ /app/html COPY public/ /app/public
COPY src/ /app/src COPY src/ /app/src
COPY templates/ /app/templates COPY templates/ /app/templates
COPY composer.json LICENSE package.json README.md /app/
COPY --from=composer /app/vendor/ /app/vendor COPY --from=composer /app/vendor/ /app/vendor
COPY --from=composer /app/composer.lock /app/ 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/ COPY --from=themes /app/yarn.lock /app/
RUN rm -f /app/config/config.php RUN rm -f /app/config/config.php
FROM php:7-fpm-alpine FROM php:7-fpm-alpine
WORKDIR /var/www
COPY --from=data /app/ /var/www COPY --from=data /app/ /var/www
RUN apk add --no-cache icu-dev gettext-dev && \ 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,\ 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,\ 127.0.0.0/8,::ffff:127.0.0.0/8,\

View File

@ -26,7 +26,7 @@ http {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $forwarded_proto; proxy_set_header X-Forwarded-Proto $forwarded_proto;
index index.php; index index.php;
root /var/www/html; root /var/www/public;
location / { location / {
try_files $uri $uri/ /index.php?$args; try_files $uri $uri/ /index.php?$args;