Skip to content

Commit

Permalink
runner get runner token using github token
Browse files Browse the repository at this point in the history
  • Loading branch information
xprotocol-bot committed Apr 14, 2022
1 parent 9505c35 commit 6de531d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
26 changes: 21 additions & 5 deletions src/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const config = require('./config');

// User data scripts are run as the root user
/* eslint-disable no-useless-escape */
function buildUserDataScript(githubRegistrationToken) {
function buildUserDataScript(githubToken) {
if (config.input.runnerHomeDir) {
// If runner home directory is specified, we expect the actions-runner software (and dependencies)
// to be pre-installed in the AMI, so we simply cd into that directory and then start the runner
Expand All @@ -29,15 +29,23 @@ Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash
cd "${config.input.runnerHomeDir}"
if [ ! -f "./jq" ]; then
curl https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 -o jq
chmod +x ./jq
fi
echo 'Getting token to get metadata of EC2 instance'
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
echo 'Getting ec2 instance id'
INSTANCE_ID=$(curl -H "X-aws-ec2-metadata-token: $\{TOKEN\}" -v http://169.254.169.254/latest/meta-data/instance-id)
echo 'Got instance id $\{INSTANCE_ID\}'
RUNNER_TOKEN=$(curl -s -XPOST \
-H "authorization: token ${githubToken}" \
https://api.github.com/repos/wayofthepie/gh-app-test/actions/runners/registration-token |\
jq -r .token)
echo 'Configuring runner'
./config.sh \
--url https://github.com/${config.githubContext.owner}/${config.githubContext.repo} \
--token ${githubRegistrationToken} \
--token $\{RUNNER_TOKEN\} \
--labels $\{INSTANCE_ID\} \
--work _work \
--ephemeral
Expand Down Expand Up @@ -76,15 +84,23 @@ if [ ! -d "./actions-runner" ]; then
else
cd actions-runner
fi
if [ ! -f "./jq" ]; then
curl https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 -o jq
chmod +x ./jq
fi
echo 'Getting token to get metadata of EC2 instance'
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
echo 'Getting ec2 instance id'
INSTANCE_ID=$(curl -H "X-aws-ec2-metadata-token: $\{TOKEN\}" -v http://169.254.169.254/latest/meta-data/instance-id)
echo 'Got instance id $\{INSTANCE_ID\}'
RUNNER_TOKEN=$(curl -s -XPOST \
-H "authorization: token ${githubToken}" \
https://api.github.com/repos/wayofthepie/gh-app-test/actions/runners/registration-token |\
jq -r .token)
echo 'Configuring runner'
./config.sh \
--url https://github.com/${config.githubContext.owner}/${config.githubContext.repo} \
--token ${githubRegistrationToken} \
--token ${githubToken} \
--labels $\{INSTANCE_ID\} \
--work _work \
--ephemeral
Expand All @@ -94,10 +110,10 @@ echo 'Starting runner'
}
}

async function startEc2Instance(githubRegistrationToken) {
async function startEc2Instance(githubToken) {
const ec2 = new AWS.EC2();

const userData = buildUserDataScript(githubRegistrationToken);
const userData = buildUserDataScript(githubToken);

const runParams = {
ImageId: config.input.ec2ImageId,
Expand Down
15 changes: 0 additions & 15 deletions src/gh.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,6 @@ async function getRunner(ec2InstanceId) {
}
}

// get GitHub Registration Token for registering a self-hosted runner
async function getRegistrationToken() {
const octokit = github.getOctokit(config.input.githubToken);

try {
const response = await octokit.request('POST /repos/{owner}/{repo}/actions/runners/registration-token', config.githubContext);
core.info('GitHub Registration Token is received');
return response.data.token;
} catch (error) {
core.error('GitHub Registration Token receiving error');
throw error;
}
}

async function removeRunner() {
const runner = await getRunner(config.input.ec2InstanceId);
const octokit = github.getOctokit(config.input.githubToken);
Expand Down Expand Up @@ -86,7 +72,6 @@ async function waitForRunnerRegistered(ec2InstanceId) {
}

module.exports = {
getRegistrationToken,
removeRunner,
waitForRunnerRegistered,
};
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ function setOutput(ec2InstanceId) {
}

async function start() {
const githubRegistrationToken = await gh.getRegistrationToken();
const ec2InstanceId = await aws.startEc2Instance(githubRegistrationToken);
const ec2InstanceId = await aws.startEc2Instance(config.input.githubToken);
setOutput(ec2InstanceId);
await aws.waitForInstanceRunning(ec2InstanceId);
await gh.waitForRunnerRegistered(ec2InstanceId);
Expand Down

0 comments on commit 6de531d

Please sign in to comment.