stages:
  - Build
  - Release

variables:
  PACKAGE_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/naturvardarna-site/${CI_COMMIT_TAG}/naturvardarna-site.zip"

"Build":
  stage: Build
  image: node:alpine
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - npm ci
    - npm run build
    - apk add --no-cache zip curl
    - cd dist && zip -r ../naturvardarna-site.zip . && cd ..
    - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file naturvardarna-site.zip "${PACKAGE_URL}"'
  artifacts:
    paths:
      - dist/
    expire_in: 1 week

"Create Release":
  stage: Release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - echo "Creating permanent release for tag $CI_COMMIT_TAG"
  release:
    name: "Release $CI_COMMIT_TAG"
    description: "Automated release of the Naturvärdarna static site."
    tag_name: "$CI_COMMIT_TAG"
    ref: "$CI_COMMIT_TAG"
    assets:
      links:
        - name: "Compiled Site (ZIP)"
          url: "$PACKAGE_URL"
