-
Notifications
You must be signed in to change notification settings - Fork 0
132 lines (116 loc) · 5.98 KB
/
build.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
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
name: Check for any build error
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
# begin wyb 30min build once
check_push_time:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Get Last Two Commit Timestamps
id: last_two_commits
run: |
# 获取最新的两个commit(X)(由于github action的执行有延迟,因此最新的两个提交的间隔不见得是当前提交和上一个提交的间隔)
# COMMITS=$(curl -s -H "Authorization: Bearer ${{ secrets.ACTION_NETLIFYDIGITALGARDEN_30MIN_PER_BUILD }}" \
# "https://api.github.com/repos/${{ github.repository }}/commits?per_page=2")
# LATEST_COMMIT_TIME=$(echo "$COMMITS" | jq -r '.[0].commit.author.date' | xargs -I{} date -d {} +%s)
# PREVIOUS_COMMIT_TIME=$(echo "$COMMITS" | jq -r '.[1].commit.author.date' | xargs -I{} date -d {} +%s)
# # echo "::set-output name=latest_commit_timestamp::$LATEST_COMMIT_TIME"
# # echo "::set-output name=previous_commit_timestamp::$PREVIOUS_COMMIT_TIME"
# echo "latest_commit_timestamp=$LATEST_COMMIT_TIME" >> $GITHUB_OUTPUT
# echo "previous_commit_timestamp=$PREVIOUS_COMMIT_TIME" >> $GITHUB_OUTPUT
# 当前commit的SHA
CURRENT_SHA=$GITHUB_SHA
echo "The commit SHA is: $GITHUB_SHA"
# 获取上一个SHA(X) (github action的git命令只能获取当前提交,gitlog没有历史记录)
# PREVIOUS_SHA=$(git rev-parse "$CURRENT_SHA~1")
# PREVIOUS_SHA=$(git rev-parse "$CURRENT_SHA^")
# echo "Previous commit SHA: $PREVIOUS_SHA"
# 获取上一个SHA(X)(github action的git命令只能获取当前提交,gitlog没有历史记录)
# 获取至少20个 commit 的历史
# COMMIT_HISTORY=$(git log --pretty=format:'%H %ai' --reverse -n 20)
# COMMIT_HISTORY=$(git log --pretty=format:'%H' --reverse -n 5)
# 找到当前 commit 的索引
# COMMIT_INDEX=$(echo "$COMMIT_HISTORY" | grep " $CURRENT_SHA " | cut -d' ' -f1)
# COMMIT_INDEX=$(echo "$COMMIT_HISTORY" | grep "$CURRENT_SHA" | cut -d: -f1)
# echo "commit index: $COMMIT_INDEX"
# 如果找到当前 commit,获取前一个 commit 的 SHA
# if [ "$COMMIT_INDEX" != "" ]; then
# PREVIOUS_COMMIT_INDEX=$(($COMMIT_INDEX - 1))
# PREVIOUS_SHA=$(echo "$COMMIT_HISTORY" | cut -d' ' -f$PREVIOUS_COMMIT_INDEX)
# echo "Previous commit SHA: $PREVIOUS_SHA"
# else
# echo "Current commit not found in the last 20 commits."
# fi
# 最新的20个commit列表
COMMITS=$(curl -s -H "Authorization: Bearer ${{ secrets.ACTION_NETLIFYDIGITALGARDEN_30MIN_PER_BUILD }}" \
"https://api.github.com/repos/${{ github.repository }}/commits?per_page=20")
# 当前commit的时间
# 将 JSON 字符串转换为数组
COMMIT_SHAS=$(echo "$COMMITS" | jq -r '.[].sha')
# IFS=$'\n' read -rd '' -a SHAS_ARRAY <<< "$COMMIT_SHAS"
SHAS_ARRAY=($(echo "$COMMIT_SHAS" | tr '\n' ' '))
# 获取当前 commit 的索引
CURRENT_INDEX=20
for i in "${!SHAS_ARRAY[@]}"; do
echo "SHAS_ARRAY $i is ${SHAS_ARRAY[$i]}"
if [ "${SHAS_ARRAY[$i]}" == "$CURRENT_SHA" ]; then
CURRENT_INDEX=$i
echo "find current index success~!!"
break
fi
done
echo "current index $CURRENT_INDEX"
# 如果当前 commit 不是末尾的,则获取前一个 commit 的 SHA
if [ $CURRENT_INDEX -lt 19 ]; then
PREVIOUS_INDEX=${CURRENT_INDEX+1}
echo "previous index $PREVIOUS_INDEX"
# 当前commit的提交时间
LATEST_COMMIT_TIME=$(echo "$COMMITS" | jq -r --argjson CURRENT_INDEX "$CURRENT_INDEX" '.[$CURRENT_INDEX].commit.author.date' | xargs -I{} date -d {} +%s)
echo "latest_commit_time $LATEST_COMMIT_TIME"
# 获取前一个 commit 的提交时间
PREVIOUS_COMMIT_TIME=$(echo "$COMMITS" | jq -r --argjson PREVIOUS_INDEX "$PREVIOUS_INDEX" '.[$PREVIOUS_INDEX].commit.author.date' | xargs -I{} date -d {} +%s)
echo "previous_commit_time $PREVIOUS_COMMIT_TIME"
# 输出下一步
echo "latest_commit_timestamp=$LATEST_COMMIT_TIME" >> $GITHUB_OUTPUT
echo "previous_commit_timestamp=$PREVIOUS_COMMIT_TIME" >> $GITHUB_OUTPUT
fi
- name: Check Push Time
run: |
LATEST_COMMIT_TIME=${{ steps.last_two_commits.outputs.latest_commit_timestamp }}
PREVIOUS_COMMIT_TIME=${{ steps.last_two_commits.outputs.previous_commit_timestamp }}
ELAPSED_TIME=$((LATEST_COMMIT_TIME - PREVIOUS_COMMIT_TIME))
echo "Elapsed time since last commit: $ELAPSED_TIME seconds"
if [ $ELAPSED_TIME -gt 1800 ]; then
echo "Time elapsed is greater than 30 minutes. Not performing action... Start check..."
# curl -X POST -d '{}' https://api.netlify.com/build_hooks/65a77015df78f3742a8265b4
else
echo "Time elapsed is within 30 minutes. Skipping action."
exit 1
fi
# end wyb 30min build once
build:
runs-on: ubuntu-22.04
# begin wyb 30min build once
needs: check_push_time
if: success()
# end wyb 30min build once
strategy:
matrix:
node-version: [20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm install
- run: npm run build --if-present