Skip to content

Commit 70cf3cb

Browse files
mmarchinicodebytere
authored andcommitted
build: auto start Jenkins CI via PR labels
Add an Action that will find every PR with the `request-ci` label and will start a Jenkins CI for each of these Pull Requests. The scheduler event is used to circumvent GitHub Actions limitations on Pull Requests from forks (where secrets are not accessible and the GITHUB_TOKEN is read-only). If the Action fails to start a CI, it will add a `request-ci-failed` label and will leave a comment with the error message from NCU. Fixes: nodejs/github-bot#234 PR-URL: #34089 Reviewed-By: Christian Clauss <[email protected]>
1 parent 2703fe4 commit 70cf3cb

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

.github/workflows/auto-start-ci.yml

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
name: Auto Start CI
3+
4+
on:
5+
push:
6+
schedule:
7+
# `schedule` event is used instead of `pull_request` because when a
8+
# `pull_requesst` event is triggered on a PR from a fork, GITHUB_TOKEN will
9+
# be read-only, and the Action won't have access to any other repository
10+
# secrets, which it needs to access Jenkins API. Runs every five minutes
11+
# (fastest the scheduler can run). Five minutes is optimistic, it can take
12+
# longer to run.
13+
- cron: "*/5 * * * *"
14+
15+
jobs:
16+
commitQueue:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@master
20+
21+
# Install dependencies
22+
- name: Install jq
23+
run: sudo apt-get install jq -y
24+
- name: Install Node.js
25+
uses: actions/setup-node@v2-beta
26+
with:
27+
node-version: '12'
28+
- name: Install node-core-utils
29+
run: npm install -g node-core-utils
30+
31+
- name: Set variables
32+
run: |
33+
echo "::set-env name=REPOSITORY::$(echo ${{ github.repository }} | cut -d/ -f2)"
34+
echo "::set-env name=OWNER::${{ github.repository_owner }}"
35+
36+
# Get Pull Requests
37+
- name: Get Pull Requests
38+
uses: octokit/[email protected]
39+
id: get_prs_for_ci
40+
with:
41+
query: |
42+
query prs($owner:String!, $repo:String!) {
43+
repository(owner:$owner, name:$repo) {
44+
pullRequests(labels: ["request-ci"], states: OPEN, last: 100) {
45+
nodes {
46+
number
47+
}
48+
}
49+
}
50+
}
51+
owner: ${{ env.OWNER }}
52+
repo: ${{ env.REPOSITORY }}
53+
env:
54+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
55+
56+
- name: Setup node-core-utils
57+
run: |
58+
ncu-config set username ${{ secrets.JENKINS_USER }}
59+
ncu-config set token none
60+
ncu-config set jenkins_token ${{ secrets.JENKINS_TOKEN }}
61+
ncu-config set owner ${{ env.OWNER }}
62+
ncu-config set repo ${{ env.REPOSITORY }}
63+
64+
- name: Start CI
65+
run: ./tools/start-ci.sh ${{ secrets.GITHUB_TOKEN }} ${{ env.OWNER }} ${{ env.REPOSITORY }} $(echo '${{ steps.get_prs_for_ci.outputs.data }}' | jq '.repository.pullRequests.nodes | map(.number) | .[]')

tools/actions/start-ci.sh

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
3+
set -xe
4+
5+
GITHUB_TOKEN=$1
6+
OWNER=$2
7+
REPOSITORY=$3
8+
API_URL=https://api.github.com
9+
REQUEST_CI_LABEL='request-ci'
10+
REQUEST_CI_FAILED_LABEL='request-ci-failed'
11+
shift 3
12+
13+
function issueUrl() {
14+
echo "$API_URL/repos/${OWNER}/${REPOSITORY}/issues/${1}"
15+
}
16+
17+
function labelsUrl() {
18+
echo "$(issueUrl "${1}")/labels"
19+
}
20+
21+
function commentsUrl() {
22+
echo "$(issueUrl "${1}")/comments"
23+
}
24+
25+
for pr in "$@"; do
26+
curl -sL --request DELETE \
27+
--url "$(labelsUrl "$pr")"/"$REQUEST_CI_LABEL" \
28+
--header "authorization: Bearer ${GITHUB_TOKEN}" \
29+
--header 'content-type: application/json'
30+
31+
ci_started=yes
32+
rm -f output;
33+
ncu-ci run "$pr" >output 2>&1 || ci_started=no
34+
35+
if [ "$ci_started" == "no" ]; then
36+
# Do we need to reset?
37+
curl -sL --request PUT \
38+
--url "$(labelsUrl "$pr")" \
39+
--header "authorization: Bearer ${GITHUB_TOKEN}" \
40+
--header 'content-type: application/json' \
41+
--data '{"labels": ["'"${REQUEST_CI_FAILED_LABEL}"'"]}'
42+
43+
jq -n --arg content "<details><summary>Couldn't start CI</summary><pre>$(cat output)</pre></details>" '{body: $content}' > output.json
44+
45+
curl -sL --request POST \
46+
--url "$(commentsUrl "$pr")" \
47+
--header "authorization: Bearer ${GITHUB_TOKEN}" \
48+
--header 'content-type: application/json' \
49+
--data @output.json
50+
51+
rm output.json;
52+
fi
53+
done;

0 commit comments

Comments
 (0)