Skip to content

Commit

Permalink
feat: Adding support for new workflow_job event. (#1019)
Browse files Browse the repository at this point in the history
Added support for new workflow_job event, the check_run event will remain for backwards compatibility
  • Loading branch information
npalm committed Aug 6, 2021
1 parent fab09aa commit a74e10b
Show file tree
Hide file tree
Showing 22 changed files with 2,113 additions and 2,039 deletions.
210 changes: 120 additions & 90 deletions README.md

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions examples/default/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ resource "random_password" "random" {
length = 28
}

module "runners" {
source = "../../"

aws_region = local.aws_region
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets
################################################################################
### Hybrid acccount
################################################################################

module "runners" {
source = "../../"
create_service_linked_role_spot = true
aws_region = local.aws_region
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets

environment = local.environment
tags = {
Expand Down
1 change: 1 addition & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ module "webhook" {
lambda_zip = var.webhook_lambda_zip
lambda_timeout = var.webhook_lambda_timeout
logging_retention_in_days = var.logging_retention_in_days
runner_extra_labels = var.runner_extra_labels

role_path = var.role_path
role_permissions_boundary = var.role_permissions_boundary
Expand Down
831 changes: 308 additions & 523 deletions modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/yarn.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions modules/runners/lambdas/runners/src/local-down.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { scaleDown } from './scale-runners/scale-down';


export function run(): void {
scaleDown();
scaleDown();
}

run();
56 changes: 31 additions & 25 deletions modules/runners/lambdas/runners/src/local.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
import { scaleUp } from './scale-runners/scale-up';
import { scaleUp, ActionRequestMessage } from './scale-runners/scale-up';

const sqsEvent = {
Records: [
{
messageId: 'e8d74d08-644e-42ca-bf82-a67daa6c4dad',
// eslint-disable-next-line max-len
receiptHandle: 'AQEBCpLYzDEKq4aKSJyFQCkJduSKZef8SJVOperbYyNhXqqnpFG5k74WygVAJ4O0+9nybRyeOFThvITOaS21/jeHiI5fgaM9YKuI0oGYeWCIzPQsluW5CMDmtvqv1aA8sXQ5n2x0L9MJkzgdIHTC3YWBFLQ2AxSveOyIHwW+cHLIFCAcZlOaaf0YtaLfGHGkAC4IfycmaijV8NSlzYgDuxrC9sIsWJ0bSvk5iT4ru/R4+0cjm7qZtGlc04k9xk5Fu6A+wRxMaIyiFRY+Ya19ykcevQldidmEjEWvN6CRToLgclk=',
// eslint-disable-next-line max-len
body: { "id": 19072, "repositoryName": "ErrBud", "repositoryOwner": "ActionsTest", "eventType": "check_run", "installationId": 5 },
attributes: {
ApproximateReceiveCount: '1',
SentTimestamp: '1626450047230',
SequenceNumber: '18863115285800432640',
MessageGroupId: '19072',
SenderId: 'AROA5KW7SQ6TTB3PW6WPH:cicddev-webhook',
MessageDeduplicationId: '0c458eeb87b7f6d2607301268fd3bf33dd898a49ebd888754ff7db510c4bff1e',
ApproximateFirstReceiveTimestamp: '1626450077251'
},
messageAttributes: {},
md5OfBody: '4aef3bd70526e152e86426a0938cbec6',
eventSource: 'aws:sqs',
eventSourceARN: 'arn:aws:sqs:us-west-2:916370655143:cicddev-queued-builds.fifo',
awsRegion: 'us-west-2'
}
]
Records: [
{
messageId: 'e8d74d08-644e-42ca-bf82-a67daa6c4dad',
receiptHandle:
// eslint-disable-next-line max-len
'AQEBCpLYzDEKq4aKSJyFQCkJduSKZef8SJVOperbYyNhXqqnpFG5k74WygVAJ4O0+9nybRyeOFThvITOaS21/jeHiI5fgaM9YKuI0oGYeWCIzPQsluW5CMDmtvqv1aA8sXQ5n2x0L9MJkzgdIHTC3YWBFLQ2AxSveOyIHwW+cHLIFCAcZlOaaf0YtaLfGHGkAC4IfycmaijV8NSlzYgDuxrC9sIsWJ0bSvk5iT4ru/R4+0cjm7qZtGlc04k9xk5Fu6A+wRxMaIyiFRY+Ya19ykcevQldidmEjEWvN6CRToLgclk=',
body: {
id: 19072,
repositoryName: 'ErrBud',
repositoryOwner: 'ActionsTest',
eventType: 'check_run',
installationId: 5,
},
attributes: {
ApproximateReceiveCount: '1',
SentTimestamp: '1626450047230',
SequenceNumber: '18863115285800432640',
MessageGroupId: '19072',
SenderId: 'AROA5KW7SQ6TTB3PW6WPH:cicddev-webhook',
MessageDeduplicationId: '0c458eeb87b7f6d2607301268fd3bf33dd898a49ebd888754ff7db510c4bff1e',
ApproximateFirstReceiveTimestamp: '1626450077251',
},
messageAttributes: {},
md5OfBody: '4aef3bd70526e152e86426a0938cbec6',
eventSource: 'aws:sqs',
eventSourceARN: 'arn:aws:sqs:us-west-2:916370655143:cicddev-queued-builds.fifo',
awsRegion: 'us-west-2',
},
],
};
export function run(): void {
scaleUp(sqsEvent.Records[0].eventSource, sqsEvent.Records[0].body);
scaleUp(sqsEvent.Records[0].eventSource, sqsEvent.Records[0].body as ActionRequestMessage);
}

run();
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createOctoClient, createGithubAuth } from './gh-auth';
import nock from 'nock';
import { createAppAuth } from '@octokit/auth-app';

import { StrategyOptions } from '@octokit/auth-app/dist-types/types';
import { getParameterValue } from './ssm';

import { RequestInterface } from '@octokit/types';
import { mock, MockProxy } from 'jest-mock-extended';
import { request } from '@octokit/request';
Expand All @@ -23,7 +25,6 @@ const PARAMETER_GITHUB_APP_CLIENT_SECRET_NAME = `/actions-runner/${ENVIRONMENT}/

const mockedGet = mocked(getParameterValue);


beforeEach(() => {
jest.resetModules();
jest.clearAllMocks();
Expand Down Expand Up @@ -93,8 +94,7 @@ describe('Test createGithubAuth', () => {

const mockedAuth = jest.fn();
mockedAuth.mockResolvedValue({ token });
// eslint-disable-next-line @typescript-eslint/no-unused-vars
mockedCreatAppAuth.mockImplementation((authOptions: StrategyOptions) => {
mockedCreatAppAuth.mockImplementation(() => {
return mockedAuth;
});

Expand Down Expand Up @@ -184,8 +184,7 @@ describe('Test createGithubAuth', () => {
.mockResolvedValueOnce(GITHUB_APP_CLIENT_SECRET);
const mockedAuth = jest.fn();
mockedAuth.mockResolvedValue({ token });
// eslint-disable-next-line @typescript-eslint/no-unused-vars
mockedCreatAppAuth.mockImplementation((authOptions: StrategyOptions) => {
mockedCreatAppAuth.mockImplementation(() => {
return mockedAuth;
});

Expand Down
5 changes: 2 additions & 3 deletions modules/runners/lambdas/runners/src/scale-runners/gh-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ export async function createGithubAuth(
authType: 'app' | 'installation',
ghesApiUrl = '',
): Promise<AppAuthentication> {

let authOptions: StrategyOptions = {
appId: parseInt(await getParameterValue(process.env.PARAMETER_GITHUB_APP_ID_NAME)),
privateKey: Buffer.from(
await getParameterValue(process.env.PARAMETER_GITHUB_APP_KEY_BASE64_NAME),
'base64')
.toString(),
'base64',
).toString(),
clientId: await getParameterValue(process.env.PARAMETER_GITHUB_APP_CLIENT_ID_NAME),
clientSecret: await getParameterValue(process.env.PARAMETER_GITHUB_APP_CLIENT_SECRET_NAME),
};
Expand Down
26 changes: 13 additions & 13 deletions modules/runners/lambdas/runners/src/scale-runners/modules.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
declare namespace NodeJS {
export interface ProcessEnv {
ENVIRONMENT: string
SUBNET_IDS: string
GHES_URL: string
SCALE_DOWN_CONFIG: string
MINIMUM_RUNNING_TIME_IN_MINUTES: string
LAUNCH_TEMPLATE_NAME: string
AWS_REGION: string
PARAMETER_GITHUB_APP_CLIENT_ID_NAME: string
PARAMETER_GITHUB_APP_CLIENT_SECRET_NAME: string
PARAMETER_GITHUB_APP_ID_NAME: string
PARAMETER_GITHUB_APP_KEY_BASE64_NAME: string
}
export interface ProcessEnv {
ENVIRONMENT: string;
SUBNET_IDS: string;
GHES_URL: string;
SCALE_DOWN_CONFIG: string;
MINIMUM_RUNNING_TIME_IN_MINUTES: string;
LAUNCH_TEMPLATE_NAME: string;
AWS_REGION: string;
PARAMETER_GITHUB_APP_CLIENT_ID_NAME: string;
PARAMETER_GITHUB_APP_CLIENT_SECRET_NAME: string;
PARAMETER_GITHUB_APP_ID_NAME: string;
PARAMETER_GITHUB_APP_KEY_BASE64_NAME: string;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@ function getInstanceParams(
}

function getSubnet(): string {
const subnets = (process.env.SUBNET_IDS).split(',');
const subnets = process.env.SUBNET_IDS.split(',');
return subnets[Math.floor(Math.random() * subnets.length)];
}
42 changes: 21 additions & 21 deletions modules/runners/lambdas/runners/src/scale-runners/scale-down.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ function createGitHubClientForRunnerFactory(): (runner: RunnerInfo, orgLevel: bo
console.debug(`[createGitHubClientForRunner] Cache miss for ${key}`);
const installationId = orgLevel
? (
await githubClient.apps.getOrgInstallation({
org: repo.repoOwner,
})
).data.id
await githubClient.apps.getOrgInstallation({
org: repo.repoOwner,
})
).data.id
: (
await githubClient.apps.getRepoInstallation({
owner: repo.repoOwner,
repo: repo.repoName,
})
).data.id;
await githubClient.apps.getRepoInstallation({
owner: repo.repoOwner,
repo: repo.repoName,
})
).data.id;
const ghAuth2 = await createGithubAuth(installationId, 'installation', ghesApiUrl);
const octokit = await createOctoClient(ghAuth2.token, ghesApiUrl);
cache.set(key, octokit);
Expand Down Expand Up @@ -82,12 +82,12 @@ function listGithubRunnersFactory(): (
console.debug(`[listGithubRunners] Cache miss for ${key}`);
const runners = enableOrgLevel
? await client.paginate(client.actions.listSelfHostedRunnersForOrg, {
org: repo.repoOwner,
})
org: repo.repoOwner,
})
: await client.paginate(client.actions.listSelfHostedRunnersForRepo, {
owner: repo.repoOwner,
repo: repo.repoName,
});
owner: repo.repoOwner,
repo: repo.repoName,
});
cache.set(key, runners);

return runners;
Expand All @@ -110,14 +110,14 @@ async function removeRunner(
try {
const result = enableOrgLevel
? await githubAppClient.actions.deleteSelfHostedRunnerFromOrg({
runner_id: ghRunnerId,
org: repo.repoOwner,
})
runner_id: ghRunnerId,
org: repo.repoOwner,
})
: await githubAppClient.actions.deleteSelfHostedRunnerFromRepo({
runner_id: ghRunnerId,
owner: repo.repoOwner,
repo: repo.repoName,
});
runner_id: ghRunnerId,
owner: repo.repoOwner,
repo: repo.repoName,
});

if (result.status == 204) {
await terminateRunner(ec2runner);
Expand Down
Loading

0 comments on commit a74e10b

Please sign in to comment.