stages:
  - build
  - test

compile:
  stage: build
  image: golang:alpine
  script:
    - source ./env
    - go build -buildmode=pie -tags "$TAGS" -ldflags "$LDFLAGS" ./cmd/hepto.go
  artifacts:
    paths:
      - hepto
    expire_in: 1 week
  variables:
    GIT_FETCH_EXTRA_FLAGS: --tags
  before_script:
    - apk add --no-cache git gcc g++ libseccomp-dev libseccomp-static
    - git fetch --tags
    - mkdir -p .go
  cache:
    paths:
      - .go/pkg/mod/

.before_script_template: &ansible-job
  image: docker.tedomum.net/acides/ansible-ci:main
  before_script:
    - chmod 750 ansible
    - echo "$VAULT_PASSWORD" > /tmp/vault
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SSH_KEY"|base64 -d)
  variables:
    # The ansible vault only contains secret variables, cluster config itself
    # is set from the documentation inventory
    ANSIBLE_ARGS: -i ../.ci-vault -i inventories/sample-cloud.yaml --vault-password-file /tmp/vault -e node_prefix=ci-$CI_JOB_ID

upload:
  stage: test
  needs:
    - compile
  <<: *ansible-job
  script:
    - cd ansible/
    - ansible $ANSIBLE_ARGS -m include_tasks -a file=playbooks/ciupload.yaml localhost
  
test-cluster:
  stage: test
  needs:
    - upload
  dependencies: []
  <<: *ansible-job
  parallel:
    matrix:
      - PROVIDER: scaleway
        IMAGE:
        - debian12
      - PROVIDER: hcloud
        IMAGE:
        - debian12
  script:
    - cd ansible/
    - ansible-playbook $ANSIBLE_ARGS playbooks/deploy.yaml -e cloud_provider=$PROVIDER -e image=$IMAGE
    - ansible-playbook $ANSIBLE_ARGS playbooks/test_podinfo.yaml -e cloud_provider=$PROVIDER -e image=$IMAGE
  after_script:
    - cd ansible/
    - ansible-playbook $ANSIBLE_ARGS playbooks/cleanup.yaml -e cloud_provider=$PROVIDER -e image=$IMAGE
