stages:
  - test
  - build

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

before_script:
  - cp env.gitlab-ci .env
  - touch storage/app/db.sqlite
  - COMPOSER_CACHE_DIR=composer-cache composer install
  - php artisan key:generate
  - php artisan migrate

# Test with PHP7.4
test:php74:
  image: cylab/php74
  stage: test
  script:
    #- vendor/bin/phpunit --coverage-text --colors=never --coverage-html build/coverage
    - vendor/bin/phpcs
  artifacts:
    paths:
    - build/
    expire_in: 1 month

build:
  image: docker:19.03.1
  stage: build
  tags:
    - dind
  services:
    - docker:19.03.1-dind
  variables:
    DOCKER_TLS_CERTDIR: "/certs"
  before_script:
    - docker info
  script:
    - docker build -t cylab/ctf .

build:release:
  image: docker:19.03.1
  stage: build
  tags:
    - dind
  services:
    - docker:19.03.1-dind
  variables:
    DOCKER_TLS_CERTDIR: "/certs"
    IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
  before_script:
    - docker info
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build -t gitlab.cylab.be:8081/cylab/mark-web:$CI_COMMIT_REF_SLUG .
    - docker tag gitlab.cylab.be:8081/cylab/mark-web:$CI_COMMIT_REF_SLUG gitlab.cylab.be:8081/cylab/mark-web:test-webshells
    - docker push gitlab.cylab.be:8081/cylab/mark-web:test-webshells

build:tagged:
  only:
    - tags
  image: docker:19.03.1
  stage: build
  tags:
    - dind
  services:
    - docker:19.03.1-dind
  variables:
    DOCKER_TLS_CERTDIR: "/certs"
  before_script:
    - docker info
  script:
    - docker build -t cylab/mark-web:$CI_COMMIT_TAG .
    - docker tag cylab/mark-web:$CI_COMMIT_TAG cylab/mark-web:latest
    - docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
    - docker push cylab/mark-web:$CI_COMMIT_TAG
    - docker push cylab/mark-web:latest

deploy:
  image: cylab/php72
  stage: deploy
  script:
    # import ssh private key
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    # in cylab/php72, envoy is already installed...
    - ~/.composer/vendor/bin/envoy run deploy --commit="$CI_COMMIT_SHA"
