├── .gitlab-ci-deploy-aws-s3.yml ├── .gitlab-ci-laravel.yml ├── .gitlab-ci-multi-tenant.yml ├── .gitlab-ci-php-coverage-report.yml └── README.md /.gitlab-ci-deploy-aws-s3.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Gitlab CI configuration for deploying your app to AWS S3 bucket. 3 | # Next you will need to setup Amazon CodeDeploy to listen to changes in your bucket and deploy the zip file. 4 | # 5 | # You will have to setup 4 variables in your Gitlab setup for the AWS SDK to pick you your AMI credentials 6 | # AWS_ACCESS_KEY_ID, AWS_BUCKET, AWS_REGION, AWS_SECRET_ACCESS_KEY 7 | # 8 | 9 | stages: 10 | - test 11 | - deploy 12 | 13 | test:php7: 14 | image: tetraweb/php:7.0 15 | stage: test 16 | script: 17 | - echo "run your tests here" 18 | 19 | ## Deployment - Production 20 | deploy_production: 21 | image: napp/docker-aws-cli 22 | stage: deploy 23 | when: manual 24 | script: 25 | - zip myapp.zip -r . 26 | - aws s3 cp ./myapp.zip s3://$AWS_BUCKET 27 | environment: 28 | name: production 29 | url: https://myapp.com 30 | only: 31 | - master 32 | -------------------------------------------------------------------------------- /.gitlab-ci-laravel.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Gitlab CI configuration for the Laravel Framework. 3 | # 4 | 5 | stages: 6 | - test 7 | 8 | cache: 9 | # cache per-job and per-branch 10 | key: "$CI_BUILD_NAME/$CI_BUILD_REF_NAME" 11 | paths: 12 | - vendor/ 13 | 14 | before_script: 15 | # Pull in MySQL client for db setup 16 | - apt-get update -yqq 17 | 18 | # PHP Extensions 19 | - docker-php-ext-enable zip mbstring pdo_mysql soap mysqli gd 20 | 21 | # Install packages through composer 22 | - composer self-update 23 | - composer install --no-progress --no-interaction 24 | 25 | # ENV configuration 26 | - sed -i.bak 's/DB_HOST=localhost/DB_HOST=mysql/g' .env.testing 27 | - cp .env.testing .env 28 | 29 | # Laravel: Generate an application key. Re-cache. migrate 30 | - php artisan key:generate 31 | - php artisan config:clear 32 | - php artisan migrate:refresh 33 | - php artisan db:seed --env="testing" 34 | 35 | variables: 36 | WITH_XDEBUG: "1" 37 | MYSQL_ROOT_PASSWORD: mysql 38 | MYSQL_DATABASE: myapp_testing 39 | MYSQL_USER: homestead 40 | MYSQL_PASSWORD: secret 41 | COMPOSER_HOME: /cache/composer 42 | 43 | services: 44 | - mysql:5.7 45 | 46 | test:php7-mysql5.7: 47 | image: tetraweb/php:7.0 48 | stage: test 49 | script: 50 | # Mess Detection 51 | #- vendor/bin/phpmd app/ text phpmd.xml 52 | 53 | # Code Beautifier and Fixer 54 | #- vendor/bin/phpcbf --standard=psr2 app/ 55 | 56 | # Code Sniffer 57 | #- vendor/bin/phpcs --standard=psr2 app 58 | 59 | # PHPUnit 60 | - echo "Running PHPUnit Tests" 61 | - vendor/bin/phpunit --colors --debug 62 | -------------------------------------------------------------------------------- /.gitlab-ci-multi-tenant.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Gitlab CI configuration for multi tenant with multi database setup 3 | # This project is based on the Laravel Framework, so it has some php artisan command. You could use any framework or language you want. 4 | # 5 | 6 | stages: 7 | - test 8 | - deploy 9 | 10 | cache: 11 | # cache per-job and per-branch 12 | key: "$CI_BUILD_NAME/$CI_BUILD_REF_NAME" 13 | paths: 14 | - vendor/ 15 | 16 | before_script: 17 | # Pull in MySQL client for db setup 18 | - apt-get update -yqq 19 | - apt-get install -y mysql-client libmysqlclient-dev --no-install-recommends 20 | 21 | # PHP Extensions 22 | - docker-php-ext-enable zip mbstring pdo_mysql soap mysqli gd 23 | 24 | # Install packages through composer 25 | - composer self-update 26 | - composer install --no-progress --no-interaction 27 | 28 | # ENV configuration 29 | - sed -i.bak 's/DB_HOST=localhost/DB_HOST=mysql/g' .env.testing 30 | - sed -i.bak 's/APP_HOST=localhost/APP_HOST=%/g' .env.testing 31 | - sed -i.bak 's/TEAM_DB_HOST=localhost/TEAM_DB_HOST=mysql/g' .env.testing 32 | - sed -i.bak 's/APP_DEBUG=false/APP_DEBUG=true/g' .env.testing 33 | - sed -i.bak 's/CACHE_DRIVER=array/CACHE_DRIVER=redis/g' .env.testing 34 | - sed -i.bak 's/REDIS_HOST=127.0.0.1/REDIS_HOST=redis/g' .env.testing 35 | - cp .env.testing .env 36 | - sed -i.bak -e "s/mysql:host=localhost\.*/mysql:host=mysql/" codeception.yml 37 | 38 | # Setup and prepare database (optional) 39 | - php artisan key:generate 40 | - php artisan config:clear 41 | - php artisan migrate:refresh 42 | - php artisan db:seed --env="testing" 43 | 44 | # make sure our testing tenant url is possible to reach for API coverage 45 | - echo "192.168.99.100 testingenv.myapp.dev" >> /etc/hosts 46 | 47 | # Multi Tenant MySQL setup - create extra database and user with all privileges 48 | - echo "SET GLOBAL sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql 49 | - echo "CREATE DATABASE IF NOT EXISTS myapp_testing DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql 50 | - echo "CREATE DATABASE IF NOT EXISTS myapp_testing_team DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql 51 | - echo "GRANT ALL PRIVILEGES ON *.* TO '${MYSQL_USER}'@'%' WITH GRANT OPTION;FLUSH PRIVILEGES;" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql 52 | 53 | variables: 54 | WITH_XDEBUG: "1" 55 | MYSQL_ROOT_PASSWORD: mysql 56 | MYSQL_DATABASE: myapp_testing 57 | MYSQL_USER: homestead 58 | MYSQL_PASSWORD: secret 59 | COMPOSER_HOME: /cache/composer 60 | REDIS_PORT: "6379" 61 | 62 | services: 63 | - mysql:5.7 64 | - redis 65 | 66 | # only run in feature/hotfix/bugfix branches 67 | test:php7-mysql5.7: 68 | image: tetraweb/php:7.0 69 | stage: test 70 | only: 71 | - /^(feature|hotfix|bugfix)\/.*$/ 72 | script: 73 | - vendor/bin/codecept run 74 | 75 | # Full test including coverage 76 | test:php7-mysql5.7-coverage: 77 | image: tetraweb/php:7.0 78 | stage: test 79 | only: 80 | - develop 81 | - master 82 | script: 83 | - vendor/bin/codecept run --coverage-text --no-colors 84 | 85 | 86 | ## Deployment - Staging 87 | deploy_staging: 88 | image: ubuntu:16.04 89 | stage: deploy 90 | when: manual 91 | script: 92 | - apt-get update -yqq 93 | - apt-get install -y curl 94 | - curl --request GET https://trigger.my.app/deploy-script 95 | environment: 96 | name: staging 97 | url: https://demo.myapp.com 98 | only: 99 | - develop 100 | -------------------------------------------------------------------------------- /.gitlab-ci-php-coverage-report.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Gitlab CI configuration with PHP Code Coverage HTML report 3 | # We merge multiple suits coverage reports and save those for download 4 | # 5 | 6 | stages: 7 | - test 8 | 9 | cache: 10 | # cache per-job and per-branch 11 | key: "$CI_BUILD_NAME/$CI_BUILD_REF_NAME" 12 | paths: 13 | - vendor/ 14 | 15 | before_script: 16 | - apt-get update -yqq 17 | 18 | # PHP Extensions 19 | - docker-php-ext-enable zip mbstring pdo_mysql soap mysqli gd 20 | 21 | # Install packages through composer 22 | - composer self-update 23 | - composer install --no-progress --no-interaction 24 | 25 | # run your app install commands here 26 | 27 | variables: 28 | WITH_XDEBUG: "1" 29 | MYSQL_ROOT_PASSWORD: mysql 30 | MYSQL_DATABASE: myapp_testing 31 | MYSQL_USER: homestead 32 | MYSQL_PASSWORD: secret 33 | COMPOSER_HOME: /cache/composer 34 | 35 | test:php7:full-report: 36 | image: tetraweb/php:7.0 37 | stage: test 38 | artifacts: 39 | paths: 40 | - tests/_output/coverage 41 | expire_in: 1 month 42 | script: 43 | - do_run() { vendor/bin/codecept run --coverage=$1.cov $1; }; 44 | - vendor/bin/codecept run acceptance 45 | - do_run api 46 | - do_run functional 47 | - do_run unit 48 | - vendor/bin/phpcov merge tests/_output --text=Coverage 49 | - vendor/bin/phpcov merge tests/_output --html=tests/_output/coverage 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GitLab-CI-Scripts 2 | 3 | At Napp we are using Gitlab CI to run all our application tests and deploys. 4 | These files showcases how to get started with Gitlab CI if you are a PHP developer. 5 | 6 | ## Gitlab CI YML docs 7 | 8 | https://docs.gitlab.com/ce/ci/yaml/README.html 9 | 10 | 11 | ## License 12 | 13 | MIT - free to use. 14 | 15 | ## Author 16 | 17 | Mads Møller // Napp 18 | --------------------------------------------------------------------------------