├── .gitignore ├── other.yml ├── cs.yml ├── multi_deploy.yml ├── create_phar ├── phar.yml └── create_phar.php ├── tests.yml ├── Readme.md ├── deploy.yml ├── templates.yml ├── create_environment_for_branch.yml ├── Yii.yml └── gitlab.yml /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | -------------------------------------------------------------------------------- /other.yml: -------------------------------------------------------------------------------- 1 | # Информация о проекте через api 2 | - 'curl -s --header "PRIVATE-TOKEN: ${CI_JOB_TOKEN}" "http://gitlab.nextcontact.ru/api/v4/project/${CI_PROJECT_ID}" > info.json' -------------------------------------------------------------------------------- /cs.yml: -------------------------------------------------------------------------------- 1 | # Code Sniffer 2 | cs: 3 | stage: cs 4 | script: 5 | - composer require "squizlabs/php_codesniffer=*" 6 | - 'vendor/bin/phpcs --standard=PSR1,PSR2 --ignore=tests/autoload.php src tests' 7 | -------------------------------------------------------------------------------- /multi_deploy.yml: -------------------------------------------------------------------------------- 1 | # $CLUSTER - список серверов, PROJECT_PATH - путь 2 | script: 3 | for server in $CLUSTER; do if [ ${#PROJECT_PATH} -ge 10 ]; then ssh ${server} mkdir -p "${PROJECT_PATH}"; rsync -zrpth --stats --delete-after --exclude=.git $(pwd)/ ${server}:${PROJECT_PATH}; fi; done 4 | -------------------------------------------------------------------------------- /create_phar/phar.yml: -------------------------------------------------------------------------------- 1 | generate_phar: 2 | stage: build 3 | only: 4 | - master 5 | when: manual 6 | script: 7 | - echo "phar.readonly = Off" > /etc/php/7.0/mods-available/phar.ini 8 | - php -d extension=phar.so create_phar.php 9 | artifacts: 10 | paths: 11 | - project.phar 12 | expire_in: 1 day -------------------------------------------------------------------------------- /create_phar/create_phar.php: -------------------------------------------------------------------------------- 1 | startBuffering(); 7 | 8 | $defaultStub = $phar->createDefaultStub('command.php'); 9 | 10 | $phar->buildFromDirectory(dirname(__FILE__)); 11 | 12 | $stub = "#!/usr/bin/env php \n" . $defaultStub; 13 | 14 | $phar->setStub($stub); 15 | 16 | $phar->stopBuffering(); -------------------------------------------------------------------------------- /tests.yml: -------------------------------------------------------------------------------- 1 | # Запуск тестов 2 | test: 3 | stage: test 4 | script: 5 | # Download stable release of xdebug 2.4.0 6 | - apt-get update 7 | - apt-get install -y php7.0-xdebug 8 | - '[[ -f composer.json ]] && composer install --prefer-dist --no-progress' 9 | - ${CI_PROJECT_DIR}/vendor/bin/phpunit -c ${CI_PROJECT_DIR}/tests/phpunit-cov.xml 10 | artifacts: 11 | paths: 12 | - report/ -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | Примеры скриптов gitlab ci для php-приложения 2 | ============================================= 3 | 4 | * [Полный пример (Сборка, тесты, деплой)](gitlab.yml) 5 | * [Сборка](build.yml) 6 | * [Тест с покрытием](tests.yml) 7 | * [Проверка стиля кода](cs.yml) 8 | * [Выкладка](deploy.yml) 9 | * [Использование шаблонов](templates.yml) 10 | * [Деплой на несколько серверов](multi_deploy.yml) 11 | * [Для Yii](Yii.yml) 12 | * [Развертывание и удаление тестового окружения](create_environment_for_branch.yml) 13 | 14 | * [Сборка phar-архива](create_phar/phar.yml) 15 | * [Прочее](other.yml) 16 | -------------------------------------------------------------------------------- /deploy.yml: -------------------------------------------------------------------------------- 1 | stages: 2 | - deploy 3 | 4 | before_script: 5 | # Настройка ключей д 6 | - eval $(ssh-agent -s) 7 | - ssh-add <(cat ~/.ssh/id_rsa.deploy) 8 | - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' 9 | 10 | # Выкладка на боевой 11 | deploy to production: 12 | stage: deploy 13 | only: 14 | - master 15 | environment: 16 | name: production 17 | script: 18 | # Права 19 | - chmod -R 0775 tmp 20 | # Версия сборки 21 | - sed -i -- "s/{BUILD_VERSION}/$CI_JOB_ID/g" config/config.php 22 | # Выкладка 23 | - rsync -zrpth --stats --delete-after --exclude=.git $(pwd)/ ${DEPLOY_PROD_USER}@${DEPLOY_PROD_SERVER}:${DEPLOY_PROD_PROJECT_PATH}master 24 | when: manual 25 | -------------------------------------------------------------------------------- /templates.yml: -------------------------------------------------------------------------------- 1 | # Шаблон выкладки 2 | .deploy_template: &deploy_definition | 3 | - rsync -zrpth --stats --delete-after --exclude=.git $(pwd)/ ${DEPLOY_SERVER}:${PROJECT_PATH}master 4 | 5 | deploy to production: 6 | stage: deploy 7 | only: 8 | - master 9 | environment: 10 | name: production 11 | variables: 12 | PROJECT_PATH: $PROJECT_PATH_PROD 13 | DEPLOY_SERVER: $DEPLOY_SERVER_PROD 14 | script: 15 | # Выкладка 16 | - *deploy_definition 17 | when: manual 18 | 19 | deploy to staging: 20 | stage: deploy 21 | only: 22 | - develop 23 | - /^feature\/.*$/ 24 | environment: 25 | name: production 26 | variables: 27 | PROJECT_PATH: $PROJECT_PATH_DEV 28 | DEPLOY_SERVER: $DEPLOY_SERVER_DEV 29 | script: 30 | # Выкладка 31 | - *deploy_definition 32 | when: manual -------------------------------------------------------------------------------- /create_environment_for_branch.yml: -------------------------------------------------------------------------------- 1 | variables: 2 | # Заявка 3 | TASK_ID: '' 4 | 5 | stages: 6 | - deploy 7 | 8 | # Выкладка ветки для теста 9 | deploy to staging: 10 | stage: deploy 11 | only: 12 | - /^feature\/.*$/ 13 | except: 14 | - feature/master 15 | - develop 16 | - dev 17 | - master 18 | environment: 19 | name: test/${CI_COMMIT_REF_SLUG} 20 | url: ${URL_TEST}${CI_COMMIT_REF_SLUG}/index.php 21 | on_stop: remove in test 22 | script: 23 | # build script 24 | # ... 25 | when: manual 26 | 27 | # Удаление ветки для теста 28 | remove in develop: 29 | stage: deploy 30 | only: 31 | - /^feature\/.*$/ 32 | except: 33 | - feature/master 34 | - develop 35 | - dev 36 | environment: 37 | name: develop/${CI_COMMIT_REF_SLUG} 38 | action: stop 39 | script: 40 | # delete script 41 | # ... 42 | when: manual -------------------------------------------------------------------------------- /Yii.yml: -------------------------------------------------------------------------------- 1 | # Сборка проекта 2 | build: 3 | stage: build 4 | script: 5 | # Composer 6 | - composer global require "fxp/composer-asset-plugin:^1.2.0" 7 | - composer config --global github-oauth.github.com $GITHUB_TOKEN 8 | - '[[ -f composer.json ]] && composer install --prefer-dist --no-progress' 9 | 10 | # Выкладка на боевой 11 | deploy to production: 12 | stage: deploy 13 | only: 14 | - master 15 | environment: 16 | name: production 17 | script: 18 | # Права 19 | - chmod -R 0777 runtime web/assets 20 | # Конфиги 21 | - cp web/index.php.inst web/index.php 22 | - sed -i -- "s/%%YII_DEBUG%%/false/g" web/index.php 23 | - sed -i -- "s/%%YII_ENV%%/prod/g" web/index.php 24 | - sed -i -- "s/{USER}/$DB_USER_DEV/g" core/config/db.php 25 | - sed -i -- "s/{PASSWORD}/$DB_PASS_DEV/g" core/config/db.php 26 | # Минификация ассетов 27 | - php yii asset core/assets/core.php core/assets/core-prod.php 28 | # Миграции 29 | - php yii migrate/up --interactive=0 30 | # Выкладка 31 | - rsync -zrpth --stats --delete-after --exclude=.git $(pwd)/ ${DEPLOY_PROD_USER}@${DEPLOY_PROD_SERVER}:${DEPLOY_PROD_PROJECT_PATH} 32 | when: manual 33 | -------------------------------------------------------------------------------- /gitlab.yml: -------------------------------------------------------------------------------- 1 | image: php70:latest 2 | 3 | variables: 4 | # Заявка 5 | TASK_ID: '' 6 | 7 | cache: 8 | key: "vendor-$CI_COMMIT_REF_NAME-$CI_COMMIT_SHA" 9 | paths: 10 | - vendor 11 | 12 | stages: 13 | - build 14 | - test 15 | - deploy 16 | - cs 17 | 18 | before_script: 19 | # Настройка ключей деплоя одинаково для всех проектов 20 | - eval $(ssh-agent -s) 21 | - ssh-add <(cat ~/.ssh/id_rsa.deploy) 22 | - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' 23 | # Определяем задачу по ветке 24 | - TASK_ID=$(sed 's/^.*\///g' <<< $CI_COMMIT_REF_NAME) 25 | 26 | # Сборка проекта 27 | build: 28 | stage: build 29 | script: 30 | # Composer 31 | - '[[ -f composer.json ]] && composer install --prefer-dist --no-progress' 32 | 33 | # Запуск тестов 34 | test: 35 | stage: test 36 | script: 37 | # Download stable release of xdebug 2.4.0 38 | - apt-get update 39 | - apt-get install -y php7.0-xdebug 40 | - '[[ -f composer.json ]] && composer install --prefer-dist --no-progress' 41 | - ${CI_PROJECT_DIR}/vendor/bin/phpunit -c ${CI_PROJECT_DIR}/tests/phpunit-cov.xml 42 | artifacts: 43 | paths: 44 | - report/ 45 | 46 | # Code Sniffer 47 | cs: 48 | stage: cs 49 | script: 50 | - composer require "squizlabs/php_codesniffer=*" 51 | - 'vendor/bin/phpcs --standard=PSR1,PSR2 --ignore=tests/autoload.php src t 52 | 53 | # Шаблон выкладки 54 | .deploy_template: &deploy_definition | 55 | echo "$CLUSTER" 56 | echo "$PROJECT_PATH" 57 | for server in $CLUSTER; do if [ ${#PROJECT_PATH} -ge 10 ]; then ssh ${server} mkdir -p "${PROJECT_PATH}"; rsync -zrpth --stats --delete-after --exclude=.git $(pwd)/ ${server}:${PROJECT_PATH}; fi; done 58 | # чистка кеша 59 | for server in $CLUSTER; do if [ ${#PROJECT_PATH} -ge 10 ]; then ssh ${server} "php ${PROJECT_PATH}simple.php System/Cache/clear"; break; fi; done 60 | 61 | # Шаблон удаления 62 | .remove_template: &remove_definition | 63 | for server in $CLUSTER; do if [ ${#server} -ge 10 ]; then ssh ${server} rm -rf ${PROJECT_PATH}; fi; done 64 | 65 | # Удаление ветки для теста 66 | remove in test: 67 | stage: deploy 68 | only: 69 | - /^feature\/.*$/ 70 | - master 71 | except: 72 | - develop 73 | - dev 74 | environment: 75 | name: test/${CI_COMMIT_REF_SLUG} 76 | action: stop 77 | variables: 78 | CLUSTER: $CLUSTER_TEST 79 | PROJECT_PATH: ${PROJECT_PATH_TEST}${CI_COMMIT_REF_SLUG} 80 | script: 81 | - *remove_definition 82 | when: manual 83 | 84 | # Выкладка ветки для теста 85 | deploy to test: 86 | stage: deploy 87 | only: 88 | - /^feature\/.*$/ 89 | - master 90 | except: 91 | - develop 92 | - dev 93 | environment: 94 | name: test/${CI_COMMIT_REF_SLUG} 95 | url: ${URL_TEST}${CI_COMMIT_REF_SLUG}/index.php?route=Call/Call/products 96 | on_stop: remove in test 97 | variables: 98 | CLUSTER: $CLUSTER_TEST 99 | PROJECT_PATH: ${PROJECT_PATH_TEST}${CI_COMMIT_REF_SLUG}/ 100 | script: 101 | # Версия сборки 102 | - sed -i -- "s/{BUILD_VERSION}/$CI_JOB_ID/g" config/config.php 103 | # Права 104 | - chmod -R 0777 templates_c 105 | # Выкладка 106 | - *deploy_definition 107 | when: manual 108 | 109 | # Выкладка на боевой 110 | deploy to production: 111 | stage: deploy 112 | only: 113 | - master 114 | environment: 115 | name: production 116 | variables: 117 | CLUSTER: $CLUSTER_PROD 118 | PROJECT_PATH: $PROJECT_PATH_PROD 119 | script: 120 | # Права 121 | - chmod -R 0777 templates_c 122 | # Боевой конфиг 123 | - sed -i -- "s/{CONFIG_FOR_REPLACE_JENKINS}/work/g" config/config.php 124 | # Версия сборки 125 | - sed -i -- "s/{BUILD_VERSION}/$CI_JOB_ID/g" config/config.php 126 | # Выкладка 127 | - *deploy_definition 128 | when: manual 129 | --------------------------------------------------------------------------------