deploy-review-command #23
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: deploy-review-command | |
on: | |
repository_dispatch: | |
types: [deploy-review-command] | |
jobs: | |
debug: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Dump GitHub context | |
run: echo "$GITHUB_CONTEXT" | |
env: | |
GITHUB_CONTEXT: ${{ toJson(github.event) }} | |
# plan: | |
# uses: nepalevov/ai-dial-ci/.github/workflows/gh_environment.yml@main | |
# with: | |
# operation: plan | |
# environment_name: ${{ github.event.client_payload. | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check repository owner | |
id: owner | |
run: | | |
#!/usr/bin/env bash | |
set -x # TODO: remove after debug | |
readonly STATUS_SUCCESS="success" | |
readonly STATUS_FAILURE="failure" | |
log_error() { | |
echo "Error: $1" >&2 | |
} | |
set_failure_status() { | |
local message="$1" | |
log_error "${message}" | |
echo "status=${FAILURE_MESSAGE}" >> "${GITHUB_OUTPUT}" | |
exit 1 | |
} | |
# Validate repository owner | |
if [[ "${INPUT_DISPATCHED_REPO_OWNER}" != "${GITHUB_REPOSITORY_OWNER}" ]]; then | |
set_failure_status "The event was not dispatched by a repository within the same owner." | |
fi | |
# Validate repository name prefix | |
if [[ "${INPUT_DISPATCHED_REPO_NAME}" != ai-dial* ]]; then | |
set_failure_status "The repository name does not start with '${PREFIX}'." | |
fi | |
echo "status=${STATUS_SUCCESS}" >> "${GITHUB_OUTPUT}" | |
echo "Owner checks passed successfully" | |
env: | |
INPUT_DISPATCHED_REPO_OWNER: ${{ github.event.client_payload.github.payload.repository.owner.login || github.event.client_payload.github.event.repository.owner.login }} | |
INPUT_DISPATCHED_REPO_NAME: ${{ github.event.client_payload.github.payload.repository.name || github.event.client_payload.github.event.repository.name }} | |
- name: Gather PR info | |
id: pr_info | |
run: | | |
#!/usr/bin/env bash | |
set -x # TODO: remove after debug | |
make_api_call() { | |
local url="$1" | |
curl -s -H "${GITHUB_TOKEN}" "${url}" | |
} | |
# Function to output variables to GITHUB_OUTPUT | |
output_variable() { | |
local name="$1" | |
local value="$2" | |
echo "${name}=${value}" >>"$GITHUB_OUTPUT" | |
} | |
# If any required input value is missing, try to resolve via API | |
if [[ (-z "$INPUT_PR_NUMBER" || -z "$INPUT_HEAD_REPO" || -z "$INPUT_HEAD_REF") && -n "$INPUT_HEAD_SHA" ]]; then | |
echo "Some INPUT_ values missing but INPUT_HEAD_SHA is set, falling back to API resolution..." | |
# Get PR details by commit SHA | |
SEARCH_PR_DATA=$(make_api_call "${GITHUB_API_URL}/search/issues?q=${INPUT_HEAD_SHA}+repo:${INPUT_BASE_REPO}+is:pr") | |
PR_URL=$(echo "$SEARCH_PR_DATA" | jq -r '.items[0].pull_request.url // empty') | |
if [[ -n "$PR_URL" && "$PR_URL" != "null" ]]; then | |
PR_NUMBER=$(echo "$SEARCH_PR_DATA" | jq -r '.items[0].number // empty') | |
PR_DETAILS=$(make_api_call "$PR_URL") | |
HEAD_REPO=$(echo "$PR_DETAILS" | jq -r '.head.repo.full_name // empty') | |
HEAD_REF=$(echo "$PR_DETAILS" | jq -r '.head.ref // empty') | |
fi | |
else | |
# If all inputs are present, just assign them | |
PR_NUMBER="${INPUT_PR_NUMBER}" | |
HEAD_REPO="${INPUT_HEAD_REPO}" | |
HEAD_REF="${INPUT_HEAD_REF}" | |
fi | |
# Validate and output | |
ERROR_MESSAGE="" | |
required_vars=("PR_NUMBER" "HEAD_REPO" "HEAD_REF") | |
for var in "${required_vars[@]}"; do | |
if [[ -z "${!var}" ]]; then | |
ERROR_MESSAGE+="${var} is missing. " | |
fi | |
done | |
if [[ -n "$ERROR_MESSAGE" ]]; then | |
echo "$ERROR_MESSAGE" | |
output_variable "status" "Could not resolve required PR information: $ERROR_MESSAGE" | |
exit 1 | |
fi | |
# TODO: remove after debug? | |
echo "| Parameter | Value |" | |
echo "|-------------------|-------------|" | |
echo "| PR_NUMBER | $PR_NUMBER |" | |
echo "| HEAD_REPO | $HEAD_REPO |" | |
echo "| HEAD_REF | $HEAD_REF |" | |
output_variable "pr_number" "$PR_NUMBER" | |
output_variable "head_repo" "$HEAD_REPO" | |
output_variable "head_ref" "$HEAD_REF" | |
# Set trigger URL based on source | |
if [[ "$INPUT_TRIGGER" == "scd" ]]; then | |
TRIGGER_URL="${{ github.event.client_payload.github.payload.comment.html_url }}" | |
else | |
# Only workflow_run is supported as alternative to slash-command-dispatch for now | |
TRIGGER_URL="${{ github.event.client_payload.github.event.workflow_run.html_url }}" | |
fi | |
output_variable "trigger_url" "$TRIGGER_URL" | |
echo "PR info gathered successfully" | |
output_variable "status" "success" | |
env: | |
INPUT_PR_NUMBER: ${{ github.event.client_payload.pull_request.number }} | |
INPUT_HEAD_REPO: ${{ github.event.client_payload.pull_request.head.repo.full_name }} | |
INPUT_HEAD_REF: ${{ github.event.client_payload.pull_request.head.ref }} | |
INPUT_HEAD_SHA: ${{ github.event.client_payload.github.event.workflow_run.head_sha }} | |
INPUT_BASE_REPO: ${{ github.event.client_payload.github.payload.repository.full_name || github.event.client_payload.github.event.repository.full_name }} | |
INPUT_TRIGGER: ${{ github.event.client_payload.github.action }} | |
GITHUB_TOKEN: ${{ secrets.ACTIONS_BOT_TOKEN }} | |
- name: Deploy environment | |
id: deploy | |
run: | | |
# TODO: remove after debug? | |
echo "Deploying with the following parameters:" | |
echo "| Parameter | Value |" | |
echo "|---------------- |---------------------------------------------|" | |
echo "| GITHUB_PR | pr-${{ steps.pr_info.outputs.pr_number }} |" | |
echo "| GITHUB_REPO | ${{ steps.pr_info.outputs.head_repo }} |" | |
echo "| GITHUB_REF | ${{ steps.pr_info.outputs.head_ref }} |" | |
echo "| GITHUB_TRIGGER | ${{ steps.pr_info.outputs.trigger_url }} |" | |
echo "status=success" >>$GITHUB_OUTPUT # TODO: remove mock status after debug | |
echo "status=failure" >>$GITHUB_OUTPUT # TODO: remove mock status after debug | |
- name: Check overall status | |
id: status | |
if: success() || failure() | |
run: | | |
#!/usr/bin/env bash | |
set -x # TODO: remove after debug | |
# Constants | |
readonly SUCCESS_FLAG="✅" | |
readonly FAILURE_FLAG="❌" | |
readonly WORKFLOW_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" | |
echo "Checking deployment status..." | |
# Initialize flags and message | |
OWNER_CHECK_FLAG="$SUCCESS_FLAG" | |
PR_INFO_CHECK_FLAG="$SUCCESS_FLAG" | |
DEPLOY_CHECK_FLAG="$SUCCESS_FLAG" | |
MESSAGE="" | |
# Check owner status | |
if [[ "${{ steps.owner.outputs.status }}" != "success" ]]; then | |
OWNER_CHECK_FLAG="$FAILURE_FLAG" | |
MESSAGE+="> ${{ steps.owner.outputs.status }}"$'\n' | |
fi | |
# Check PR info status | |
if [[ "${{ steps.pr_info.outputs.status }}" != "success" ]]; then | |
PR_INFO_CHECK_FLAG="$FAILURE_FLAG" | |
MESSAGE+="> ${{ steps.pr_info.outputs.status }}"$'\n' | |
fi | |
# Check deploy status | |
if [[ "${{ steps.deploy.outputs.status }}" != "success" ]]; then | |
DEPLOY_CHECK_FLAG="$FAILURE_FLAG" | |
MESSAGE+="> ${{ steps.deploy.outputs.status }}"$'\n' | |
fi | |
# Append workflow run link if needed | |
if [[ -z "$MESSAGE" ]]; then | |
MESSAGE="> OK" | |
else | |
MESSAGE+="> Check [GitHub Workflow run](${WORKFLOW_URL}) for details" | |
fi | |
# Output markdown table | |
{ | |
echo "| Check | Status |" | |
echo "|---------------------|-----------------------|" | |
echo "| Owner Check | $OWNER_CHECK_FLAG |" | |
echo "| PR Info Check | $PR_INFO_CHECK_FLAG |" | |
echo "| Deployment Check | $DEPLOY_CHECK_FLAG |" | |
} >> "$GITHUB_STEP_SUMMARY" | |
# Store message for potential comment | |
echo "message<<EOF" >> "$GITHUB_OUTPUT" | |
echo "$MESSAGE" >> "$GITHUB_OUTPUT" | |
echo "EOF" >> "$GITHUB_OUTPUT" | |
if [[ "$INPUT_TRIGGER" == "scd" ]]; then | |
TRIGGER_URL="${{ github.event.client_payload.github.payload.comment.html_url }}" | |
echo "is_comment_trigger=true" >>$GITHUB_OUTPUT | |
fi | |
env: | |
INPUT_TRIGGER: ${{ github.event.client_payload.github.action }} | |
- name: Return status as append to comment | |
if: | | |
(success() || failure()) && | |
steps.status.outputs.is_comment_trigger == 'true' | |
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0 | |
with: | |
token: ${{ secrets.ACTIONS_BOT_TOKEN }} | |
repository: ${{ github.event.client_payload.github.payload.repository.full_name || github.event.repository.full_name }} | |
comment-id: ${{ github.event.client_payload.github.payload.comment.id }} | |
body: | | |
${{ steps.status.outputs.message }} |