Skip to content

Commit

Permalink
add gitlab-ci-pipeline.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanguedj committed Mar 5, 2024
1 parent 3865eac commit ccb9e78
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions gitlab-ci-pipeline.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

#
# A example to query Gitlab CI/CD pipeline API
#
# 1. generate a trigger token
# 2. trigger a new pipeline with variables
# 3. get pipeline info
# 4. delete trigger token
#
# GITLAB_API_TOKEN must be defined: api scope token generated from https://gitlab.com/-/user_settings/personal_access_tokens

GITLAB_PROJECT_ID="15835569" # https://gitlab.com/etalab/data.gouv.fr/infra

# https://docs.gitlab.com/ee/api/pipeline_triggers.html#create-a-trigger-token
response=$(curl -s -X POST --header "PRIVATE-TOKEN: $GITLAB_API_TOKEN" \
--form description="my description" "https://gitlab.com/api/v4/projects/$GITLAB_PROJECT_ID/triggers")

trigger_token=$(echo "$response" | jq -r '.token')
trigger_id=$(echo "$response" | jq -r '.id')
echo "# generated a new trigger token $trigger_id"

# https://docs.gitlab.com/ee/ci/triggers/#pass-cicd-variables-in-the-api-call
response=$(curl -s -X POST \
--form token="$trigger_token" \
--form ref=ci-create-release \
--form variables[API_CALL]="true" \
--form variables[REPOSITORY_URL]="[email protected]:etalab/data.gouv.fr/simple-scaffold.git" \
--form variables[REPOSITORY_NAME]="simple-scaffold" \
--form variables[RELEASE_VERSION]="1.0.0" \
"https://gitlab.com/api/v4/projects/$GITLAB_PROJECT_ID/trigger/pipeline")
pipeline_status=$(echo "$response" | jq -r '.status')
pipeline_id=$(echo "$response" | jq -r '.id')
if [[ $pipeline_status != "created" ]]; then
exit 1
fi
echo "# triggered pipeline $pipeline_id"

# pipeline statuses are listed here: https://docs.gitlab.com/ee/api/pipelines.html#list-project-pipelines
pipeline_success_statuses=("success")
pipeline_failure_statuses=("failed" "canceled" "skipped")
#pipeline_pending_statuses=("waiting_for_resource" "preparing" "pending" "running" "manual" "scheduled")

retries=50
while ((retries > 0)); do
# https://docs.gitlab.com/ee/api/pipelines.html#get-a-single-pipeline
response=$(curl -s -X GET \
--header "PRIVATE-TOKEN: ${GITLAB_API_TOKEN}" \
"https://gitlab.com/api/v4/projects/$GITLAB_PROJECT_ID/pipelines/$pipeline_id")
pipeline_status=$(echo "$response" | jq -r '.status')
pipeline_finished_at=$(echo "$response" | jq -r '.finished_at')

if printf '%s\n' "${pipeline_success_statuses[@]}" | grep -q "^$pipeline_status$"; then
echo "# pipeline $pipeline_id finished at $pipeline_finished_at"
break
elif printf '%s\n' "${pipeline_failure_statuses[@]}" | grep -q "^$pipeline_status$"; then
echo "# pipeline $pipeline_id failed with status $pipeline_status"
break
fi
echo "# waiting for pipeline $pipeline_id to finish..."
sleep 5
((retries --))
done

if ((retries == 0)); then
echo "# pipeline $pipeline_id timed out with status $pipeline_status"
fi

# https://docs.gitlab.com/ee/api/pipeline_triggers.html#remove-a-pipeline-trigger-token
echo "# deleting trigger token $trigger_id"
curl --request DELETE --header "PRIVATE-TOKEN: $GITLAB_API_TOKEN" \
"https://gitlab.com/api/v4/projects/$GITLAB_PROJECT_ID/triggers/$trigger_id"

0 comments on commit ccb9e78

Please sign in to comment.