image: golang:latest

stages:
  - build
  - release

format:
  stage: build
  script:
    - test -z "$(gofmt -l ./)"
  except:
    - tags                               # do not run for releases

vet:
  stage: build
  script:
    - go vet -json ./... | tee vet-report.json
  artifacts:
    when: always
    paths:
      - vet-report.json
    expire_in: 1 hour
  except:
    - tags                               # do not run for releases

test:
  stage: build
  script:
    - go test -coverprofile=coverage.out -json ./... | tee test-report.json
  artifacts:
    when: always
    paths:
      - test-report.json
      - coverage.out
    expire_in: 1 hour
  except:
    - tags                               # do not run for releases

staticcheck:
  stage: build
  script:
    - go install honnef.co/go/tools/cmd/staticcheck@latest # ideally we should version pin
    - staticcheck ./...
  except:
    - tags                               # do not run for releases

golint:
  stage: build
  script:
    - go install golang.org/x/lint/golint@latest # ideally we should version pin
    - golint -set_exit_status ./...
  except:
    - tags                               # do not run for releases

build:
  stage: build
  script:
    - go build ./...
  artifacts:
    # instead of manually adding i.e. the built binaries, we can instead just
    # grab anything not tracked in Git
    untracked: true
    expire_in: 1 hour
  except:
    - tags                               # do not run for releases

# sonar instance needs to be running on localhost:9000 for this to work!
sonarcloud-check:
  stage: build
  needs:
    - job: test
      artifacts: true
    - job: vet
      artifacts: true
  image:
    name: sonarsource/sonar-scanner-cli:latest
    entrypoint: [""]
  variables:
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"
    GIT_DEPTH: "0"
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script:
    - sonar-scanner
  except:
    - tags                               # do not run for releases

# to create a new release, do a release
generate_executables:
  stage: release
  rules:
    - if: $CI_COMMIT_TAG                 # Run this job when a tag is created
  before_script:
  - echo ARTIFACT_JOB_ID=$CI_JOB_ID >> executable_artifacts.env
  script:
    - ./go-executable-build.sh boilr boilr.go
    - mv builds/boilr-darwin-amd64 builds/boilr-${CI_COMMIT_TAG}-darwin-amd64
    - mv builds/boilr-darwin-arm64 builds/boilr-${CI_COMMIT_TAG}-darwin-arm64
    - mv builds/boilr-linux-amd64 builds/boilr-${CI_COMMIT_TAG}-linux-amd64
    - mv builds/boilr-linux-386 builds/boilr-${CI_COMMIT_TAG}-linux-386
    - mv builds/boilr-windows-386.exe builds/boilr-${CI_COMMIT_TAG}-windows-386.exe
    - mv builds/boilr-windows-amd64.exe builds/boilr-${CI_COMMIT_TAG}-windows-amd64.exe
  artifacts:
    paths:
      - builds/boilr-${CI_COMMIT_TAG}-darwin-amd64
      - builds/boilr-${CI_COMMIT_TAG}-darwin-arm64
      - builds/boilr-${CI_COMMIT_TAG}-linux-amd64
      - builds/boilr-${CI_COMMIT_TAG}-linux-386
      - builds/boilr-${CI_COMMIT_TAG}-windows-386.exe
      - builds/boilr-${CI_COMMIT_TAG}-windows-amd64.exe
    reports:
      # To ensure we've access to this file in the next stage
      dotenv: executable_artifacts.env

upload:
    stage: release
    image: curlimages/curl:latest
    rules:
      - if: $CI_COMMIT_TAG                 # Run this job when a tag is created
    needs:
      - job: generate_executables
        artifacts: true
    script:
        - 'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file builds/boilr-${CI_COMMIT_TAG}-darwin-amd64        ${PACKAGE_REGISTRY_URL}/boilr-${CI_COMMIT_TAG}-darwin-amd64'
        - 'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file builds/boilr-${CI_COMMIT_TAG}-darwin-arm64        ${PACKAGE_REGISTRY_URL}/boilr-${CI_COMMIT_TAG}-darwin-arm64'
        - 'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file builds/boilr-${CI_COMMIT_TAG}-linux-amd64         ${PACKAGE_REGISTRY_URL}/boilr-${CI_COMMIT_TAG}-linux-amd64'
        - 'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file builds/boilr-${CI_COMMIT_TAG}-linux-386           ${PACKAGE_REGISTRY_URL}/boilr-${CI_COMMIT_TAG}-linux-386'
        - 'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file builds/boilr-${CI_COMMIT_TAG}-windows-386.exe     ${PACKAGE_REGISTRY_URL}/boilr-${CI_COMMIT_TAG}-windows-386.exe'
        - 'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file builds/boilr-${CI_COMMIT_TAG}-windows-amd64.exe   ${PACKAGE_REGISTRY_URL}/boilr-${CI_COMMIT_TAG}-windows-amd64.exe'

release_job:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  rules:
    - if: $CI_COMMIT_TAG                 # Run this job when a tag is created
  needs:
    - job: upload
      artifacts: false
  script:
    - echo "running release_job"
    - echo "creating release $CI_COMMIT_TAG"
  release:                               # See https://docs.gitlab.com/ee/ci/yaml/#release for available properties
    tag_name: '$CI_COMMIT_TAG'
    description: '$CI_COMMIT_TAG'
    assets:
      links:
        - name: 'boilr-${CI_COMMIT_TAG}-darwin-amd64'
          url: '${PACKAGE_REGISTRY_URL}/boilr-${CI_COMMIT_TAG}-darwin-amd64'
        - name: 'boilr-${CI_COMMIT_TAG}-darwin-arm64'
          url: '${PACKAGE_REGISTRY_URL}/boilr-${CI_COMMIT_TAG}-darwin-arm64'
        - name: 'boilr-${CI_COMMIT_TAG}-linux-amd64'
          url: '${PACKAGE_REGISTRY_URL}/boilr-${CI_COMMIT_TAG}-linux-amd64'
        - name: 'boilr-${CI_COMMIT_TAG}-linux-386'
          url: '${PACKAGE_REGISTRY_URL}/boilr-${CI_COMMIT_TAG}-linux-386'
        - name: 'boilr-${CI_COMMIT_TAG}-windows-386.exe'
          url: '${PACKAGE_REGISTRY_URL}/boilr-${CI_COMMIT_TAG}-windows-386.exe'
        - name: '${PACKAGE_REGISTRY_URL}/boilr-${CI_COMMIT_TAG}-windows-amd64.exe'
          url: 'boilr-${CI_COMMIT_TAG}-windows-amd64.exe'