Skip to content

Commit b067260

Browse files
committed
build: add Action to start Jenkins CI
Add schedule action which will start Jenkins CI for all Pull Requests labeled as `request-ci`.
1 parent e0ecde9 commit b067260

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

.github/workflows/scheduled.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
name: Scheduled Actions
3+
4+
on:
5+
push:
6+
schedule:
7+
- cron: "*/5 * * * *"
8+
9+
jobs:
10+
commitQueue:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@master
14+
15+
# Install dependencies
16+
- name: Install jq
17+
run: sudo apt-get install jq -y
18+
- name: Install Node.js
19+
uses: actions/setup-node@v2-beta
20+
with:
21+
node-version: '12'
22+
- name: Install node-core-utils
23+
run: npm install -g 'https://github.com/mmarchini/node-core-utils#start-ci'
24+
# run: npm install -g node-core-utils
25+
26+
- name: Set variables
27+
run: |
28+
echo "::set-env name=REPOSITORY::$(echo ${{ github.repository }} | cut -d/ -f2)"
29+
echo "::set-env name=OWNER::${{ github.repository_owner }}"
30+
31+
# Get Pull Requests
32+
- name: Get Pull Requests
33+
uses: octokit/[email protected]
34+
id: get_prs_for_ci
35+
with:
36+
query: |
37+
query prs($owner:String!, $repo:String!) {
38+
repository(owner:$owner, name:$repo) {
39+
pullRequests(labels: ["request-ci"], states: OPEN, last: 100) {
40+
nodes {
41+
number
42+
}
43+
}
44+
}
45+
}
46+
owner: ${{ env.OWNER }}
47+
repo: ${{ env.REPOSITORY }}
48+
env:
49+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50+
51+
- name: Setup node-core-utils
52+
run: |
53+
ncu-config set username ${{ secrets.JENKINS_USER }}
54+
ncu-config set token none
55+
ncu-config set jenkins_token ${{ secrets.JENKINS_TOKEN }}
56+
ncu-config set owner ${{ env.OWNER }}
57+
ncu-config set repo ${{ env.REPOSITORY }}
58+
59+
- name: Start CI
60+
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/start-ci.sh

Lines changed: 53 additions & 0 deletions
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)