Skip to content

Commit

Permalink
initial commit: make new github runner by docker
Browse files Browse the repository at this point in the history
  • Loading branch information
Zimin Byun authored and Zimin Byun committed Jan 5, 2022
0 parents commit 3fdb630
Show file tree
Hide file tree
Showing 4 changed files with 483 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
./config.json
79 changes: 79 additions & 0 deletions index.js
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);
Loading

0 comments on commit 3fdb630

Please sign in to comment.