---
workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event" || ($CI_PIPELINE_SOURCE == "push" && ($CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "stable"))
      when: always
    - when: never
variables:
  version: 0.0.$CI_PIPELINE_IID
stages:
  - test
  - build
  - publish
test_job:
  stage: test
  image: public.ecr.aws/docker/library/golang:1.21
  script:
    - go get -t ./...
    - go test -race -v ./...
build_job:
  stage: build
  rules:
    - if: $CI_COMMIT_REF_NAME =~ /stable/
      when: always
  image: public.ecr.aws/docker/library/golang:1.21
  parallel:
    matrix:
      - GOOS: [linux, darwin, windows]
        GOARCH: [amd64, arm64]
  script: |
    go build -o webservice_${GOOS}_${GOARCH}.bin ./*.go
  artifacts:
    paths:
      - webservice_${GOOS}_${GOARCH}.bin
    expire_in: 5 min
publish_job:
  stage: publish
  rules:
    - if: $CI_COMMIT_REF_NAME =~ /stable/
      when: always
  image: public.ecr.aws/docker/library/alpine:latest
  tags:
    - docker-privileged
  dependencies:
    - build_job
  before_script:
    - apk add --no-cache curl
  script: >
    for file in $(ls webservice_*); do
      curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \
        --upload-file "./$file" \
        "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/artifacts/${version}/$file"
    done
