Build docker image with gitlab

main
Igor Scheller 6 years ago
parent bf6efe532c
commit 81890b6570

@ -1,81 +1,93 @@
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/
stages:
- build
- test
- release
- deploy
.docker_template: &docker_definition
image: docker:latest
services:
- docker:dind
tags:
- dind
before_script: before_script:
# Fix permissions after gitlab messed them up - docker login -u gitlab-ci-token -p "$CI_JOB_TOKEN" "$CI_REGISTRY"
- &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
.test_template: &test_definition 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
paths: paths:
- ./coverage/ - ./coverage/
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
- 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: release-image:
<<: *test_definition <<: *docker_definition
image: php:7.0 stage: release
script:
- docker pull "$TEST_IMAGE"
- docker tag "$TEST_IMAGE" "$RELEASE_IMAGE"
- docker push "$RELEASE_IMAGE"
only:
- master
test:7.1: release-image.nginx:
<<: *test_definition <<: *docker_definition
image: php:7.1 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 .deploy_template: &deploy_definition
services: []
stage: deploy stage: deploy
only: image: $TEST_IMAGE
- master
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
@ -87,25 +99,26 @@ test:7.1:
# Install project and dependencies # Install project and dependencies
- &deployment_dependencies |- - &deployment_dependencies |-
chmod +x ./deploy.sh chmod +x ./deploy.sh
apt update && apt install -yqq rsync openssh-client cp ./deploy.sh "${DOCROOT}/deploy.sh"
composer --no-ansi install --no-dev cd "${DOCROOT}"
composer --no-ansi dump-autoload --optimize
build_release_file: 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
- |- - |-
@ -118,11 +131,13 @@ deploy_staging:
# Deploy to server # Deploy to server
- ./deploy.sh -r "${STAGING_REMOTE}" -p "${STAGING_REMOTE_PATH}" -i "${CI_JOB_ID}-${CI_COMMIT_SHA}" - ./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
- |- - |-

@ -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,\

@ -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;

@ -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\"
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\" 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 fi
echo \"Changing symlink\"
ln -nsf \"${remote_path}/${deploy_id}\" \"${remote_path}/current\"
" "

Loading…
Cancel
Save