From 6a4b0f6d790ed3c5832ed8b099d7b91eda3a07d8 Mon Sep 17 00:00:00 2001 From: Deon George Date: Wed, 30 Jun 2021 10:36:27 +1000 Subject: [PATCH] CI to build docker container --- .gitlab-ci.yml | 75 +++++---------------------------------- .gitlab-docker-x86_64.yml | 32 +++++++++++++++++ .gitlab-test.yml | 45 +++++++++++++++++++++++ Dockerfile | 12 +++++++ 4 files changed, 97 insertions(+), 67 deletions(-) create mode 100644 .gitlab-docker-x86_64.yml create mode 100644 .gitlab-test.yml create mode 100644 Dockerfile diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 72f00fc..b4631f2 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,73 +1,14 @@ -# This file is a template, and might need editing before it works on your project. -# Official framework image. Look for the different tagged releases at: -# https://hub.docker.com/r/library/php -image: registry.leenooks.net/leenooks/php:7.4-fpm-plus - -# Pick zero or more services to be used on all builds. -# Only needed when using a docker container to run your tests in. -# Check out: http://docs.gitlab.com/ce/ci/docker/using_docker_images.html#what-is-a-service -services: - - mariadb:10.5 - -variables: - MYSQL_DATABASE: testing - MYSQL_ROOT_PASSWORD: test - MYSQL_USER: test - MYSQL_PASSWORD: test +stages: + - test + - build # This folder is cached between builds # http://docs.gitlab.com/ce/ci/yaml/README.html#cache cache: + key: ${CI_COMMIT_REF_SLUG} paths: - - vendor/ + - vendor/ -# This is a basic example for a gem or script which doesn't use -# services such as redis or postgres -before_script: - # Add gnupg & zip - - apt-get update -yqq && apt-get install gnupg2 unzip -yqq - - # Install dependencies - - apt-get install git autoconf -yqq - - # Install & enable Xdebug for code coverage reports - - pecl install xdebug - - docker-php-ext-enable xdebug - - # Install Composer and project dependencies. - - mkdir -p /root/.composer - - if [ -n "$GITHUB_TOKEN" ]; then cat $GITHUB_TOKEN |base64 -d > /root/.composer/auth.json ; fi - - composer install - - # Copy over testing configuration. - # Don't forget to set the database config in .env.testing correctly - # DB_HOST=mysql - # DB_DATABASE=project_name - # DB_USERNAME=root - # DB_PASSWORD=secret - - # Generate an application key. Re-cache. - - php artisan key:generate --env=testing - - php artisan config:cache --env=testing - - # Run database migrations. - - php artisan migrate - - # Run database seed - - php artisan db:seed - -test: - tags: - - php - only: - - master - - script: - # run laravel tests - - php vendor/bin/phpunit --coverage-text --colors=never - - # run frontend tests - # if you have any task for testing frontend - # set it in your package.json script - # comment this out if you don't have a frontend test - # npm test +include: + - .gitlab-test.yml + - .gitlab-docker-x86_64.yml diff --git a/.gitlab-docker-x86_64.yml b/.gitlab-docker-x86_64.yml new file mode 100644 index 0000000..243f86d --- /dev/null +++ b/.gitlab-docker-x86_64.yml @@ -0,0 +1,32 @@ +docker: + image: docker:latest + + stage: build + + services: + - docker:dind + + variables: + VERSION: latest + CACHETAG: build-${VERSION} + DOCKER_HOST: tcp://docker:2375 + + tags: + - docker + - x86_64 + only: + - master + + before_script: + - docker info + - docker version + - echo "$CI_JOB_TOKEN" | docker login -u "$CI_REGISTRY_USER" "$CI_REGISTRY" --password-stdin + - if [ -n "$GITHUB_TOKEN" ]; then cat $GITHUB_TOKEN |base64 -d > auth.json; fi + + script: + - if [ -f init ]; then chmod 500 init; fi + - ([ -z "$REFRESH" ] && docker pull ${CI_REGISTRY_IMAGE}:${CACHETAG}) || echo "true" + - echo -n ${CI_COMMIT_SHORT_SHA} > VERSION + - docker build --cache-from ${CI_REGISTRY_IMAGE}:${CACHETAG} -t ${CI_REGISTRY_IMAGE}:${VERSION} -t ${CI_REGISTRY_IMAGE}:${CACHETAG} . + - docker push ${CI_REGISTRY_IMAGE}:${VERSION} + - docker push ${CI_REGISTRY_IMAGE}:${CACHETAG} diff --git a/.gitlab-test.yml b/.gitlab-test.yml new file mode 100644 index 0000000..45c98b1 --- /dev/null +++ b/.gitlab-test.yml @@ -0,0 +1,45 @@ +test: + image: registry.leenooks.net/leenooks/php:8.0-fpm-ext-test + + stage: test + + # NOTE: This service is dependant on project file configuration, which is not there if the cache was deleted + # resulting in the testing to fail on the first run. + services: + - mariadb:10.5 + + variables: + MYSQL_DATABASE: testing + MYSQL_ROOT_PASSWORD: test + MYSQL_USER: test + MYSQL_PASSWORD: test + + tags: + - php + only: + - master + - test + + before_script: + - mv .env.testing .env + + # Install Composer and project dependencies. + - mkdir -p /root/.composer + - if [ -n "$GITHUB_TOKEN" ]; then cat $GITHUB_TOKEN |base64 -d > /root/.composer/auth.json ; fi + - composer install + + # Generate an application key. Re-cache. + - php artisan key:generate --env=testing + - php artisan config:cache --env=testing + - php artisan migrate + - php artisan db:seed + + script: + # run laravel tests + - XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text --colors=never + + # run frontend tests + # if you have any task for testing frontend + # set it in your package.json script + # comment this out if you don't have a frontend test + # npm test diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2622ab7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM registry.leenooks.net/leenooks/php:8.0-fpm-ext + +COPY . /var/www/html/ + +RUN mkdir /var/www/.composer \ + && ([ -r auth.json ] && mv auth.json /var/www/.composer/) || true \ + && touch .composer.refresh \ + && mv .env.example .env \ + && FORCE_PERMS=1 /sbin/init \ + && chmod +x /var/www/html/artisan \ + && touch .migrate \ + && rm -rf /var/www/.composer/*