diff --git a/README.md b/README.md index e162085bf..53681726f 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Registers an Amazon ECS task definition and deploys it to an ECS service. See [action.yml](action.yml) for the full documentation for this action's inputs and outputs. In most cases when running a one-off task, subnet ID's, subnet groups, and assign public IP will be required. +Assign public IP will only be applied when a subnet or security group is defined. ### Task definition file @@ -269,7 +270,8 @@ In the following example, the service would not be updated until the ad-hoc task wait-for-task-stopped: true ``` -Overrides and VPC networking options are available as well. See [action.yml](action.yml) for more details. +Overrides and VPC networking options are available as well. See [action.yml](action.yml) for more details. The `FARGATE` +launch type requires `awsvpc` network mode in your task definition and you must specify a network configuration. ## Troubleshooting diff --git a/action.yml b/action.yml index 0a4cde074..29ecf22fb 100644 --- a/action.yml +++ b/action.yml @@ -53,7 +53,7 @@ inputs: description: 'A comma-separated list of subnet IDs to assign to a task when run outside of a service. Will default to none.' required: false run-task-assign-public-IP: - description: "Whether the task's elastic network interface receives a public IP address. The default value is DISABLED." + description: "Whether the task's elastic network interface receives a public IP address. The default value is DISABLED but will only be applied if run-task-subnets or run-task-security-groups are also set." required: false run-task-launch-type: description: "ECS launch type for tasks run outside of a service. Valid values are 'FARGATE' or 'EC2'. Will default to 'FARGATE'." diff --git a/dist/index.js b/dist/index.js index b3db50d86..8317024be 100644 --- a/dist/index.js +++ b/dist/index.js @@ -48,10 +48,10 @@ async function runTask(ecs, clusterName, taskDefArn, waitForMinutes) { awsvpcConfiguration["securityGroups"] = securityGroupIds.split(',') } - if(assignPublicIP != ""){ + if(assignPublicIP != "" && (subnetIds != "" || securityGroupIds != "")){ awsvpcConfiguration["assignPublicIp"] = assignPublicIP } - + const runTaskResponse = await ecs.runTask({ startedBy: startedBy, cluster: clusterName, @@ -60,7 +60,7 @@ async function runTask(ecs, clusterName, taskDefArn, waitForMinutes) { containerOverrides: containerOverrides }, launchType: launchType, - networkConfiguration: Object.keys(awsvpcConfiguration).length === 0 ? {} : { awsvpcConfiguration: awsvpcConfiguration } + networkConfiguration: Object.keys(awsvpcConfiguration).length === 0 ? null : { awsvpcConfiguration: awsvpcConfiguration } }); core.debug(`Run task response ${JSON.stringify(runTaskResponse)}`) diff --git a/index.js b/index.js index aa8960836..8a7027330 100644 --- a/index.js +++ b/index.js @@ -42,10 +42,10 @@ async function runTask(ecs, clusterName, taskDefArn, waitForMinutes) { awsvpcConfiguration["securityGroups"] = securityGroupIds.split(',') } - if(assignPublicIP != ""){ + if(assignPublicIP != "" && (subnetIds != "" || securityGroupIds != "")){ awsvpcConfiguration["assignPublicIp"] = assignPublicIP } - + const runTaskResponse = await ecs.runTask({ startedBy: startedBy, cluster: clusterName, @@ -54,7 +54,7 @@ async function runTask(ecs, clusterName, taskDefArn, waitForMinutes) { containerOverrides: containerOverrides }, launchType: launchType, - networkConfiguration: Object.keys(awsvpcConfiguration).length === 0 ? {} : { awsvpcConfiguration: awsvpcConfiguration } + networkConfiguration: Object.keys(awsvpcConfiguration).length === 0 ? null : { awsvpcConfiguration: awsvpcConfiguration } }); core.debug(`Run task response ${JSON.stringify(runTaskResponse)}`) diff --git a/index.test.js b/index.test.js index 5cf74ae09..95bb51330 100644 --- a/index.test.js +++ b/index.test.js @@ -1129,7 +1129,7 @@ describe('Deploy to ECS', () => { launchType: 'FARGATE', taskDefinition: 'task:def:arn', overrides: {"containerOverrides": []}, - networkConfiguration: {awsvpcConfiguration: {assignPublicIp: "DISABLED" }} + networkConfiguration: null }); expect(core.setOutput).toHaveBeenNthCalledWith(2, 'run-task-arn', ["arn:aws:ecs:fake-region:account_id:task/arn"]); @@ -1236,6 +1236,36 @@ describe('Deploy to ECS', () => { expect(waitUntilTasksStopped).toHaveBeenCalledTimes(1); }); + test('run task in bridge network mode', async () => { + core.getInput = jest + .fn() + .mockReturnValueOnce('task-definition.json') // task-definition + .mockReturnValueOnce('service-456') // service + .mockReturnValueOnce('somecluster') // cluster + .mockReturnValueOnce('true') // wait-for-service-stability + .mockReturnValueOnce('') // wait-for-minutes + .mockReturnValueOnce('') // force-new-deployment + .mockReturnValueOnce('') // desired-count + .mockReturnValueOnce('true') // run-task + .mockReturnValueOnce('true') // wait-for-task-stopped + .mockReturnValueOnce('someJoe') // run-task-started-by + .mockReturnValueOnce('EC2') // run-task-launch-type + .mockReturnValueOnce('') // run-task-subnet-ids + .mockReturnValueOnce('') // run-task-security-group-ids + .mockReturnValueOnce('') // run-task-container-overrides + .mockReturnValueOnce('') // run-task-assign-public-IP + + await run(); + expect(mockRunTask).toHaveBeenCalledWith({ + startedBy: 'someJoe', + cluster: 'somecluster', + taskDefinition: 'task:def:arn', + launchType: 'EC2', + overrides: { containerOverrides: [] }, + networkConfiguration: null + }); + }); + test('error is caught if run task fails with (wait-for-task-stopped: true)', async () => { core.getInput = jest .fn()