Upgrade gitlab-ci

This commit is contained in:
ErickSkrauch 2019-11-05 15:10:37 +03:00
parent 6c4ce8cb8d
commit 7505bc8262

View File

@ -1,32 +1,75 @@
stages:
- test
- build
- release
- cleanup
image: edbizarro/gitlab-ci-pipeline-php:7.3-alpine
.defineVersion: &defineVersion |-
export VERSION="${CI_COMMIT_TAG:-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHA:0:7}}"
.loginIntoRegistry: &loginIntoRegistry |-
docker login -u gitlab-ci -p $CI_BUILD_TOKEN registry.ely.by
stages:
- prepare
- testing
- build
variables:
APP_IMAGE_NAME: "registry.ely.by/elyby/accounts"
WEB_IMAGE_NAME: "registry.ely.by/elyby/accounts-nginx"
DB_IMAGE_NAME: "registry.ely.by/elyby/accounts-mariadb"
APP_IMAGE_NAME: "$CI_REGISTRY_IMAGE/app"
WEB_IMAGE_NAME: "$CI_REGISTRY_IMAGE/web"
DB_IMAGE_NAME: "$CI_REGISTRY_IMAGE/db"
php-cs-fixer:
image: edbizarro/gitlab-ci-pipeline-php:7.3-alpine
stage: test
#######################
# Shared script steps #
#######################
# GitLab do not supports bash syntax in the "variables" definitions,
# so we use custom step to define all necessary environment variables
.defineVars: &defineVars |-
export VERSION="${CI_COMMIT_TAG:-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHORT_SHA}}"
export APP_VERSIONED_IMAGE_NAME="$APP_IMAGE_NAME:$VERSION"
export APP_LATEST_IMAGE_NAME="$APP_IMAGE_NAME:latest"
export WEB_VERSIONED_IMAGE_NAME="$WEB_IMAGE_NAME:$VERSION"
export WEB_LATEST_IMAGE_NAME="$WEB_IMAGE_NAME:latest"
export DB_VERSIONED_IMAGE_NAME="$DB_IMAGE_NAME:$VERSION"
export DB_LATEST_IMAGE_NAME="$DB_IMAGE_NAME:latest"
.dockerLogin: &dockerLogin |-
docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
###################
# Steps to extend #
###################
.vendorCache:
cache:
key: backend-vendor
key: backend-deps
paths:
- vendor
policy: pull
#################
# Prepare stage #
#################
Precache dependencies:
stage: prepare
extends:
- .vendorCache
cache:
policy: pull-push
script:
- composer install --ignore-platform-reqs
#################
# Testing stage #
#################
PHP-CS-Fixer:
stage: testing
extends:
- .vendorCache
script:
- composer install
- vendor/bin/php-cs-fixer fix -v --dry-run
codeception:
image: edbizarro/gitlab-ci-pipeline-php:7.3-alpine
Codeception:
stage: testing
extends:
- .vendorCache
services:
- name: redis:4.0.10-alpine
alias: redis
@ -45,103 +88,67 @@ codeception:
MYSQL_DATABASE: "ely_accounts_test"
MYSQL_USER: "ely_accounts_tester"
MYSQL_PASSWORD: "ely_accounts_tester_password"
stage: test
cache:
key: backend-vendor
paths:
- vendor
before_script:
# While we not counting coverage, xdebug only slow down tests
# We don't count code coverage yet, so xdebug can be removed
- sudo rm /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
script:
- composer install
- php yii rbac/generate
- ./docker/php/wait-for-it.sh "${DB_HOST}:3306" -s -t 0 -- "php yii migrate/up --interactive=0"
- vendor/bin/codecept run
build:production:
image: docker:18.02
###############
# Build stage #
###############
Docker images:
stage: build
image: docker:19.03.2
services:
- docker:dind
before_script:
- *defineVersion
- *loginIntoRegistry
- *defineVars
- *dockerLogin
- sed -i -e "s/{{PLACE_VERSION_HERE}}/$VERSION/g" common/config/config.php
script:
- docker pull "$APP_IMAGE_NAME:latest"
- docker pull "$WEB_IMAGE_NAME:latest"
- docker pull "$DB_IMAGE_NAME:latest"
# Download previous images to use them as a cache
- docker pull "$APP_LATEST_IMAGE_NAME" || true
- docker pull "$WEB_LATEST_IMAGE_NAME" || true
- docker pull "$DB_LATEST_IMAGE_NAME" || true
# Build images
- >
docker build
--pull
--target app
--build-arg "build_env=prod"
--cache-from "$APP_IMAGE_NAME:latest"
-t "$APP_IMAGE_NAME:$CI_PIPELINE_ID" .
--cache-from "$APP_LATEST_IMAGE_NAME"
-t "$APP_VERSIONED_IMAGE_NAME"
-t "$APP_LATEST_IMAGE_NAME" .
- >
docker build
--pull
--target web
--build-arg "build_env=prod"
--cache-from "$APP_IMAGE_NAME:$CI_PIPELINE_ID"
--cache-from "$WEB_IMAGE_NAME:latest"
-t "$WEB_IMAGE_NAME:$CI_PIPELINE_ID" .
--cache-from "$APP_VERSIONED_IMAGE_NAME"
--cache-from "$WEB_LATEST_IMAGE_NAME"
-t "$WEB_VERSIONED_IMAGE_NAME"
-t "$WEB_LATEST_IMAGE_NAME" .
- >
docker build
--pull
--target db
--build-arg "build_env=prod"
--cache-from "$APP_IMAGE_NAME:$CI_PIPELINE_ID"
--cache-from "$DB_IMAGE_NAME:latest"
-t "$DB_IMAGE_NAME:$CI_PIPELINE_ID" .
--cache-from "$APP_VERSIONED_IMAGE_NAME"
--cache-from "$WEB_VERSIONED_IMAGE_NAME"
--cache-from "$DB_LATEST_IMAGE_NAME"
-t "$DB_VERSIONED_IMAGE_NAME"
-t "$DB_LATEST_IMAGE_NAME" .
# Push images to the registry
- docker push $APP_IMAGE_VERSIONED_NAME
- docker push $APP_IMAGE_LATEST_NAME
- docker push $WEB_IMAGE_VERSIONED_NAME
- docker push $WEB_IMAGE_LATEST_NAME
- docker push $DB_IMAGE_VERSIONED_NAME
- docker push $DB_IMAGE_LATEST_NAME
only:
- master
- tags
release:latest:
image: docker:18.02
stage: release
variables:
GIT_STRATEGY: none
before_script:
- *loginIntoRegistry
script:
- docker tag "$APP_IMAGE_NAME:$CI_PIPELINE_ID" "$APP_IMAGE_NAME:latest"
- docker push "$APP_IMAGE_NAME:latest"
- docker tag "$WEB_IMAGE_NAME:$CI_PIPELINE_ID" "$WEB_IMAGE_NAME:latest"
- docker push "$WEB_IMAGE_NAME:latest"
- docker tag "$DB_IMAGE_NAME:$CI_PIPELINE_ID" "$DB_IMAGE_NAME:latest"
- docker push "$DB_IMAGE_NAME:latest"
only:
- master
- tags
release:tag:
image: docker:18.02
stage: release
variables:
GIT_STRATEGY: none
before_script:
- *loginIntoRegistry
script:
- docker tag "$APP_IMAGE_NAME:$CI_PIPELINE_ID" "$APP_IMAGE_NAME:$VERSION"
- docker push "$APP_IMAGE_NAME:$VERSION"
- docker tag "$WEB_IMAGE_NAME:$CI_PIPELINE_ID" "$WEB_IMAGE_NAME:$VERSION"
- docker push "$WEB_IMAGE_NAME:$VERSION"
- docker tag "$DB_IMAGE_NAME:$CI_PIPELINE_ID" "$DB_IMAGE_NAME:$VERSION"
- docker push "$DB_IMAGE_NAME:$VERSION"
only:
- tags
cleanup:
stage: cleanup
variables:
GIT_STRATEGY: none
script:
- docker rmi "$APP_IMAGE_NAME:$CI_PIPELINE_ID" "$APP_IMAGE_NAME:$VERSION" "$APP_IMAGE_NAME:latest" || true
- docker rmi "$WEB_IMAGE_NAME:$CI_PIPELINE_ID" "$WEB_IMAGE_NAME:$VERSION" "$WEB_IMAGE_NAME:latest" || true
- docker rmi "$DB_IMAGE_NAME:$CI_PIPELINE_ID" "$DB_IMAGE_NAME:$VERSION" "$DB_IMAGE_NAME:latest" || true
when: always