Skip to content
Snippets Groups Projects
.gitlab-ci.yml 2.92 KiB
Newer Older
Tibo's avatar
Tibo committed
stages:
  - test
  - deploy

Tibo's avatar
Tibo committed
## Cache composer packages between all jobs and all branches
## of this project...
cache:
  key: one-key-to-rull-them-all
  paths:
    - composer-cache/

Thibault Debatty's avatar
Thibault Debatty committed
# Test with PHP7.4 
test:php74:
Tibo's avatar
Tibo committed
  stage: test
  image: cylab/php:7.4
Tibo's avatar
Tibo committed
  coverage: '/^\s*Lines:\s*\d+.\d+\%/'
  services:
    - mysql:5.7
  variables:
    # Configure mysql environment variables (https://hub.docker.com/_/mysql/)
    MYSQL_DATABASE: laravel
    MYSQL_ROOT_PASSWORD: root
Tibo's avatar
Tibo committed
  before_script:
    # Install all project dependencies
Tibo's avatar
Tibo committed
    - COMPOSER_CACHE_DIR=./composer-cache composer install
Tibo's avatar
Tibo committed
    # setup Laravel
    - cp env.test .env
    - php artisan migrate
Tibo's avatar
Tibo committed
  script:
Tibo's avatar
Tibo committed
    - vendor/bin/phpunit --coverage-text --colors=never
Tibo's avatar
Tibo committed
    - vendor/bin/phpcs
    - vendor/bin/phpstan analyze --memory-limit=512M
Thibault Debatty's avatar
Thibault Debatty committed
    - vendor/bin/unused_scanner unused-scanner.php
  artifacts:
    paths:
      - storage/logs/*.log
    when: always
Tibo's avatar
Tibo committed

Thibault Debatty's avatar
Thibault Debatty committed
test:dependencies:
  image: cylab/php:7.4
Thibault Debatty's avatar
Thibault Debatty committed
  script:
    # in cylab/php:7.4, security-checker is already installed...
Thibault Debatty's avatar
Thibault Debatty committed
    - ~/.composer/vendor/bin/security-checker security:check composer.lock

Tibo's avatar
Tibo committed
test:gitleaks:
Tibo's avatar
Tibo committed
  stage: test
Tibo's avatar
Tibo committed
  image: 
    name: "zricethezav/gitleaks"
    entrypoint: [""]
  script:
Tibo's avatar
Tibo committed
    # to avoid
    # fatal: unsafe repository ('/builds/...' is owned by someone else)
    # with recent git versions
    - git config --global --add safe.directory $CI_PROJECT_DIR
Tibo's avatar
Tibo committed
    - gitleaks detect -v -c gitleaks.toml ./
Tibo's avatar
Tibo committed
    
Thibault Debatty's avatar
Thibault Debatty committed
build:
  stage: test
  ## Run on a gitlab-runner that is configured with docker-in-docker
  tags:
    - dind
  image: docker:20.10.16
  services:
    - docker:20.10.16-dind
  variables:
    DOCKER_TLS_CERTDIR: "/certs"
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker pull $CI_REGISTRY_IMAGE:latest || true
    - docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA --tag $CI_REGISTRY_IMAGE:latest .
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
    - docker push $CI_REGISTRY_IMAGE:latest

Tibo's avatar
Tibo committed
deploy:
  stage: deploy
  only:
    # only deploy when we push on the master branch
    - master
Tibo's avatar
Tibo committed
  image: alpine
  before_script:
    # install envsubst and ssh-add
    - apk add gettext openssh-client
  script:
    # create the new docker-compose.yml
    - envsubst < docker-compose.tmpl > docker-compose.yml
    # start ssh-agent and import ssh private key
    - eval `ssh-agent`
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    # add server to list of known hosts
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - touch ~/.ssh/known_hosts
    - chmod 600 ~/.ssh/known_hosts
    - echo $SSH_HOST_KEY >> ~/.ssh/known_hosts
    # upload docker-compose to the server
    - scp docker-compose.yml monitoring@$SERVER:/home/monitoring/
    # docker login and restart services
    - ssh monitoring@$SERVER "cd /home/monitoring; 
        docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY;
        docker compose up -d --scale queue=4"