forked from skypilot-org/skypilot
-
Notifications
You must be signed in to change notification settings - Fork 0
132 lines (125 loc) · 4.95 KB
/
pre-merge-test.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
name: Pre-Merge Test
on:
issue_comment:
types:
- created
pull_request:
branches:
- '**'
jobs:
pre-merge-test:
if: startsWith(github.event.comment.body, '/pre-merge-test')
runs-on: ubuntu-latest
steps:
# Step 1: Debugging - Echo VALID_USERS and BUILDKITE_TOKEN
- name: Debug Secrets
env:
VALID_USERS: ${{ secrets.VALID_USERS }}
BUILDKITE_TOKEN: ${{ secrets.BUILDKITE_TOKEN }}
run: |
echo "Debugging Secrets:"
echo "VALID_USERS starts with: ${VALID_USERS:0:5}..." # Print first 5 characters
echo "VALID_USERS length: ${#VALID_USERS}"
echo "BUILDKITE_TOKEN starts with: ${BUILDKITE_TOKEN:0:5}..." # Print first 5 characters
echo "BUILDKITE_TOKEN length: ${#BUILDKITE_TOKEN}"
# Step 2: Validate User and Comment
- name: Validate User and Comment
id: validate_user
env:
VALID_USERS: ${{ secrets.VALID_USERS }}
run: |
IFS=',' read -ra VALID_USERS_ARRAY <<< "$VALID_USERS"
COMMENT_BODY="${{ github.event.comment.body }}"
COMMENT_USER="${{ github.event.sender.login }}"
ACTION="${{ github.event.action }}"
echo "Valid users: ${VALID_USERS_ARRAY[@]}"
echo "Comment user: $COMMENT_USER"
echo "Action: $ACTION"
if [[ "${ACTION}" != "created" ]]; then
echo "Invalid action: ${ACTION}"
echo "valid=false" >> $GITHUB_ENV
exit 0
fi
USER_IS_VALID=false
for VALID_USER in "${VALID_USERS_ARRAY[@]}"; do
if [[ "$VALID_USER" == "$COMMENT_USER" ]]; then
USER_IS_VALID=true
break
fi
done
if [[ "$USER_IS_VALID" == "false" ]]; then
echo "Invalid user: ${COMMENT_USER}"
echo "valid=false" >> $GITHUB_ENV
exit 0
fi
if [[ ! "${COMMENT_BODY}" =~ ^/pre-merge-test ]]; then
echo "Invalid comment format: ${COMMENT_BODY}"
echo "valid=false" >> $GITHUB_ENV
exit 0
fi
echo "valid=true" >> $GITHUB_ENV
# Step 3: Stop the job if the user/comment is invalid
- name: Exit if Invalid User/Comment
if: env.valid == 'false'
run: echo "Exiting because the user/comment is invalid."
# Step 4: Parse Environment Variables
- name: Parse Environment Variables
if: env.valid == 'true'
id: parse_envs
run: |
COMMENT_BODY="${{ github.event.comment.body }}"
ENV_ARGS=$(echo "${COMMENT_BODY}" | sed -E 's|/pre-merge-test ?||')
ALLOWED_ENVS=("aws" "azure" "gcp")
FINAL_ENVS=()
if [[ -z "${ENV_ARGS}" ]]; then
FINAL_ENVS+=("aws")
else
IFS=',' read -ra ENV_LIST <<< "${ENV_ARGS}"
for ENV in "${ENV_LIST[@]}"; do
ENV=$(echo "$ENV" | xargs)
if [[ " ${ALLOWED_ENVS[@]} " =~ " ${ENV} " ]]; then
FINAL_ENVS+=("$ENV")
fi
done
fi
if [[ ${#FINAL_ENVS[@]} -eq 0 ]]; then
FINAL_ENVS+=("aws")
fi
ENV_JSON="{"
for ENV in "${FINAL_ENVS[@]}"; do
ENV_JSON+="\"$ENV\":\"1\","
done
ENV_JSON="${ENV_JSON%,}}"
echo "Parsed environments: ${ENV_JSON}"
echo "envs=${ENV_JSON}" >> $GITHUB_ENV
# Step 5: Trigger Buildkite Pipeline
- name: Trigger Buildkite Pipeline
if: env.valid == 'true'
env:
BUILDKITE_TOKEN: ${{ secrets.BUILDKITE_TOKEN }}
run: |
PR_API_URL="${{ github.event.issue.pull_request.url }}"
AUTH_HEADER="Authorization: Bearer $BUILDKITE_TOKEN"
PR_DATA=$(curl -s -H "$AUTH_HEADER" "$PR_API_URL")
HEAD=$(echo "$PR_DATA" | jq -r '.head.ref')
COMMIT=$(echo "$PR_DATA" | jq -r '.head.sha')
PULL_REQUEST_REPO=$(echo "$PR_DATA" | jq -r '.head.repo.html_url')
BASE_BRANCH=$(echo "$PR_DATA" | jq -r '.base.ref')
PR_NUMBER=$(echo "$PR_DATA" | jq -r '.number')
TITLE=$(echo "$PR_DATA" | jq -r '.title')
BUILDKITE_PAYLOAD=$(jq -n \
--arg commit "$COMMIT" \
--arg branch "$HEAD" \
--arg message "PR #$PR_NUMBER: $TITLE triggered by: ${{ github.event.sender.login }}" \
--arg pull_request_repository "$PULL_REQUEST_REPO" \
--argjson envs "$envs" \
'{
commit: $commit,
branch: $branch,
message: $message,
pull_request_repository: $pull_request_repository,
ignore_pipeline_branch_filters: true,
env: $envs
}')
BUILDKITE_URL="https://api.buildkite.com/v2/organizations/skypilot-1/pipelines/test-smoke-tests-required-before-merging/builds"
curl -X POST -H "$AUTH_HEADER" -H "Content-Type: application/json" -d "$BUILDKITE_PAYLOAD" "$BUILDKITE_URL"