Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre-merge-test GitHub Actions workflow #7

Merged
merged 4 commits into from
Dec 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions .github/workflows/pre-merge-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,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"
Loading