Skip to content

Commit b06b935

Browse files
authored
chore: Update documentation on task def file management (#71)
1 parent 476d343 commit b06b935

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

README.md

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Registers an Amazon ECS task definition and deploys it to an ECS service.
77
<!-- toc -->
88

99
- [Usage](#usage)
10+
+ [Task definition file](#task-definition-file)
11+
+ [Task definition container image values](#task-definition-container-image-values)
1012
- [Credentials and Region](#credentials-and-region)
1113
- [Permissions](#permissions)
1214
- [AWS CodeDeploy Support](#aws-codedeploy-support)
@@ -28,10 +30,78 @@ Registers an Amazon ECS task definition and deploys it to an ECS service.
2830
wait-for-service-stability: true
2931
```
3032
31-
The action can be passed a `task-definition` generated dynamically via [the `aws-actions/amazon-ecs-render-task-definition` action](https://github.com/aws-actions/amazon-ecs-render-task-definition).
32-
3333
See [action.yml](action.yml) for the full documentation for this action's inputs and outputs.
3434
35+
### Task definition file
36+
37+
It is highly recommended to treat the task definition "as code" by checking it into your git repository as a JSON file. Changes to any task definition attributes like container images, environment variables, CPU, and memory can be deployed with this GitHub action by editing your task definition file and pushing a new git commit.
38+
39+
An existing task definition can be downloaded to a JSON file with the following command. Account IDs can be removed from the file by removing the `taskDefinitionArn` attribute, and updating the `executionRoleArn` and `taskRoleArn` attribute values to contain role names instead of role ARNs.
40+
```sh
41+
aws ecs describe-task-definition \
42+
--task-definition my-task-definition-family \
43+
--query taskDefinition > task-definition.json
44+
```
45+
46+
Alternatively, you can start a new task definition file from scratch with the following command. In the generated file, fill in your attribute values and remove any attributes not needed for your application.
47+
```sh
48+
aws ecs register-task-definition \
49+
--generate-cli-skeleton > task-definition.json
50+
```
51+
52+
If you do not wish to store your task definition as a file in your git repository, your GitHub Actions workflow can download the existing task definition.
53+
```yaml
54+
- name: Download task definition
55+
run: |
56+
aws ecs describe-task-definition --task-definition my-task-definition-family --query taskDefinition > task-definition.json
57+
```
58+
59+
### Task definition container image values
60+
61+
It is highly recommended that each time your GitHub Actions workflow runs and builds a new container image for deployment, a new container image ID is generated. For example, use the commit ID as the new image's tag, instead of updating the 'latest' tag with the new image. Using a unique container image ID for each deployment allows rolling back to a previous container image.
62+
63+
The task definition file can be updated prior to deployment with the new container image ID using [the `aws-actions/amazon-ecs-render-task-definition` action](https://github.com/aws-actions/amazon-ecs-render-task-definition). The following example builds a new container image tagged with the commit ID, inserts the new image ID as the image for the `my-container` container in the task definition file, and then deploys the rendered task definition file to ECS:
64+
65+
```yaml
66+
- name: Configure AWS credentials
67+
uses: aws-actions/configure-aws-credentials@v1
68+
with:
69+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
70+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
71+
aws-region: us-east-2
72+
73+
- name: Login to Amazon ECR
74+
id: login-ecr
75+
uses: aws-actions/amazon-ecr-login@v1
76+
77+
- name: Build, tag, and push image to Amazon ECR
78+
id: build-image
79+
env:
80+
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
81+
ECR_REPOSITORY: my-ecr-repo
82+
IMAGE_TAG: ${{ github.sha }}
83+
run: |
84+
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
85+
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
86+
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
87+
88+
- name: Fill in the new image ID in the Amazon ECS task definition
89+
id: task-def
90+
uses: aws-actions/amazon-ecs-render-task-definition@v1
91+
with:
92+
task-definition: task-definition.json
93+
container-name: my-container
94+
image: ${{ steps.build-image.outputs.image }}
95+
96+
- name: Deploy Amazon ECS task definition
97+
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
98+
with:
99+
task-definition: ${{ steps.task-def.outputs.task-definition }}
100+
service: my-service
101+
cluster: my-cluster
102+
wait-for-service-stability: true
103+
```
104+
35105
## Credentials and Region
36106

37107
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.

0 commit comments

Comments
 (0)