Skip to content
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

Option to Directly Specify taskDefinitionArn as an Input Parameter #524

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ The task definition file can be updated prior to deployment with the new contain
wait-for-service-stability: true
```

If you're using CloudFormation tools such as AWS CDK, Serverless Framework, or others to construct your task definition, you can directly pass the ARN of the task definition. For example:
```yaml
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: arn:aws:ecs:<region>:<aws_account_id>:task-definition/<task_definition_name>:<revision_number>
service: my-service
cluster: my-cluster
wait-for-service-stability: true

```


## Credentials and Region

This action relies on the [default behavior of the AWS SDK for Javascript](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html) to determine AWS credentials and region.
Expand Down
48 changes: 30 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ async function run() {
});

// Get inputs
const taskDefinitionFile = core.getInput('task-definition', { required: true });
const taskDefinitionContent = core.getInput('task-definition', { required: true });
const service = core.getInput('service', { required: false });
const cluster = core.getInput('cluster', { required: false });
const waitForService = core.getInput('wait-for-service-stability', { required: false });
Expand All @@ -276,23 +276,35 @@ async function run() {
const desiredCount = parseInt((core.getInput('desired-count', {required: false})));


// Register the task definition
core.debug('Registering the task definition');
const taskDefPath = path.isAbsolute(taskDefinitionFile) ?
taskDefinitionFile :
path.join(process.env.GITHUB_WORKSPACE, taskDefinitionFile);
const fileContents = fs.readFileSync(taskDefPath, 'utf8');
const taskDefContents = maintainValidObjects(removeIgnoredAttributes(cleanNullKeys(yaml.parse(fileContents))));
let registerResponse;
try {
registerResponse = await ecs.registerTaskDefinition(taskDefContents).promise();
} catch (error) {
core.setFailed("Failed to register task definition in ECS: " + error.message);
core.debug("Task definition contents:");
core.debug(JSON.stringify(taskDefContents, undefined, 4));
throw(error);
}
const taskDefArn = registerResponse.taskDefinition.taskDefinitionArn;
let taskDefArn = null;

// Of taskDefContent starts with arn: then we assume it is a task definition ARN
if (taskDefinitionContent.startsWith("arn:")) {
taskDefArn = taskDefinitionContent;

//
// Else we assume it is a task definition file
} else {
const taskDefinitionFile = taskDefinitionContent;

core.debug('Registering the task definition');
const taskDefPath = path.isAbsolute(taskDefinitionFile) ?
taskDefinitionFile :
path.join(process.env.GITHUB_WORKSPACE, taskDefinitionFile);
const fileContents = fs.readFileSync(taskDefPath, 'utf8');
const taskDefContents = maintainValidObjects(removeIgnoredAttributes(cleanNullKeys(yaml.parse(fileContents))));
let registerResponse;
try {
registerResponse = await ecs.registerTaskDefinition(taskDefContents).promise();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR needs to be updated to use AWS SDK for JavaScript (v3)

Refer: #529

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
registerResponse = await ecs.registerTaskDefinition(taskDefContents).promise();
registerResponse = await ecs.registerTaskDefinition(taskDefContents);

} catch (error) {
core.setFailed("Failed to register task definition in ECS: " + error.message);
core.debug("Task definition contents:");
core.debug(JSON.stringify(taskDefContents, undefined, 4));
throw(error);
}
taskDefArn = registerResponse.taskDefinition.taskDefinitionArn;
}

core.setOutput('task-definition-arn', taskDefArn);

// Update the service with the new task definition
Expand Down
15 changes: 15 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const mockEcsWaiter = jest.fn();
const mockCodeDeployCreateDeployment = jest.fn();
const mockCodeDeployGetDeploymentGroup = jest.fn();
const mockCodeDeployWaiter = jest.fn();

let config = {
region: 'fake-region',
};
Expand Down Expand Up @@ -151,6 +152,20 @@ describe('Deploy to ECS', () => {
});
});

test('uses task definition ARN if taskDefinitionContent starts with arn:', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('arn:aws:ecs:region:account-id:task-definition/task-name:task-revision') // task-definition
.mockReturnValueOnce('service-456') // service
.mockReturnValueOnce('cluster-789'); // cluster

await run();

expect(core.setFailed).toHaveBeenCalledTimes(0);
expect(mockEcsRegisterTaskDef).toHaveBeenCalledTimes(0); // Importante, não deve chamar a função de registro
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'task-definition-arn', 'arn:aws:ecs:region:account-id:task-definition/task-name:task-revision');
});

test('registers the task definition contents and updates the service', async () => {
await run();
expect(core.setFailed).toHaveBeenCalledTimes(0);
Expand Down
Loading