image: docker:latest

variables:
  DOCKER_DRIVER: overlay2
  IMAGE_NAME: gitlab.nexdev.uk:5555/web-dev/users

stages:
  - prebuild
  - build
  - testing
  - release
  - deploy

docker_lint:
  stage: prebuild
  image: hadolint/hadolint:latest-debian
  script:
  # Ignoring DL3008 - Pin Versions of apt-get modules
  - hadolint --ignore DL3008 Dockerfile
  tags:
  - nexus_runner

build_image:
  stage: build
  script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN gitlab.nexdev.uk:5555
    - docker build -t $IMAGE_NAME:$CI_COMMIT_SHA .
    - docker push $IMAGE_NAME:$CI_COMMIT_SHA
  tags:
    - nexus_runner

rubocop_lint:
  variables:
    PRONTO_GITLAB_API_ENDPOINT: "https://gitlab.nexdev.uk/api/v4"
  stage: testing
  image: $IMAGE_NAME:$CI_COMMIT_SHA
  script:
  - cd /builds/web-dev/users
  - git fetch
  # - bundle exec pronto run -c=origin/master --exit-code -f gitlab
  tags:
  - nexus_runner

dependency_scanning:
  stage: testing
  image: ruby:2.6.2
  allow_failure: true
  script:
    - gem install bundler-audit
    - bundle audit check --update
  artifacts:
    paths: [gl-dependency-scanning-report.json]
  tags:
    - nexus_runner

test:unit:
  stage: testing
  image: $IMAGE_NAME:$CI_COMMIT_SHA
  services:
    - name:   postgres:12.2
      alias:  user_db
    - name:   mysql:5.6
      alias:  mysql
  variables:
    # Set variables for the MySQL image
    FW_DB_TEST_HOST: mysql
    MYSQL_DATABASE: nexus_api_forum_test
    MYSQL_USER: runner
    MYSQL_PASSWORD: password
    MYSQL_ROOT_PASSWORD: password
    OTP_ENC_KEY: 'avQrhpsxSNtx26xdM6M65Z7KUARctq5z'   
    TWILIO_NUMBER: '+15005550006'
    TWILIO_SID: 'ACe28fd8b14f682e46536a3a2910f5ea0e'
    TWILIO_TOKEN: 'd117d8463ebf05cf8ed5b9584b1a6d66'
    POSTGRES_PASSWORD: password
  script:
    - bundle exec rake db:create RAILS_ENV=test
    - bundle exec rake db:migrate RAILS_ENV=test
    - bundle exec rake fw_test_db:import_forum RAILS_ENV=test
    - COVERAGE=true bundle exec rspec --format documentation
  tags:
    - nexus_runner

test:integration:
  stage: testing
  image: $IMAGE_NAME:$CI_COMMIT_SHA
  allow_failure: true
  services:
    - name:   postgres:12.2
      alias:  user_db
    - name:   mysql:5.6
      alias:  mysql
  variables:
    # Set variables for the MySQL image
    FW_DB_TEST_HOST: mysql
    MYSQL_DATABASE: nexus_api_forum_test
    MYSQL_USER: runner
    MYSQL_PASSWORD: password
    MYSQL_ROOT_PASSWORD: password
    OTP_ENC_KEY: 'avQrhpsxSNtx26xdM6M65Z7KUARctq5z'   
    TWILIO_NUMBER: '+15005550006'
    TWILIO_SID: 'ACe28fd8b14f682e46536a3a2910f5ea0e'
    TWILIO_TOKEN: 'd117d8463ebf05cf8ed5b9584b1a6d66'
    POSTGRES_PASSWORD: password
  script:
    - bundle exec rake db:create RAILS_ENV=test
    - bundle exec rake db:migrate RAILS_ENV=test
    - bundle exec rake fw_test_db:import_forum RAILS_ENV=test
    - bundle exec rspec --format documentation --tag integration
  tags:
    - nexus_runner

release:version:
  image: gitlab.nexdev.uk:5555/pub/nexus-release-image/master
  stage: release
  before_script:
    - eval $(ssh-agent -s)
    - echo "$CI_DEPLOY_KEY" | tr -d '\r' | ssh-add -
    - ssh-keyscan gitlab.nexdev.uk >> ~/.ssh/known_hosts
  script:
    - /build/release.sh
  tags:
    - nexus_runner
  only:
    - master

release_staging:
  stage: release
  script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN gitlab.nexdev.uk:5555

    - docker tag $IMAGE_NAME:$CI_COMMIT_SHA $IMAGE_NAME:staging
    - docker push $IMAGE_NAME:$CI_COMMIT_SHA
    - docker push $IMAGE_NAME:staging
  only:
    - staging
  tags:
    - nexus_runner

deploy_staging:
  stage: deploy
  image: nexusmods/gitlab-ci-kube-deploy
  script:
    - mkdir /root/.kube
    - echo "$STAGING_KUBE_CONFIG" > /root/.kube/config
    - kubectl apply -f deploy/staging
    - kubectl -n staging-nexus set image deployment/users users=$IMAGE_NAME:$CI_COMMIT_SHA
  environment: staging
  when: manual
  except:
    - master
  tags:
    - nexus_runner

release:
  stage: release
  script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN gitlab.nexdev.uk:5555

    - docker tag $IMAGE_NAME:$CI_COMMIT_SHA $IMAGE_NAME:latest
    - docker push $IMAGE_NAME:$CI_COMMIT_SHA
    - docker push $IMAGE_NAME:latest
  only:
    - release
    - master
  tags:
    - nexus_runner

deploy_production:
  stage: deploy
  image: nexusmods/gitlab-ci-kube-deploy
  script:
    - mkdir /root/.kube
    - echo "$KUBE_CONFIG" > /root/.kube/config
    - kubectl apply -f deploy/production
    - kubectl -n production set image deployment/users users=$IMAGE_NAME:$CI_COMMIT_SHA
  environment: production
  only:
    - master
  tags:
    - nexus_runner