-
Notifications
You must be signed in to change notification settings - Fork 22
89 lines (70 loc) · 3.75 KB
/
trigger-gitlab-pipeline.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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"