Skip to content

Commit

Permalink
Merge pull request #82 from GovTechSG/MOL-14987
Browse files Browse the repository at this point in the history
[MOL-14987][BC] Update gitlab action
  • Loading branch information
chongwyb authored Mar 12, 2024
2 parents 66ce2bd + 438e77b commit b4433e0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ We have set variables in the settings of the GitHub repository that is needed to

These variables are:

- GITLAB_TOKEN - The token created in GitLab to authenticate the use of the API
- GITLAB_PAT - The personal access token by the gitlab user (currently no organisation pat)
- GITLAB_TOKEN - The gitlab pipeline trigger token created in GitLab to authenticate the use of the API ([instruction](https://docs.gitlab.com/ee/ci/triggers/#create-a-pipeline-trigger-token))
- GITLAB_ENDPOINT - The host domain of GitLab
- GITLAB_PROJECT_ID - The project ID of the GitLab pipeline that builds the lib
- GITLAB_REPO_NAME - The name of the repository being built
Expand All @@ -39,3 +40,5 @@ The API contains variables that are important to be passed in the body of the re
- PIPELINE_COMMIT_MSG - the 1st line of the commit message

Then the Project in GitLab responsible for running the pipeline will create and start the pipeline.

The GitHub action is marked as successful when the GitLab pipeline's `build-dist-job` is successful. After that you are able to deploy to storybook/github/npm.
38 changes: 27 additions & 11 deletions .github/workflows/trigger-gitlab-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,41 @@ jobs:
GITLAB_PROJECT_ID: ${{ vars.GITLAB_PROJECT_ID }}
SLEEP_DURATION_MINUTES: 15
CHECK_INTERVAL_MINUTES: 5
DOWNSTREAM_PIPELINE_NAME: library
DOWNSTREAM_JOB_NAME: build-dist-job
run: |
echo "Pipeline ID: $PIPELINE_ID"
echo "[ Running pipline check after $SLEEP_DURATION_MINUTES minutes ]"
sleep $(($SLEEP_DURATION_MINUTES*60))
pipeline_status=""
until [[ $pipeline_status == "success" || $pipeline_status == "failed" || $pipeline_status == "canceled" || $pipeline_status == "skipped" ]]
num_attempts=1
job_status=""
until [[ $num_attempts -gt 5 || $job_status == "success" || $job_status == "failed" || $job_status == "canceled" || $job_status == "skipped" ]]
do
pipeline_status=$(curl -sS --request GET --header "PRIVATE-TOKEN: $GITLAB_PAT" --url "$GITLAB_ENDPOINT/$GITLAB_PROJECT_ID/pipelines/$PIPELINE_ID" | jq -r '.status')
echo "Pipeline status: $pipeline_status"
sleep $(($CHECK_INTERVAL_MINUTES*60))
echo "Attempt: $num_attempts"
downstream_pipeline_id=$(curl -sS --request GET --header "PRIVATE-TOKEN: $GITLAB_PAT" \
--url "$GITLAB_ENDPOINT/$GITLAB_PROJECT_ID/pipelines/$PIPELINE_ID/bridges" \
| jq -r '[ .[] | select( .name | contains("'"$DOWNSTREAM_PIPELINE_NAME"'")) ]' | jq -r '.[].downstream_pipeline.id')
echo "($DOWNSTREAM_PIPELINE_NAME) Downstream pipeline id: $downstream_pipeline_id"
job_status=$(curl -sS --header "PRIVATE-TOKEN: $GITLAB_PAT" \
--url "$GITLAB_ENDPOINT/$GITLAB_PROJECT_ID/pipelines/$downstream_pipeline_id/jobs" \
| jq -r '[ .[] | select( .name | contains("'"$DOWNSTREAM_JOB_NAME"'")) ]' | jq -r '.[].status')
echo "($DOWNSTREAM_JOB_NAME) Job status: $job_status"
if [[ $job_status == "running" ]]; then
num_attempts=$((num_attempts+1))
sleep $(($CHECK_INTERVAL_MINUTES*60))
fi
done
[[ $pipeline_status == "canceled" ]] && echo "GitLab Pipeline was cancelled"
[[ $num_attempts -gt 5 ]] && echo "Number of retries exceeded, please head to the GitLab job to check what happened" && exit 1
[[ $job_status == "canceled" ]] && echo "GitLab job was cancelled"
[[ $pipeline_status == "skipped" ]] && echo "GitLab pipeline has stopped, please head to the GitLab pipeline to trigger job manually"
[[ $job_status == "skipped" ]] && echo "GitLab job has stopped, please head to the GitLab job to trigger job manually"
[[ $pipeline_status == "failed" ]] && echo "GitLab pipeline has failed, please head to the GitLab pipeline to check on error" && exit 1
[[ $job_status == "failed" ]] && echo "GitLab job has failed, please head to the GitLab job to check on error" && exit 1
[[ $pipeline_status == "success" ]] && echo "GitLab pipeline is successfully completed, please head to the GitLab pipeline for further deployments"
[[ $job_status == "success" ]] && echo "GitLab job is successfully completed, please head to the GitLab job for further deployments"

0 comments on commit b4433e0

Please sign in to comment.