-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial commit: make new github runner by docker
- Loading branch information
Zimin Byun
authored and
Zimin Byun
committed
Jan 5, 2022
0 parents
commit 3fdb630
Showing
4 changed files
with
483 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,2 @@ | ||
node_modules/ | ||
./config.json |
This file contains 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,79 @@ | ||
const { Octokit } = require("@octokit/rest"); | ||
const { exec } = require("child_process"); | ||
const { connected } = require("process"); | ||
|
||
const args = process.argv.slice(2); | ||
|
||
const runnerName = args[0]; | ||
|
||
if (runnerName == undefined) { | ||
console.log("Usage: addRunner [runnerName]"); | ||
process.exit(1); | ||
} | ||
|
||
const config = require('./config.json'); | ||
|
||
const host = config.host; | ||
const apiUrl = config.githubApiUrl; | ||
const githubPAT = config.githubPAT; | ||
const ownerName = config.ownerName; | ||
const repoName = config.repoName; | ||
|
||
const haveConfigError = [githubPAT, ownerName, repoName].some((element) => { | ||
element == undefined || element == null || typeof element != "string" | ||
}); | ||
|
||
console.log(`configError: ${haveConfigError}`); | ||
|
||
console.log(`Add runner by name ${runnerName}`); | ||
|
||
const octokit = new Octokit({ | ||
auth: githubPAT, | ||
baseUrl: apiUrl, | ||
}); | ||
|
||
async function loadRepositoryToken() { | ||
const tokenResponse = await octokit.request( | ||
"POST /repos/{owner}/{repo}/actions/runners/registration-token", | ||
{ | ||
owner: ownerName, | ||
repo: repoName, | ||
} | ||
); | ||
|
||
const newToken = tokenResponse.data.token; | ||
|
||
console.log(`Created new Repository token: ${newToken}`); | ||
|
||
return newToken; | ||
} | ||
|
||
function runDocker(runnerName, repoToken) { | ||
const script = ` | ||
docker run -d --restart always \ | ||
--name ${runnerName} \ | ||
-e REPO_URL="https://${host}/${ownerName}/${repoName}" \ | ||
-e RUNNER_NAME="docker-runner-${runnerName}" \ | ||
-e RUNNER_TOKEN=${repoToken} \ | ||
-e RUNNER_GROUP="default" \ | ||
-e RUNNER_WORKDIR="/tmp/github-runner-${repoName}" \ | ||
-v /var/run/docker.sock:/var/run/docker.sock \ | ||
-v /tmp/github-runner-${runnerName}:/tmp/github-runner-${repoName} \ | ||
myoung34/github-runner:latest | ||
`; | ||
|
||
exec(script, (error, stdout, stderr) => { | ||
console.log(stdout); | ||
console.log(stderr); | ||
if (error !== null) { | ||
console.log(`exec error: ${error}`); | ||
} | ||
}); | ||
} | ||
|
||
async function addNewRunner(runnerName) { | ||
const token = await loadRepositoryToken(); | ||
runDocker(runnerName, token); | ||
} | ||
|
||
addNewRunner(runnerName); |
Oops, something went wrong.