stages:
  - build
  - test
  - upload
  - release

variables:
  LINUX_AMD64_BINARY: "shift-linux-amd64-${CI_COMMIT_TAG}"
  PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/shift/${CI_COMMIT_TAG}"
  CACHE_FALLBACK_KEY: master

stack-build:
  stage: build
  image: haskell:8.10.2
  cache:
    key: "$CI_COMMIT_REF_SLUG"
    paths:
      - .stack-work
      - .stack
  before_script:
    - export STACK_ROOT="$CI_PROJECT_DIR/.stack"
  script:
    - stack --no-terminal build
    - mkdir bin
    - bash -c "cp $(stack path --local-install-root)/bin/shift bin/"
  artifacts:
    untracked: false
    expire_in: 30 days
    paths:
      - bin/

stack-test:
  stage: test
  image: haskell:8.10.2
  cache:
    key: "$CI_COMMIT_REF_SLUG"
    paths:
      - .stack-work
      - .stack
    policy: pull
  before_script:
    - export STACK_ROOT="$CI_PROJECT_DIR/.stack"
  script:
    - stack --no-terminal test --ta "--xml=report.xml"
  artifacts:
    when: always
    untracked: false
    expire_in: 30 days
    paths:
      - report.xml
    reports:
      junit: report.xml

upload:
  stage: upload
  image: curlimages/curl:latest
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - |
      curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file bin/shift ${PACKAGE_REGISTRY_URL}/${LINUX_AMD64_BINARY}

release:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - |
      release-cli create --name "Release $CI_COMMIT_TAG" --tag-name $CI_COMMIT_TAG \
        --assets-link "{\"name\":\"${LINUX_AMD64_BINARY}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${LINUX_AMD64_BINARY}\"}"
