Skip to content

Add on demand [just testing] #6706

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

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,13 @@ fi

./config.sh --unattended --name $INSTANCE_ID --work "_work" $CONFIG

# Set tag as runner id for scale down later
# Set tag `GithubRunnerID` as runner id for scale down later
GH_RUNNER_ID=$(jq '.agentId' .runner)
retry aws ec2 create-tags --region $REGION --resource $INSTANCE_ID --tags "Key=GithubRunnerID,Value=$GH_RUNNER_ID"

# Remove tag `Stage`` from instance to indicate that the instance is finished the previous step with fresh start
retry aws ec2 delete-tags --region "$REGION" --resources "$INSTANCE_ID" --tags "Key=Stage"

chown -R $USER_NAME:$USER_NAME .
OVERWRITE_SERVICE_USER=${run_as_root_user}
SERVICE_USER=$${OVERWRITE_SERVICE_USER:-$USER_NAME}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { Config } from './config';
import { getRunnerTypes } from './gh-runners';
import { ScaleUpMetrics } from './metrics';
import { getRunner, RunnerInputParameters } from './runners';
import { innerCreateRunnerConfigArgument } from './scale-up';
import { tryRefreshRunner } from './scale-up-try-reuse-runner-utils';
import { getRepoKey, Repo, RunnerInfo } from './utils';

export interface ActionRequestMessage {
id: number;
instanceId: string;
awsRegion: string;
}

class RetryableRefreshError extends Error {
constructor(message: string) {
super(message);
this.name = 'RetryableRefreshError';
}
}

export async function refreshRunner(
eventSource: string,
payload: ActionRequestMessage,
metrics: ScaleUpMetrics,
): Promise<void> {
if (eventSource !== 'aws:sqs') {
throw new Error('Cannot handle non-SQS events!');
}

const { instanceId, awsRegion } = payload;
if (!instanceId || !awsRegion) {
console.warn(`[Skip] Missing required field(s):${!instanceId ? ' instanceId' : ''}${!awsRegion ? ' awsRegion' : ''}`);
return;
}

console.debug(`Refreshing runner: instanceId=${instanceId}, region=${awsRegion}`);

let runner: RunnerInfo | undefined;
try {
runner = await getRunner(metrics, instanceId, awsRegion);
if (!runner) {
console.warn(`Runner not found in aws: instanceId=${instanceId}, region=${awsRegion}`);
return;
}
} catch (e) {
console.error(`Failed to get runner: ${e}`);
return;
}

const { runnerType: runnerTypeName, repositoryOwner, repositoryName, org, repo } = runner;
if (!runnerTypeName || !repositoryOwner || !repositoryName) {
console.warn(`[Skip] Missing runner metadata: ${JSON.stringify({ runnerTypeName, repositoryOwner, repositoryName })}`);
return;
}

if (!org && !repo) {
console.warn(`Runner is missing both org and repo: instanceId=${instanceId}`);
return;
}

const isOrgRunner = !!org;
const isEphemeral = true;
const ghesUrlHost = Config.Instance.ghesUrlHost;
const repoInfo: Repo = { owner: repositoryOwner, repo: repositoryName };

console.debug(`Fetching runner type for: ${runnerTypeName}`);
const runnerTypes = await getRunnerTypes(repoInfo, metrics, awsRegion);
const runnerType = runnerTypes.get(runnerTypeName);

if (!runnerType) {
console.warn(`Runner type not found: ${runnerTypeName}`);
return;
}

const createRunnerParams: RunnerInputParameters = {
environment: Config.Instance.environment,
runnerConfig: (awsRegion: string, experimentalRunner: boolean) =>
innerCreateRunnerConfigArgument(
runnerTypeName,
repositoryName,
repositoryOwner,
awsRegion,
metrics,
ghesUrlHost,
isOrgRunner,
isEphemeral,
experimentalRunner,
runner.runnerExtraLabels,
runner.runnerTypeLabels,
runner.runnerGroupName,
),
runnerType,
repositoryOwner,
repositoryName,
...(Config.Instance.enableOrganizationRunners
? { orgName: repositoryOwner }
: { repoName: getRepoKey(repoInfo) }),
};

try {
await tryRefreshRunner(createRunnerParams, metrics, runner);
console.debug(`Refreshed runner: instanceId=${instanceId}, region=${awsRegion}`);
} catch (e) {
console.error(`Error refreshing runner: ${e}`);
}
}
Loading
Loading