2024-12-02 15:40:55 +05:30
|
|
|
image: thecodingmachine/php:8.3-v4-cli
|
2019-11-05 17:40:37 +05:30
|
|
|
|
2016-08-09 12:23:05 +05:30
|
|
|
stages:
|
2019-11-05 17:40:37 +05:30
|
|
|
- prepare
|
|
|
|
- testing
|
2019-08-03 18:46:08 +05:30
|
|
|
- build
|
2020-06-10 20:45:19 +05:30
|
|
|
- deploy
|
2019-08-04 00:20:34 +05:30
|
|
|
|
2016-08-09 12:23:05 +05:30
|
|
|
variables:
|
2019-11-05 17:40:37 +05:30
|
|
|
APP_IMAGE_NAME: "$CI_REGISTRY_IMAGE/app"
|
|
|
|
WEB_IMAGE_NAME: "$CI_REGISTRY_IMAGE/web"
|
|
|
|
DB_IMAGE_NAME: "$CI_REGISTRY_IMAGE/db"
|
2024-12-02 17:05:21 +05:30
|
|
|
PHP_EXTENSION_INTL: 1
|
|
|
|
PHP_EXTENSION_IMAGICK: 1
|
2019-11-05 17:40:37 +05:30
|
|
|
|
|
|
|
#######################
|
|
|
|
# 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"
|
2016-08-09 12:23:05 +05:30
|
|
|
|
2019-11-05 17:40:37 +05:30
|
|
|
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
|
|
|
|
|
2020-06-10 20:45:19 +05:30
|
|
|
.installSentry: &installSentry |-
|
|
|
|
curl -sL https://sentry.io/get-cli/ | bash
|
|
|
|
|
|
|
|
.setupSSH: &setupSSH |-
|
|
|
|
mkdir ~/.ssh
|
|
|
|
echo -e "Host *\n StrictHostKeyChecking no\n" > ~/.ssh/config
|
|
|
|
eval $(ssh-agent -s)
|
|
|
|
echo "$SSH_DEPLOY_KEY" | tr -d '\r' | ssh-add -
|
|
|
|
|
2019-11-05 17:40:37 +05:30
|
|
|
###################
|
|
|
|
# Steps to extend #
|
|
|
|
###################
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
.vendorCache: &vendorCache
|
|
|
|
key: composer
|
|
|
|
paths:
|
|
|
|
- vendor
|
|
|
|
policy: pull
|
2019-11-05 17:40:37 +05:30
|
|
|
|
|
|
|
#################
|
|
|
|
# Prepare stage #
|
|
|
|
#################
|
|
|
|
|
2020-05-21 18:49:28 +05:30
|
|
|
Composer:
|
2019-11-05 17:40:37 +05:30
|
|
|
stage: prepare
|
|
|
|
cache:
|
2024-12-02 15:40:55 +05:30
|
|
|
<<: *vendorCache
|
2019-11-05 17:40:37 +05:30
|
|
|
policy: pull-push
|
2019-12-15 07:56:45 +05:30
|
|
|
before_script:
|
2021-02-14 23:31:49 +05:30
|
|
|
- composer config github-oauth.github.com "$GITHUB_TOKEN"
|
2019-11-05 17:40:37 +05:30
|
|
|
script:
|
|
|
|
- composer install --ignore-platform-reqs
|
|
|
|
|
|
|
|
#################
|
|
|
|
# Testing stage #
|
|
|
|
#################
|
|
|
|
|
|
|
|
PHP-CS-Fixer:
|
|
|
|
stage: testing
|
2024-12-02 15:40:55 +05:30
|
|
|
cache:
|
|
|
|
- *vendorCache
|
|
|
|
- key: php-cs-fixer-$CI_COMMIT_REF_SLUG
|
|
|
|
fallback_keys:
|
|
|
|
- php-cs-fixer-$CI_DEFAULT_BRANCH
|
|
|
|
paths:
|
|
|
|
- .php-cs-fixer.cache
|
|
|
|
when: always
|
2019-08-03 18:46:08 +05:30
|
|
|
script:
|
|
|
|
- vendor/bin/php-cs-fixer fix -v --dry-run
|
2018-07-10 22:32:19 +05:30
|
|
|
|
2019-11-05 17:40:37 +05:30
|
|
|
Codeception:
|
|
|
|
stage: testing
|
2024-12-02 15:40:55 +05:30
|
|
|
cache: *vendorCache
|
2019-08-03 18:46:08 +05:30
|
|
|
services:
|
|
|
|
- name: redis:4.0.10-alpine
|
|
|
|
alias: redis
|
2019-11-15 22:33:52 +05:30
|
|
|
- name: bitnami/mariadb:10.3.20-debian-9-r4
|
2019-08-03 18:46:08 +05:30
|
|
|
alias: db
|
|
|
|
variables:
|
|
|
|
# App config
|
|
|
|
DB_HOST: "db"
|
|
|
|
DB_DATABASE: "ely_accounts_test"
|
|
|
|
DB_USER: "ely_accounts_tester"
|
|
|
|
DB_PASSWORD: "ely_accounts_tester_password"
|
|
|
|
REDIS_HOST: "redis"
|
|
|
|
REDIS_PORT: "6379"
|
|
|
|
# MariaDB config
|
2019-11-15 22:39:19 +05:30
|
|
|
ALLOW_EMPTY_PASSWORD: "yes"
|
2019-11-15 22:33:52 +05:30
|
|
|
MARIADB_DATABASE: "ely_accounts_test"
|
|
|
|
MARIADB_USER: "ely_accounts_tester"
|
|
|
|
MARIADB_PASSWORD: "ely_accounts_tester_password"
|
2019-08-03 18:46:08 +05:30
|
|
|
before_script:
|
2019-11-05 17:40:37 +05:30
|
|
|
# We don't count code coverage yet, so xdebug can be removed
|
2019-08-03 18:46:08 +05:30
|
|
|
- sudo rm /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
|
2021-02-14 23:31:49 +05:30
|
|
|
# Install wait-for-it script
|
|
|
|
- sudo curl "https://raw.githubusercontent.com/vishnubob/wait-for-it/81b1373f17855/wait-for-it.sh" -o /usr/local/bin/wait-for-it
|
|
|
|
- sudo chmod a+x /usr/local/bin/wait-for-it
|
2019-08-03 18:46:08 +05:30
|
|
|
script:
|
|
|
|
- php yii rbac/generate
|
2021-02-15 00:02:40 +05:30
|
|
|
- wait-for-it "${DB_HOST}:3306" -s -t 0 -- php yii migrate/up --interactive=0
|
2019-08-03 18:46:08 +05:30
|
|
|
- vendor/bin/codecept run
|
2016-08-09 12:23:05 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
PHPStan:
|
|
|
|
stage: testing
|
|
|
|
image: $PHP_BUILD_IMAGE
|
|
|
|
cache:
|
|
|
|
- *vendorCache
|
|
|
|
- key: phpstan-$CI_COMMIT_REF_SLUG
|
|
|
|
fallback_keys:
|
|
|
|
- phpstan-$CI_DEFAULT_BRANCH
|
|
|
|
paths:
|
|
|
|
- .phpstan
|
|
|
|
when: on_success
|
|
|
|
before_script:
|
|
|
|
- |
|
|
|
|
echo -e "includes: [phpstan.dist.neon]\nparameters:\n tmpDir: .phpstan\n reportUnmatchedIgnoredErrors: false" > phpstan.neon
|
|
|
|
script:
|
|
|
|
- vendor/bin/codecept build
|
|
|
|
- vendor/bin/phpstan analyse --no-progress --memory-limit 2G
|
|
|
|
|
2019-11-05 17:40:37 +05:30
|
|
|
###############
|
|
|
|
# Build stage #
|
|
|
|
###############
|
|
|
|
|
2020-05-21 18:49:28 +05:30
|
|
|
Docker:
|
2019-08-03 18:46:08 +05:30
|
|
|
stage: build
|
2022-12-06 03:42:09 +05:30
|
|
|
image: docker:20.10.21
|
2020-05-21 18:53:42 +05:30
|
|
|
services:
|
2022-12-06 03:42:09 +05:30
|
|
|
- docker:20.10.21-dind
|
2020-05-21 18:53:42 +05:30
|
|
|
variables:
|
|
|
|
# Use TLS https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#tls-enabled
|
|
|
|
DOCKER_HOST: tcp://docker:2376
|
|
|
|
DOCKER_TLS_CERTDIR: "/certs"
|
2019-08-03 18:46:08 +05:30
|
|
|
before_script:
|
2019-11-05 17:40:37 +05:30
|
|
|
- *defineVars
|
|
|
|
- *dockerLogin
|
2019-08-03 18:46:08 +05:30
|
|
|
- sed -i -e "s/{{PLACE_VERSION_HERE}}/$VERSION/g" common/config/config.php
|
|
|
|
script:
|
2019-11-05 17:40:37 +05:30
|
|
|
# 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
|
2019-08-03 18:46:08 +05:30
|
|
|
- >
|
2021-02-14 23:31:49 +05:30
|
|
|
docker build .
|
2019-08-03 18:46:08 +05:30
|
|
|
--pull
|
|
|
|
--target app
|
|
|
|
--build-arg "build_env=prod"
|
2019-11-05 17:40:37 +05:30
|
|
|
--cache-from "$APP_LATEST_IMAGE_NAME"
|
|
|
|
-t "$APP_VERSIONED_IMAGE_NAME"
|
2021-02-14 23:31:49 +05:30
|
|
|
-t "$APP_LATEST_IMAGE_NAME"
|
2019-08-03 18:46:08 +05:30
|
|
|
- >
|
2021-02-14 23:31:49 +05:30
|
|
|
docker build .
|
2019-08-03 18:46:08 +05:30
|
|
|
--pull
|
|
|
|
--target web
|
|
|
|
--build-arg "build_env=prod"
|
2019-11-05 17:40:37 +05:30
|
|
|
--cache-from "$APP_VERSIONED_IMAGE_NAME"
|
|
|
|
--cache-from "$WEB_LATEST_IMAGE_NAME"
|
|
|
|
-t "$WEB_VERSIONED_IMAGE_NAME"
|
2021-02-14 23:31:49 +05:30
|
|
|
-t "$WEB_LATEST_IMAGE_NAME"
|
2019-08-03 18:46:08 +05:30
|
|
|
- >
|
2021-02-14 23:31:49 +05:30
|
|
|
docker build .
|
2019-08-03 18:46:08 +05:30
|
|
|
--pull
|
|
|
|
--target db
|
|
|
|
--build-arg "build_env=prod"
|
2019-11-05 17:40:37 +05:30
|
|
|
--cache-from "$APP_VERSIONED_IMAGE_NAME"
|
|
|
|
--cache-from "$WEB_VERSIONED_IMAGE_NAME"
|
|
|
|
--cache-from "$DB_LATEST_IMAGE_NAME"
|
|
|
|
-t "$DB_VERSIONED_IMAGE_NAME"
|
2021-02-14 23:31:49 +05:30
|
|
|
-t "$DB_LATEST_IMAGE_NAME"
|
2019-11-05 17:40:37 +05:30
|
|
|
# Push images to the registry
|
2019-11-05 17:57:07 +05:30
|
|
|
- docker push $APP_VERSIONED_IMAGE_NAME
|
|
|
|
- docker push $APP_LATEST_IMAGE_NAME
|
|
|
|
- docker push $WEB_VERSIONED_IMAGE_NAME
|
|
|
|
- docker push $WEB_LATEST_IMAGE_NAME
|
|
|
|
- docker push $DB_VERSIONED_IMAGE_NAME
|
|
|
|
- docker push $DB_LATEST_IMAGE_NAME
|
2022-01-22 01:41:45 +05:30
|
|
|
rules:
|
|
|
|
- if: '$CI_COMMIT_TAG'
|
|
|
|
when: on_success
|
|
|
|
- if: '$CI_COMMIT_BRANCH == "master"'
|
|
|
|
when: on_success
|
|
|
|
- if: '$CI_COMMIT_MESSAGE =~ /\[deploy.*\]/'
|
|
|
|
when: on_success
|
|
|
|
# Default:
|
|
|
|
- when: never
|
2020-06-10 20:45:19 +05:30
|
|
|
|
|
|
|
##########
|
|
|
|
# Deploy #
|
|
|
|
##########
|
|
|
|
|
|
|
|
.beforeSentryDeploy: &beforeSentryDeploy |-
|
|
|
|
sentry-cli releases new $VERSION
|
|
|
|
sentry-cli releases set-commits --commit "elyby/accounts@${CI_COMMIT_SHA}" $VERSION
|
|
|
|
|
|
|
|
.afterSentryDeploy: &afterSentryDeploy |-
|
|
|
|
sentry-cli releases deploys $VERSION new -e $CI_ENVIRONMENT_NAME
|
|
|
|
sentry-cli releases finalize $VERSION
|
|
|
|
|
|
|
|
.deployJob:
|
|
|
|
stage: deploy
|
|
|
|
image: docksal/ssh-agent:1.3
|
|
|
|
needs:
|
|
|
|
- Docker
|
|
|
|
variables:
|
|
|
|
GIT_STRATEGY: none
|
|
|
|
before_script:
|
|
|
|
- *defineVars
|
|
|
|
- *installSentry
|
|
|
|
- *setupSSH
|
|
|
|
script:
|
|
|
|
- *beforeSentryDeploy
|
|
|
|
# Escape $ with backslash to prevent value evaluation from CI container.
|
2020-06-11 17:11:18 +05:30
|
|
|
# We're not using $APP_LATEST_IMAGE_NAME because on remote server might be
|
2020-06-10 20:45:19 +05:30
|
|
|
# a different semantic of preferred image version tag
|
|
|
|
- |
|
|
|
|
ssh -J deploy@ely.by:4534 -p 722 "root@$VM_HOST_NAME" /bin/bash << EOF
|
|
|
|
set -e
|
|
|
|
cd "$VM_DEPLOY_PATH"
|
|
|
|
docker pull "$APP_VERSIONED_IMAGE_NAME"
|
|
|
|
docker pull "$WEB_VERSIONED_IMAGE_NAME"
|
|
|
|
docker tag "$APP_VERSIONED_IMAGE_NAME" "$APP_IMAGE_NAME:latest"
|
|
|
|
docker tag "$WEB_VERSIONED_IMAGE_NAME" "$WEB_IMAGE_NAME:latest"
|
|
|
|
docker-compose stop app worker cron
|
|
|
|
docker-compose rm -fv app worker cron
|
2020-06-11 17:11:18 +05:30
|
|
|
docker-compose up -d --scale worker=3 app worker cron
|
2020-06-10 20:45:19 +05:30
|
|
|
docker-compose stop web
|
|
|
|
docker-compose rm -fv web
|
|
|
|
docker-compose up -d web
|
|
|
|
EOF
|
|
|
|
- *afterSentryDeploy
|
|
|
|
|
|
|
|
Dev:
|
|
|
|
extends:
|
|
|
|
- .deployJob
|
|
|
|
environment:
|
|
|
|
name: Development
|
|
|
|
variables:
|
|
|
|
VM_HOST_NAME: playground.ely.local
|
|
|
|
VM_DEPLOY_PATH: /srv/dev.account.ely.by
|
|
|
|
rules:
|
|
|
|
- if: '$CI_COMMIT_TAG'
|
|
|
|
when: on_success
|
|
|
|
- if: '$CI_COMMIT_BRANCH == "master"'
|
|
|
|
when: on_success
|
|
|
|
- if: '$CI_COMMIT_MESSAGE =~ /\[deploy dev\]/'
|
|
|
|
when: on_success
|
|
|
|
# Default:
|
|
|
|
- when: never
|
2020-06-11 17:11:18 +05:30
|
|
|
|
|
|
|
Prod:
|
|
|
|
extends:
|
|
|
|
- .deployJob
|
|
|
|
environment:
|
|
|
|
name: Production
|
|
|
|
variables:
|
|
|
|
VM_HOST_NAME: accounts.ely.local
|
|
|
|
VM_DEPLOY_PATH: /srv
|
|
|
|
rules:
|
|
|
|
- if: '$CI_COMMIT_BRANCH != "master"'
|
|
|
|
when: never
|
|
|
|
- if: '$CI_COMMIT_MESSAGE =~ /\[deploy\]/'
|
|
|
|
when: on_success
|
|
|
|
# Default:
|
|
|
|
- when: manual
|