-
-
Notifications
You must be signed in to change notification settings - Fork 32k
build: auto start Jenkins CI via PR labels #34089
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
name: Auto Start CI | ||
|
||
on: | ||
push: | ||
schedule: | ||
# `schedule` event is used instead of `pull_request` because when a | ||
# `pull_requesst` event is triggered on a PR from a fork, GITHUB_TOKEN will | ||
# be read-only, and the Action won't have access to any other repository | ||
# secrets, which it needs to access Jenkins API. Runs every five minutes | ||
# (fastest the scheduler can run). Five minutes is optimistic, it can take | ||
# longer to run. | ||
- cron: "*/5 * * * *" | ||
|
||
jobs: | ||
commitQueue: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@master | ||
|
||
# Install dependencies | ||
- name: Install jq | ||
run: sudo apt-get install jq -y | ||
- name: Install Node.js | ||
uses: actions/setup-node@v2-beta | ||
with: | ||
node-version: '12' | ||
- name: Install node-core-utils | ||
run: npm install -g node-core-utils | ||
|
||
- name: Set variables | ||
run: | | ||
echo "::set-env name=REPOSITORY::$(echo ${{ github.repository }} | cut -d/ -f2)" | ||
echo "::set-env name=OWNER::${{ github.repository_owner }}" | ||
|
||
# Get Pull Requests | ||
- name: Get Pull Requests | ||
uses: octokit/[email protected] | ||
id: get_prs_for_ci | ||
with: | ||
query: | | ||
query prs($owner:String!, $repo:String!) { | ||
repository(owner:$owner, name:$repo) { | ||
pullRequests(labels: ["request-ci"], states: OPEN, last: 100) { | ||
nodes { | ||
number | ||
} | ||
} | ||
} | ||
} | ||
owner: ${{ env.OWNER }} | ||
repo: ${{ env.REPOSITORY }} | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Setup node-core-utils | ||
run: | | ||
ncu-config set username ${{ secrets.JENKINS_USER }} | ||
ncu-config set token none | ||
ncu-config set jenkins_token ${{ secrets.JENKINS_TOKEN }} | ||
ncu-config set owner ${{ env.OWNER }} | ||
ncu-config set repo ${{ env.REPOSITORY }} | ||
Comment on lines
+58
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably should provide a way to set ncu configs via environment variables, so we don't have to worry about writing to disk here. |
||
|
||
- name: Start CI | ||
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) | .[]') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/bin/bash | ||
|
||
set -xe | ||
|
||
GITHUB_TOKEN=$1 | ||
OWNER=$2 | ||
REPOSITORY=$3 | ||
API_URL=https://api.github.com | ||
REQUEST_CI_LABEL='request-ci' | ||
REQUEST_CI_FAILED_LABEL='request-ci-failed' | ||
shift 3 | ||
|
||
function issueUrl() { | ||
echo "$API_URL/repos/${OWNER}/${REPOSITORY}/issues/${1}" | ||
} | ||
|
||
function labelsUrl() { | ||
echo "$(issueUrl "${1}")/labels" | ||
} | ||
|
||
function commentsUrl() { | ||
echo "$(issueUrl "${1}")/comments" | ||
} | ||
|
||
for pr in "$@"; do | ||
curl -sL --request DELETE \ | ||
--url "$(labelsUrl "$pr")"/"$REQUEST_CI_LABEL" \ | ||
--header "authorization: Bearer ${GITHUB_TOKEN}" \ | ||
--header 'content-type: application/json' | ||
|
||
ci_started=yes | ||
rm -f output; | ||
ncu-ci run "$pr" >output 2>&1 || ci_started=no | ||
|
||
if [ "$ci_started" == "no" ]; then | ||
# Do we need to reset? | ||
curl -sL --request PUT \ | ||
--url "$(labelsUrl "$pr")" \ | ||
--header "authorization: Bearer ${GITHUB_TOKEN}" \ | ||
--header 'content-type: application/json' \ | ||
--data '{"labels": ["'"${REQUEST_CI_FAILED_LABEL}"'"]}' | ||
|
||
jq -n --arg content "<details><summary>Couldn't start CI</summary><pre>$(cat output)</pre></details>" '{body: $content}' > output.json | ||
|
||
curl -sL --request POST \ | ||
--url "$(commentsUrl "$pr")" \ | ||
--header "authorization: Bearer ${GITHUB_TOKEN}" \ | ||
--header 'content-type: application/json' \ | ||
--data @output.json | ||
|
||
rm output.json; | ||
fi | ||
done; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're worried about the number of concurrent Jenkins jobs running, we could reduce this to 5 and increase the scheduler to 15-30 minutes.