stages:
  - setup
  - audit
  - lint
  - build
  - test
  - publish
  - deploy

install:
  image: node:16
  stage: setup
  script:
    - npm ci
  after_script:
    - node -v
    - npm -v
  artifacts:
    paths:
      - node_modules
    expire_in: 1 week

audit:
  image: node:16
  needs: ["install"]
  stage: audit
  script:
    - npm audit --audit-level=critical

es:lint:
  image: node:16
  needs: ["install"]
  stage: lint
  script:
    - npm run lint:es

css:lint:
  image: node:16
  needs: ["install"]
  stage: lint
  script:
    - npm run lint:css

# test:unit:
#     image: node:16
#     stage: test
#     script:
#         - npm run test:unit

build:
  image: node:16
  needs: ["install"]
  stage: build
  script:
    - NODE_ENV=test npm run build
  dependencies:
    - install
  artifacts:
    paths:
      - .next
    expire_in: 1 week
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
      - .next/cache/

test:e2e:
  image: circleci/node:16-browsers
  needs: ["install", "build"]
  stage: test
  script:
    - npm run testcafe
  artifacts:
    paths:
      - e2e-screenshots
    expire_in: 1 week
    when: on_failure

publish:dev:
  stage: publish
  image: docker:stable
  needs: ["build", "test:e2e", "es:lint", "css:lint"]
  services:
    - docker:stable-dind
  only:
    - master
  script:
    - docker info
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker build . -t "$IMAGE_NAME":latest --build-arg SENTRY_AUTH_TOKEN="$SENTRY_AUTH_TOKEN" --build-arg NEXT_PUBLIC_DEPLOY_ENV=development --build-arg NEXT_PUBLIC_API_URL=https://api.dev.sahkoinsinoorikilta.fi/api --build-arg NEXT_PUBLIC_SITE_URL=https://dev.sahkoinsinoorikilta.fi
    - docker push "$IMAGE_NAME":latest

publish:prod:
  stage: publish
  image: docker:stable
  services:
    - docker:stable-dind
  only:
    - production
  script:
    - docker info
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker build . -t "$IMAGE_NAME":prod --build-arg SENTRY_AUTH_TOKEN="$SENTRY_AUTH_TOKEN"
    - docker push "$IMAGE_NAME":prod

deploy:dev:
  stage: deploy
  image: docker:stable
  only:
    - master
  environment:
    name: dev
    url: https://dev.sahkoinsinoorikilta.fi
  variables:
    DOCKER_HOST: $DEV_CI_DOCKER_HOST
    DOCKER_TLS_VERIFY: 1
  before_script:
    - mkdir -p ~/.docker
    - echo "$DEV_TLSCACERT" > ~/.docker/ca.pem
    - echo "$DEV_TLSCERT" > ~/.docker/cert.pem
    - echo "$DEV_TLSKEY" > ~/.docker/key.pem
    - docker login -u gitlab-ci-token -p "$CI_BUILD_TOKEN" "$CI_REGISTRY"
  script:
    - docker stack deploy --with-registry-auth -c stack-compose-dev.yml "$SERVICE_NAME"
  after_script:
    - docker logout "$CI_REGISTRY"

deploy:prod:
  stage: deploy
  image: docker:stable
  only:
    - production
  environment:
    name: production
    url: https://sahkoinsinoorikilta.fi
  variables:
    DOCKER_HOST: $CI_DOCKER_HOST
    DOCKER_TLS_VERIFY: 1
  before_script:
    - mkdir -p ~/.docker
    - echo "$TLSCACERT" > ~/.docker/ca.pem
    - echo "$TLSCERT" > ~/.docker/cert.pem
    - echo "$TLSKEY" > ~/.docker/key.pem
    - docker login -u gitlab-ci-token -p "$CI_BUILD_TOKEN" "$CI_REGISTRY"
  script:
    - docker stack deploy --with-registry-auth -c stack-compose.yml "$SERVICE_NAME"
  after_script:
    - docker logout "$CI_REGISTRY"
