Skip to content

Commit

Permalink
Merge pull request #16 from sonikro/allow-ecs-cluster-name-input
Browse files Browse the repository at this point in the history
feat: allow ecs-cluster-name to be an input
  • Loading branch information
sonikro authored Jul 18, 2023
2 parents e4b2c17 + 83bb969 commit 050c16b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 24 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ inputs:
required: false
default: "256"
description: Amount of vCPU to be used by the remote ECS Task (Must be a FARGATE Compatible combination. See https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html)
ecs_cluster_name:
required: false
default: "github-actions-aws-run"
description: The name of the ECS Cluster where the Tasks will run. It will be automatically created if it doesn't exist
image:
required: true
description: Name of the docker container to be used for the step execution
Expand Down
21 changes: 11 additions & 10 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async function run(): Promise<void> {
const securityGroupId: string = core.getInput('security_group_id')
const memory: string = core.getInput(`memory`)
const cpu: string = core.getInput(`cpu`)
const ecsClusterName: string = core.getInput(`ecs_cluster_name`)

core.debug(`Using ${roleArn} to authenticate to AWS`) // debug is only output if you set the secret `ACTIONS_STEP_DEBUG` to true
core.debug(`Using ${image} as the container image for running the task`)
Expand Down Expand Up @@ -57,7 +58,8 @@ async function run(): Promise<void> {
shell,
securityGroupId,
memory,
cpu
cpu,
ecsClusterName
})

if (executionResult.exitCode !== 0) {
Expand Down
29 changes: 17 additions & 12 deletions src/providers/remoteEnvironments/AWSECSRemoteEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface ECSExecutionSettings extends ExecutionSettings {
securityGroupId: string
memory: string
cpu: string
ecsClusterName: string
}

export interface ECSTaskExecutionResult extends ExecutionResult {
Expand All @@ -44,8 +45,6 @@ export type TeardownFunction = () => Promise<any>
export class AWSECSRemoteEnvironment
implements RemoteEnvironment<ECSExecutionSettings>
{
static readonly CLUSTER_NAME = 'github-actions-aws-run'

private readonly tearDownQueue: TeardownFunction[] = []

static async fromGithubOidc({
Expand Down Expand Up @@ -108,7 +107,7 @@ export class AWSECSRemoteEnvironment

// Setup Environment
console.log('Setting up required infrastructure')
const ecsCluster = await this.setupECSCluster()
const ecsCluster = await this.setupECSCluster({settings})
console.log(`Using ECS Cluster ${ecsCluster.clusterName}`)
core.debug(
`Uploading runner workspace to S3 so it can be shared with the remote execution ECS Task`
Expand Down Expand Up @@ -161,8 +160,12 @@ export class AWSECSRemoteEnvironment
}
}

protected async setupECSCluster(): Promise<ECS.Cluster> {
const ecsCluster = await this.getOrCreateCluster()
protected async setupECSCluster({
settings
}: {
settings: ECSExecutionSettings
}): Promise<ECS.Cluster> {
const ecsCluster = await this.getOrCreateCluster({settings})
return ecsCluster
}

Expand Down Expand Up @@ -190,7 +193,7 @@ export class AWSECSRemoteEnvironment

const awsLogsParameters = {
'awslogs-create-group': 'true',
'awslogs-group': AWSECSRemoteEnvironment.CLUSTER_NAME,
'awslogs-group': settings.ecsClusterName,
'awslogs-region': ecs.config.region!,
'awslogs-stream-prefix': 'aws-run-logs'
}
Expand Down Expand Up @@ -321,9 +324,7 @@ export class AWSECSRemoteEnvironment
}): Promise<ECS.Task> {
const {cloudwatchLogs, ecs} = this.dependencies

const taskId = taskArn.split(
`:task/${AWSECSRemoteEnvironment.CLUSTER_NAME}/`
)[1]
const taskId = taskArn.split(`:task/${settings.ecsClusterName}/`)[1]

const POLLING_INTERVAL = 2000

Expand Down Expand Up @@ -476,12 +477,16 @@ export class AWSECSRemoteEnvironment
await s3.deleteBucket({Bucket: bucketName}).promise()
}

protected async getOrCreateCluster(): Promise<ECS.Cluster> {
protected async getOrCreateCluster({
settings
}: {
settings: ECSExecutionSettings
}): Promise<ECS.Cluster> {
const {ecs} = this.dependencies

const existingClusterResponse = await ecs
.describeClusters({
clusters: [AWSECSRemoteEnvironment.CLUSTER_NAME]
clusters: [settings.ecsClusterName]
})
.promise()

Expand All @@ -492,7 +497,7 @@ export class AWSECSRemoteEnvironment
const newClusterResponse = await ecs
.createCluster({
capacityProviders: ['FARGATE'],
clusterName: AWSECSRemoteEnvironment.CLUSTER_NAME,
clusterName: settings.ecsClusterName,
tags: [{key: 'managedBy', value: 'aws-run'}]
})
.promise()
Expand Down

0 comments on commit 050c16b

Please sign in to comment.