#!/bin/sh set -eu if [ "$#" -ne 3 ]; then echo "usage: $0 TAG NAME DESCRIPTION" >&2 exit 2 fi : "${CI_API_V4_URL:?CI_API_V4_URL is required}" : "${CI_PROJECT_ID:?CI_PROJECT_ID is required}" : "${CI_JOB_TOKEN:?CI_JOB_TOKEN is required}" # GitLab may advertise HTTP behind a TLS proxy; never send the token before a redirect. case "$CI_API_V4_URL" in http://*) api_url="https://${CI_API_V4_URL#http://}" ;; https://*) api_url=$CI_API_V4_URL ;; *) echo "CI_API_V4_URL must use HTTP or HTTPS" >&2 exit 2 ;; esac tag_name=$1 release_name=$2 description=$3 response_file=$(mktemp) trap 'rm -f "$response_file"' EXIT HUP INT TERM status=$(curl --silent --show-error --proto '=https' \ --output "$response_file" \ --write-out '%{http_code}' \ --request POST \ --header "JOB-TOKEN: $CI_JOB_TOKEN" \ --data-urlencode "tag_name=$tag_name" \ --data-urlencode "name=$release_name" \ --data-urlencode "description=$description" \ "$api_url/projects/$CI_PROJECT_ID/releases") case "$status" in 201) echo "GitLab release created for $tag_name" ;; 409) echo "GitLab release already exists for $tag_name" ;; *) echo "GitLab release creation failed for $tag_name (HTTP $status)" >&2 cat "$response_file" >&2 exit 1 ;; esac