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

ci: add dynamic URL handling, retry mechanism, and error reporting #846

Merged
merged 20 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
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
158 changes: 53 additions & 105 deletions .github/workflows/pull.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,62 @@ name: Pull Request Checker
on:
pull_request:

permissions: write-all

jobs:
start:
name: Test [Dev Mode]
strategy:
matrix:
node-version: [16.x, 18.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Cache pnpm modules
uses: actions/cache@v3
env:
cache-name: cache-pnpm-modules
with:
path: ~/.pnpm-store
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ matrix.node-version }}-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-${{ matrix.node-version }}-
- uses: pnpm/action-setup@v2.4.0
with:
version: 8.x
run_install: false
- name: Install Dependencies
run: |
pnpm i --no-optional
- name: Start MongoDB
uses: supercharge/mongodb-github-action@v1.10.0
with:
mongodb-version: 4.4
- name: Start Redis
uses: supercharge/redis-github-action@1.6.0
with:
redis-version: 6
- name: Start project & test
run: |
nohup pnpm run dev > /dev/null 2>&1 &
pid=$!
sleep 60 # wait for server to start
bash ./scripts/workflow/test-request.sh pid=$pid dev
build:
name: Build & Upload [Bundle Mode]
needs: start
strategy:
matrix:
node-version: [16.x, 18.x]
os: [ubuntu-latest, macos-latest]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand Down Expand Up @@ -48,6 +95,7 @@ jobs:
name: PR-#${{ github.event.pull_request.number }}-mog-core-bundle-${{ runner.os }}-${{ matrix.node-version }}.zip

test:
name: Test [Bundle Mode]
needs: build
strategy:
matrix:
Expand Down Expand Up @@ -75,108 +123,8 @@ jobs:
# continue-on-error: true
run: |
unzip ./build/mog-core-bundle-Linux.zip -d ./
wget https://raw.githubusercontent.com/mogland/core/main/scripts/workflow/test-server.sh -O test-server.sh
cd ./out
# wget https://raw.githubusercontent.com/mogland/core/main/scripts/workflow/test-server.sh -O test-server.sh
wget https://raw.githubusercontent.com/${{ github.repository }}/${{ github.sha }}/scripts/workflow/test-server.sh -O test-server.sh
wget https://raw.githubusercontent.com/${{ github.repository }}/${{ github.sha }}/scripts/workflow/test-request.sh -O test-request.sh
bash test-server.sh
# - name: Reply PR Failed (if not draft PR)
# if: ${{ failure() && github.event.pull_request.draft == false }}
# uses: peter-evans/create-or-update-comment@v2
# with:
# issue-number: ${{ github.event.pull_request.number }}
# body: |
# Hi, @${{ github.event.pull_request.user.login }}. Test **failed** on ${{ matrix.os }} with Node.js ${{ matrix.node-version }}.

# - OS: ${{ matrix.os }}
# - Node.js: ${{ matrix.node-version }}
# - MongoDB: 4.4
# - Redis: 6
# - Test Result: ${{ steps.test_bundle.outcome }}
# - Commit: ${{ github.event.pull_request.head.sha }}

# - name: Reply PR Success
# if: ${{ success() && github.event.pull_request.draft == false }}
# uses: peter-evans/create-or-update-comment@v2
# with:
# issue-number: ${{ github.event.pull_request.number }}
# body: |
# Hi, ${{ github.event.pull_request.user.login }}. Test **passed** on ${{ matrix.os }} with Node.js ${{ matrix.node-version }}.

# - OS: ${{ matrix.os }}
# - Node.js: ${{ matrix.node-version }}
# - MongoDB: 4.4
# - Redis: 6
# - Test Result: ${{ steps.test_bundle.outcome }}
# - Commit: ${{ github.event.pull_request.head.sha }}

# You can request a review from @mogland/developers. **Thanks for your contribution!**
- name: Add ci-passed label
if: ${{ success() && github.event.pull_request.draft == false }}
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['ci-passed']
})
- name: Remove ci-failed label
if: ${{ success() && github.event.pull_request.draft == false }}
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const labels = await github.rest.issues.listLabelsOnIssue({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo
})
const label = labels.data.find(label => label.name === 'ci-failed')
if (label) {
github.rest.issues.removeLabel({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
name: label.name
})
}
- name: Add ci-failed label
if: ${{ failure() && github.event.pull_request.draft == false }}
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const labels = await github.rest.issues.listLabelsOnIssue({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo
})
const label = labels.data.find(label => label.name === 'ci-failed')
if (!label) {
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['ci-failed']
})
}
- name: Remove ci-passed label
if: ${{ failure() && github.event.pull_request.draft == false }}
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const labels = await github.rest.issues.listLabelsOnIssue({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo
})
const hasPassedLabel = labels.data.some(label => label.name === 'ci-passed')
if (hasPassedLabel) {
github.rest.issues.removeLabel({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
name: 'ci-passed'
})
}
76 changes: 76 additions & 0 deletions scripts/workflow/test-request.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash

url_array=("friends" "page" "user" "store" "configs" "themes" "comments")
special_url="http://127.0.0.1:2330/api/ping"
error_items=()
has_errors=false
MAX_RETRIES=3
base_url="http://127.0.0.1:2330/api"
PARAM=""

while [[ $# -gt 0 ]]; do
case "$1" in
"pid="*)
PID_ARG="${1#*=}"
echo "PID_ARG: $PID_ARG"
shift
;;
"dev")
PARAM="dev"
base_url="http://127.0.0.1:2330"
special_url="http://127.0.0.1:2330/ping"
echo "Testing dev server"
shift
;;
*)
echo "Unknown argument: $1"
exit 1
;;
esac
done

do_request() {
local url="$1"
local retries=$MAX_RETRIES
echo "\n--- Checking $url ---"

while [[ $retries -gt 0 ]]; do
curl -f -m 10 "$url" -H "user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36"

if [[ $? -eq 0 ]]; then
return
fi

((retries--))
sleep 5
done

error_items+=("$url")
has_errors=true
}

# Check special URL
do_request "$special_url"

# Check other URLs
for item in "${url_array[@]}"; do
item_url="$base_url/$item/ping"
do_request "$item_url"
done

if [[ -n "$PID_ARG" ]]; then
kill "$PID_ARG"
else
pm2 kill
fi

if $has_errors; then
echo "Ping errors for the following items:"
for error_item in "${error_items[@]}"; do
echo "$error_item"
done

exit 1
else
echo "All pings successful."
fi
53 changes: 7 additions & 46 deletions scripts/workflow/test-server.sh
Original file line number Diff line number Diff line change
@@ -1,63 +1,24 @@
#!/bin/bash

MAX_RETRIES=10

node -v

if [[ $? -ne 0 ]]; then
echo "failed to run node"
exit 1
fi

# 检查 out/core文件夹和里面的文件是否存在
if [[ ! -d out/core ]]; then
echo "ERROR: out/core folder not found"
exit 1
fi

cd out/core
touch env.yaml
nohup node index.js 1>/dev/null &
npm i -g pm2

p=$!
echo "started server with pid $p"
pm2 -v

if [[ $? -ne 0 ]]; then
echo "failed to run node index.js"
echo "failed to run pm2"
exit 1
fi

RETRY=0

do_request() {
curl -f -m 10 localhost:2330/api/ping -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36'

}
pm2 start ecosystem.bundle.config.js

do_request

while [[ $? -ne 0 ]] && [[ $RETRY -lt $MAX_RETRIES ]]; do
sleep 5
((RETRY++))
echo -e "RETRY: ${RETRY}\n"
do_request
done
request_exit_code=$?

echo -e "\nrequest code: ${request_exit_code}\n"

if [[ $RETRY -gt $MAX_RETRIES ]]; then
echo -n "Unable to run, aborted"
kill -9 $p
exit 1

elif [[ $request_exit_code -ne 0 ]]; then
echo -n "Request error"
kill -9 $p
exit 1
sleep 60

else
echo -e "\nSuccessfully acquire homepage, passing"
kill -9 $p
exit 0
fi
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
bash $DIR/test-request.sh
Loading