1717 pull_request :
1818 branches : [main]
1919 types : [opened, synchronize, reopened]
20+ pull_request_review :
21+ types : [submitted]
2022 paths-ignore :
2123 - ' cmake/**'
2224 - ' docs/**'
@@ -34,12 +36,56 @@ concurrency:
3436 cancel-in-progress : ${{ startsWith(github.ref, 'refs/pull/') }}
3537
3638jobs :
37- check-approval-if-needed :
38- if : ${{ github.event_name == 'pull_request' }}
39+ determine-target :
3940 runs-on : [self-hosted]
4041 outputs :
41- requires_approval : ${{ steps.check_sensitive.outputs.requires_approval }}
42- approved : ${{ steps.check_approved.outputs.approved }}
42+ should_run : ${{ steps.decide.outputs.should_run }}
43+ pr_number : ${{ steps.set_vars.outputs.pr_number }}
44+ head_sha : ${{ steps.set_vars.outputs.head_sha }}
45+ steps :
46+ - name : Set variables based on event
47+ id : set_vars
48+ run : |
49+ if [ "${{ github.event_name }}" == "pull_request_review" ]; then
50+ echo "pr_number=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
51+ echo "head_sha=${{ github.event.pull_request.head.sha }}" >> $GITHUB_OUTPUT
52+ elif [ "${{ github.event_name }}" == "pull_request" ]; then
53+ echo "pr_number=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
54+ echo "head_sha=${{ github.sha }}" >> $GITHUB_OUTPUT
55+ else
56+ echo "pr_number=" >> $GITHUB_OUTPUT
57+ echo "head_sha=${{ github.sha }}" >> $GITHUB_OUTPUT
58+ fi
59+
60+ - name : Decide whether to run build
61+ id : decide
62+ run : |
63+ event="${{ github.event_name }}"
64+ if [[ "$event" == "workflow_dispatch" || "$event" == "push" ]]; then
65+ echo "should_run=true" >> $GITHUB_OUTPUT
66+ elif [[ "$event" == "pull_request" ]]; then
67+ echo "should_run=true" >> $GITHUB_OUTPUT
68+ elif [[ "$event" == "pull_request_review" ]]; then
69+ if [[ "${{ github.event.review.state }}" == "approved" ]]; then
70+ echo "should_run=true" >> $GITHUB_OUTPUT
71+ else
72+ echo "should_run=false" >> $GITHUB_OUTPUT
73+ fi
74+ else
75+ echo "should_run=false" >> $GITHUB_OUTPUT
76+ fi
77+
78+ check-sensitive-and-approval :
79+ # if: ${{ github.event_name == 'pull_request' }}
80+ needs : determine-target
81+ if : >
82+ needs.determine-target.outputs.should_run == 'true' &&
83+ (github.event_name == 'pull_request' || github.event_name == 'pull_request_review')
84+ runs-on : [self-hosted]
85+ outputs :
86+ # requires_approval: ${{ steps.check_sensitive.outputs.requires_approval }}
87+ # approved: ${{ steps.check_approved.outputs.approved }}
88+ can_proceed : ${{ steps.check.outputs.can_proceed }}
4389 steps :
4490 - name : Checkout Code
4591 uses : actions/checkout@v4
@@ -85,29 +131,47 @@ jobs:
85131
86132 - name : Check PR approvals
87133 id : check_approved
88- if : ${{ steps.check_sensitive.outputs.requires_approval == 'true' }}
89- echo "=========================> Check PR approvals - true"
90- env :
91- GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
92- run : |
93- pr_number=${{ github.event.pull_request.number }}
94- response=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
95- -H "Accept: application/vnd.github.v3+json" \
96- "https://api.github.com/repos/${{ github.repository }}/pulls/$pr_number/reviews")
134+ if : ${{ github.event_name == 'pull_request' && steps.check_sensitive.outputs.requires_approval == 'true' }}
135+ env :
136+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
137+ run : |
138+ pr_number=${{ github.event.pull_request.number }}
139+ response=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
140+ -H "Accept: application/vnd.github.v3+json" \
141+ "https://api.github.com/repos/${{ github.repository }}/pulls/$pr_number/reviews")
97142
98- if echo "$response" | jq -e '.[] | select(.state == "APPROVED")' > /dev/null; then
99- echo "approved=true" >> $GITHUB_OUTPUT
143+ if echo "$response" | jq -e '.[] | select(.state == "APPROVED")' > /dev/null; then
144+ echo "approved=true" >> $GITHUB_OUTPUT
145+ else
146+ echo "approved=false" >> $GITHUB_OUTPUT
147+ fi
148+
149+ - name : Final decision
150+ id : check
151+ run : |
152+ if [ "${{ github.event_name }}" == "pull_request_review" ]; then
153+ echo "can_proceed=true" >> $GITHUB_OUTPUT
154+ #if [ "${{ steps.check_sensitive.outputs.requires_approval }}" == "true" ]; then
155+ # echo "can_proceed=true" >> $GITHUB_OUTPUT
156+ #else
157+ # echo "can_proceed=true" >> $GITHUB_OUTPUT
158+ #fi
159+ else
160+ if [ "${{ steps.check_sensitive.outputs.requires_approval }}" != "true" ]; then
161+ echo "can_proceed=true" >> $GITHUB_OUTPUT
162+ elif [ "${{ steps.check_approved.outputs.approved }}" == "true" ]; then
163+ echo "can_proceed=true" >> $GITHUB_OUTPUT
100164 else
101- echo "approved =false" >> $GITHUB_OUTPUT
165+ echo "can_proceed =false" >> $GITHUB_OUTPUT
102166 fi
103- else
104- echo "=========================> Check PR approvals - no need"
105- echo "approved=true" >> $GITHUB_OUTPUT
106- fi
167+ fi
107168
108169 build :
109- needs : check-approval-if-needed
110- if : ${{ github.event_name == 'workflow_dispatch' || github.event_name == 'push' || github.event_name == 'pull_request' }}
170+ needs : [determine-target, check-sensitive-and-approval]
171+ # if: ${{ github.event_name == 'workflow_dispatch' || github.event_name == 'push' || github.event_name == 'pull_request' }}
172+ if : >
173+ (github.event_name == 'workflow_dispatch' || github.event_name == 'push') ||
174+ (needs.check-sensitive-and-approval.outputs.can_proceed == 'true')
111175 runs-on : [self-hosted]
112176 steps :
113177 - name : Checkout Code
0 commit comments