Skip to content

Commit

Permalink
Reduce check time onBuildkite (#1284)[backend][frontend]
Browse files Browse the repository at this point in the history
* chore(check): update buildkite pipeline

* test(check): test yml

* test(check): add env

* test(check): add branch into env

* test(check): update deps

* test(check): order

* test(check): steps

* fix(check): check status and conclusion

* fix(check): update to main branch
  • Loading branch information
guzhongren authored Mar 24, 2024
1 parent a006985 commit e6b2e67
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 95 deletions.
87 changes: 17 additions & 70 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,38 @@ env:
RETENTION_DAYS: "10"

steps:
- label: ":white_check_mark: Check Shell"
key: "check-shell"
command: ./ops/check.sh shell

- label: ":lock: Check Security"
key: "check-security"
command: ./ops/check.sh security

- label: ":lock: Check CSS px"
key: "check-px"
command: ./ops/check.sh px

- label: ":lock: Deny CSS hex"
key: "deny-css-hex-check"
command: ./ops/check.sh hex

- label: ":lock: Deny CSS rgba"
key: "deny-css-rgba-check"
command: ./ops/check.sh rgba

- label: ":lock: Check .* in backend"
key: "check-dot-star"
command: ./ops/check.sh dot-star

- label: ":white_check_mark: Check Backend"
if: build.branch == "main" && build.message =~ /(?i)\[backend\]/
key: "check-backend"
command: ./ops/check.sh backend

- label: ":white_check_mark: Check Frontend"
if: build.branch == "main" && build.message =~ /(?i)\[frontend\]/
key: "check-frontend"
command: ./ops/check.sh frontend

- label: ":mag: Check Frontend License"
key: "check-frontend-license"
commands: ./ops/check.sh frontend-license

- label: ":mag: Check Backend License"
key: "check-backend-license"
commands: ./ops/check.sh backend-license
plugins:
- artifacts#v1.9.3:
upload:
- "backend/build/reports/dependency-license/**/*"
name: "backend-license-report"
expire_in: "${RETENTION_DAYS} days"

- label: ":cloudformation: Deploy infra"
if: build.branch == "main" && build.message =~ /(?i)\[infra\]/
key: "deploy-infra"
depends_on:
- "check-shell"
- "check-security"
- "check-frontend"
- "check-px"
- deny-css-rgba-check
- deny-css-hex-check
- "check-backend"
- "check-frontend-license"
- "check-backend-license"
env:
AWSHost: "$AWS_HOST"
AWSAccountId: "$AWS_ACCOUNT_ID"
AWSRegion: "$AWS_REGION"
command: ./ops/deploy.sh infra

- label: ":white_check_mark: GitHub Basic Check"
if: build.branch == "main"
key: "check-github-basic"
command: ./ops/check.sh github-basic-passed
env:
COMMIT_SHA: "$BUILDKITE_COMMIT"
GITHUB_TOKEN: "$E2E_TOKEN_GITHUB"
BRANCH: "$BUILDKITE_BRANCH"
depends_on:
- "deploy-infra"

- label: ":react: Build Frontend"
if: build.branch == "main" && build.message =~ /(?i)\[frontend\]/
key: "build-frontend"
depends_on: "deploy-infra"
depends_on:
- "check-github-basic"
command: ./ops/build.sh frontend

- label: ":java: Build Backend"
if: build.branch == "main" && build.message =~ /(?i)\[backend\]/
key: "build-backend"
depends_on: "deploy-infra"
depends_on:
- "check-github-basic"
command: ./ops/build.sh backend

- label: ":rocket: Deploy e2e"
Expand All @@ -87,22 +42,14 @@ steps:
depends_on:
- "build-frontend"
- "build-backend"
- "check-github-basic"
command: ./ops/deploy.sh e2e

- label: ":rocket: Run e2e"
branches: main
if: build.branch == "main"
key: "check-e2e"
depends_on:
- "deploy-e2e"
- "check-shell"
- "check-security"
- "check-frontend"
- "check-px"
- deny-css-rgba-check
- deny-css-hex-check
- "check-backend"
- "check-frontend-license"
- "check-backend-license"
command: ./ops/check.sh e2e-container
plugins:
- artifacts#v1.9.3:
Expand Down
66 changes: 41 additions & 25 deletions ops/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -161,49 +161,65 @@ buildkite_e2e_deployed_check() {

github_actions_passed_check() {

MAX_ATTEMPTS="${MAX_ATTEMPTS:-40}"
MAX_ATTEMPTS="${MAX_ATTEMPTS:-80}"
SLEEP_DURATION_SECONDS="${SLEEP_DURATION_SECONDS:-30}"
BRANCH="${BRANCH:-"main"}"
GITHUB_TOKEN="${GITHUB_TOKEN:-empty GitHub token}"
COMMIT_SHA="${COMMIT_SHA:-empty commit sha}"
WORKFLOW_RUN_NAME="${WORKFLOW_RUN_NAME:-Build and Deploy}"
JOB_ID_NAME="${JOB_ID_NAME:-deploy-infra}"
GITHUB_REPO_NAME="${GITHUB_REPO_NAME:-au-heartbeat/Heartbeat}"

attempt=1

jobs_url=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${GITHUB_REPO_NAME}/actions/runs?event=push&branch=main" \
| jq -r ".workflow_runs[] | select(.head_sha == \"$COMMIT_SHA\" and .name == \"Build and Deploy\") | .jobs_url")
echo "The jobs URL is: $jobs_url"
"https://api.github.com/repos/${GITHUB_REPO_NAME}/actions/runs?event=push&branch=$BRANCH")
echo "The current build response: ${response:0:100}"

while [ $attempt -le "$MAX_ATTEMPTS" ]; do
echo "🍗 Attempt $attempt: Checking if the GitHub job basic check(deploy-infra) is completed..."
is_empty=$(echo "$response" | jq 'length == 0')
if [ "$is_empty" == "true" ]; then
echo "🍗 The current GitHub actions job basic check($JOB_ID_NAME) has not been executed"
sleep "$SLEEP_DURATION_SECONDS"
continue
fi

jobs_url=$(echo "$response"| jq -r ".workflow_runs[] | select(.head_sha == \"$COMMIT_SHA\" and .name == \"$WORKFLOW_RUN_NAME\") | .jobs_url")
echo "The jobs URL is: $jobs_url"

if [ -z "$jobs_url" ]; then
echo "🍗 The current GitHub Actions baisc check(deploy-infra) has not been created"
sleep "$SLEEP_DURATION_SECONDS"
continue
fi
echo "🍗 Attempt $attempt: Checking if the GitHub job basic check($JOB_ID_NAME) is completed..."
if [ -z "$jobs_url" ]; then
echo "🍗 The current GitHub Actions baisc check($JOB_ID_NAME) has not been created"
sleep "$SLEEP_DURATION_SECONDS"
continue
fi

job_response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"$jobs_url")

deploy_infra_status=$(echo "$job_response" | jq -r ".jobs[] | select(.name == \"$JOB_ID_NAME\") | .status")
deploy_infra_conclusion=$(echo "$job_response" | jq -r ".jobs[] | select(.name == \"$JOB_ID_NAME\") | .conclusion")

deploy_infra_result=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/au-heartbeat/Heartbeat/actions/runs/8402368469/jobs"\
| jq -r ".jobs[] | select(.name == \"deploy-infra\") | .status"
)
echo "$deploy_infra_result"
if [ "$deploy_infra_result" = "completed" ]; then
echo "🎉 The GitHub basic check(deploy-infra) job is completed"
echo "$deploy_infra_conclusion"
if [ "$deploy_infra_status" = "completed" ]; then
echo "🍗 The GitHub basic check($JOB_ID_NAME) job is completed"
if [ "$deploy_infra_conclusion" = "success" ]; then
echo "🎉 The GitHub basic check($JOB_ID_NAME) job is successful"
exit 0
else
echo "🍗 The GitHub basic check(deploy-infra) job is not completed yet. Waiting for $SLEEP_DURATION_SECONDS seconds..."
sleep "$SLEEP_DURATION_SECONDS"
((attempt++))
fi
else
echo "❌ Error: The GitHub basic check($JOB_ID_NAME) job did complete but failed or skipped"
exit 2
fi
else
echo "🍗 The GitHub basic check($JOB_ID_NAME) job is not completed yet. Waiting for $SLEEP_DURATION_SECONDS seconds..."
sleep "$SLEEP_DURATION_SECONDS"
((attempt++))
fi
done

echo "❌ Error: The GitHub basic check(deploy-infra) job did not complete within the specified number of attempts."
echo "❌ Error: The GitHub basic check($JOB_ID_NAME) job did not complete within the specified number of attempts."
exit 1
}

Expand Down

0 comments on commit e6b2e67

Please sign in to comment.