Skip to content

Commit

Permalink
[MISC][WS] add junit and trigger gitlab pipeline workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
WAYNE SEE authored and wsee911 committed Aug 3, 2023
1 parent a73a959 commit 8dcfd40
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 45 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# GitHub Workflow for LifeSG libs

This guide will breifly explain what how the GitHub workflow works for LifeSG and the necessary setups to get it working on another LifeSG GitHub repository.

## <u>trigger-gitlab-pipeline.yml</u>

This file is responsible for creating the GitHub workflow and the job that triggers the GitLab pipeline.

--

## <u>Trigger Pipeline Job</u>

When a GitHub `push on master branch` or `pull request` is made it will trigger the job.

Pull Request events that will trigger the pipeline are:

- `opened`: when opening of a PR
- `synchronize`: when a new commit is added to the PR
- `reopened`: when reopening of a PR

We have set variables in the settings of the GitHub repository that is needed to be passed into the trigger pipeline API.

These variables are:

- GITLAB_TOKEN - The token created in GitLab to authenticate the use of the API
- 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

The job will make a `curl` to the GitLab pipeline trigger API.

The API contains variables that are important to be passed in the body of the request:

- PIPELINE_PROJECT_NAME - the repo name
- PIPELINE_PROJECT_URL - the github url of the repo
- PIPELINE_EVENT - the workflow trigger event type
- PIPELINE_BRANCH_NAME - the branch being built
- PIPELINE_TRIGGER_PERSON - the person that triggered the workflow
- 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.
89 changes: 89 additions & 0 deletions .github/workflows/trigger-gitlab-pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: Trigger GitLab CI Pipeline

on:
push:
branches:
- master
- "release/**"
- "!release/**-published"
pull_request:
types: [opened, synchronize, reopened]

jobs:
trigger_pipeline:
runs-on: ubuntu-latest

steps:
- name: Print Configs
run: |
[[ $GITHUB_EVENT_NAME = "pull_request" ]] && BRANCH_NAME="${{ github.head_ref }}" || BRANCH_NAME="${{ github.ref_name }}"
[[ $GITHUB_EVENT_NAME = "pull_request" ]] && COMMIT_MSG="${{ github.event.pull_request.title }}" || COMMIT_MSG=$(echo -e "${{ github.event.head_commit.message }}" | head -n 1)
PIPELINE_PROJECT_URL="github.com/$GITHUB_REPOSITORY.git"
echo "BRANCH_NAME=$BRANCH_NAME" >> "$GITHUB_ENV"
echo "COMMIT_MSG=$COMMIT_MSG" >> "$GITHUB_ENV"
echo "PIPELINE_PROJECT_URL=$PIPELINE_PROJECT_URL" >> "$GITHUB_ENV"
echo "PIPELINE_PROJECT_URL: $PIPELINE_PROJECT_URL"
echo "PIPELINE_EVENT: $GITHUB_EVENT_NAME"
echo "PIPELINE_BRANCH_NAME: $BRANCH_NAME"
echo "PIPELINE_TRIGGER_PERSON: $GITHUB_ACTOR"
echo "PIPELINE_COMMIT_MSG: $COMMIT_MSG"
- name: Trigger GitLab Pipeline
env:
GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
GITLAB_ENDPOINT: ${{ vars.GITLAB_ENDPOINT }}
GITLAB_PROJECT_ID: ${{ vars.GITLAB_PROJECT_ID }}
GITLAB_REPO_NAME: ${{ vars.GITLAB_REPO_NAME }}
run: |
response=$(curl -sS --request POST \
--fail \
--form token=$GITLAB_TOKEN \
--form "ref=main" \
--form "variables[PIPELINE_PROJECT_NAME]=$GITLAB_REPO_NAME" \
--form "variables[PIPELINE_PROJECT_URL]=$PIPELINE_PROJECT_URL" \
--form "variables[PIPELINE_EVENT]=$GITHUB_EVENT_NAME" \
--form "variables[PIPELINE_BRANCH_NAME]=$BRANCH_NAME" \
--form "variables[PIPELINE_TRIGGER_PERSON]=$GITHUB_ACTOR" \
--form "variables[PIPELINE_COMMIT_MSG]=$COMMIT_MSG" \
--url "$GITLAB_ENDPOINT/$GITLAB_PROJECT_ID/trigger/pipeline")
echo "Pipeline URL: $(echo $response | jq -r '.web_url')"
PIPELINE_ID=$(echo $response | jq -r '.id')
echo "PIPELINE_ID=$PIPELINE_ID" >> "$GITHUB_ENV"
echo "Pipeline ID: $PIPELINE_ID"
- name: Check GitLab Pipeline
env:
GITLAB_PAT: ${{ secrets.GITLAB_PAT }}
GITLAB_ENDPOINT: ${{ vars.GITLAB_ENDPOINT }}
GITLAB_PROJECT_ID: ${{ vars.GITLAB_PROJECT_ID }}
SLEEP_DURATION_MINUTES: 15
CHECK_INTERVAL_MINUTES: 5
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" ]]
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))
done
[[ $pipeline_status == "canceled" ]] && echo "GitLab Pipeline was cancelled"
[[ $pipeline_status == "skipped" ]] && echo "GitLab pipeline has stopped, please head to the GitLab pipeline to trigger job manually"
[[ $pipeline_status == "failed" ]] && echo "GitLab pipeline has failed, please head to the GitLab pipeline to check on error" && exit 1
[[ $pipeline_status == "success" ]] && echo "GitLab pipeline is successfully completed, please head to the GitLab pipeline for further deployments"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*


# Runtime data
pids
*.pid
Expand Down Expand Up @@ -646,3 +647,4 @@ $RECYCLE.BIN/

dist
jest.json
junit.xml
Loading

0 comments on commit 8dcfd40

Please sign in to comment.