Commit e22f1c55 authored by Michael Tesch's avatar Michael Tesch
Browse files

Fix auto-tag: use git push + API pipeline trigger

parent 1730422e
Loading
Loading
Loading
Loading
+11 −9
Original line number Diff line number Diff line
@@ -98,23 +98,25 @@ pages:
    allow_failure: true

# Auto-tag when version in CMakeLists.txt is newer than latest git tag.
# Uses the API to create the tag so that a tag pipeline is triggered.
# Uses git push to create the tag, then the API to trigger a pipeline for it
# (git push from CI_JOB_TOKEN doesn't auto-trigger tag pipelines).
auto-tag:
  stage: tag
  image: curlimages/curl:latest
  script:
  - dnf install -y git curl
  - VERSION=$(sed -n 's/.*VERSION \([0-9]*\.[0-9]*\.[0-9]*\).*/\1/p' CMakeLists.txt)
  - |
    EXISTING=$(curl -s --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \
      "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/repository/tags/v${VERSION}" \
      | grep -c '"name"') || true
    if [ "$EXISTING" -gt 0 ]; then
    if git ls-remote --tags origin "refs/tags/v${VERSION}" | grep -q .; then
      echo "Tag v${VERSION} already exists, skipping"
    else
      echo "Creating tag v${VERSION}"
      curl -s --fail --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \
        --request POST \
        "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/repository/tags?tag_name=v${VERSION}&ref=${CI_COMMIT_SHA}"
      git tag "v${VERSION}"
      git push "https://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git" "v${VERSION}"
      echo "Triggering pipeline for v${VERSION}"
      curl -s --fail --request POST \
        --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \
        --form "ref=v${VERSION}" \
        "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/trigger/pipeline"
    fi
  rules:
  - if: $CI_COMMIT_TAG