Skip to content

Commit 7b76e34

Browse files
authored
Create aws.yml
1 parent 61622e0 commit 7b76e34

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

.github/workflows/aws.yml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# This workflow will build and push a new container image to Amazon ECR,
2+
# and then will deploy a new task definition to Amazon ECS, when there is a push to the "master" branch.
3+
#
4+
# To use this workflow, you will need to complete the following set-up steps:
5+
#
6+
# 1. Create an ECR repository to store your images.
7+
# For example: `aws ecr create-repository --repository-name my-ecr-repo --region us-east-2`.
8+
# Replace the value of the `ECR_REPOSITORY` environment variable in the workflow below with your repository's name.
9+
# Replace the value of the `AWS_REGION` environment variable in the workflow below with your repository's region.
10+
#
11+
# 2. Create an ECS task definition, an ECS cluster, and an ECS service.
12+
# For example, follow the Getting Started guide on the ECS console:
13+
# https://us-east-2.console.aws.amazon.com/ecs/home?region=us-east-2#/firstRun
14+
# Replace the value of the `ECS_SERVICE` environment variable in the workflow below with the name you set for the Amazon ECS service.
15+
# Replace the value of the `ECS_CLUSTER` environment variable in the workflow below with the name you set for the cluster.
16+
#
17+
# 3. Store your ECS task definition as a JSON file in your repository.
18+
# The format should follow the output of `aws ecs register-task-definition --generate-cli-skeleton`.
19+
# Replace the value of the `ECS_TASK_DEFINITION` environment variable in the workflow below with the path to the JSON file.
20+
# Replace the value of the `CONTAINER_NAME` environment variable in the workflow below with the name of the container
21+
# in the `containerDefinitions` section of the task definition.
22+
#
23+
# 4. Store an IAM user access key in GitHub Actions secrets named `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`.
24+
# See the documentation for each action used below for the recommended IAM policies for this IAM user,
25+
# and best practices on handling the access key credentials.
26+
27+
name: Deploy to Amazon ECS
28+
29+
on:
30+
push:
31+
branches: [ "master" ]
32+
33+
env:
34+
AWS_REGION: us-east-1 # set this to your preferred AWS region, e.g. us-west-1
35+
ECR_REPOSITORY: amazon-ecs-deploy-task-definition # set this to your Amazon ECR repository name
36+
ECS_SERVICE: testing # set this to your Amazon ECS service name
37+
ECS_CLUSTER: testing-cluster # set this to your Amazon ECS cluster name
38+
ECS_TASK_DEFINITION: testing.json # set this to the path to your Amazon ECS task definition
39+
# file, e.g. .aws/task-definition.json
40+
CONTAINER_NAME: "app" # set this to the name of the container in the
41+
# containerDefinitions section of your task definition
42+
43+
jobs:
44+
deploy:
45+
name: Deploy
46+
runs-on: ubuntu-latest
47+
environment: development
48+
49+
steps:
50+
- name: Checkout
51+
uses: actions/checkout@v4
52+
53+
- name: Configure AWS credentials
54+
uses: aws-actions/configure-aws-credentials@v1
55+
with:
56+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
57+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
58+
aws-region: ${{ env.AWS_REGION }}
59+
60+
- name: Login to Amazon ECR
61+
id: login-ecr
62+
uses: aws-actions/amazon-ecr-login@v1
63+
64+
- name: Build, tag, and push image to Amazon ECR
65+
id: build-image
66+
env:
67+
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
68+
IMAGE_TAG: ${{ github.sha }}
69+
run: |
70+
# Build a docker container and
71+
# push it to ECR so that it can
72+
# be deployed to ECS.
73+
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
74+
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
75+
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
76+
77+
- name: Fill in the new image ID in the Amazon ECS task definition
78+
id: task-def
79+
uses: aws-actions/amazon-ecs-render-task-definition@v1
80+
with:
81+
task-definition: ${{ env.ECS_TASK_DEFINITION }}
82+
container-name: ${{ env.CONTAINER_NAME }}
83+
image: ${{ steps.build-image.outputs.image }}
84+
85+
- name: Deploy Amazon ECS task definition
86+
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
87+
with:
88+
task-definition: ${{ steps.task-def.outputs.task-definition }}
89+
service: ${{ env.ECS_SERVICE }}
90+
cluster: ${{ env.ECS_CLUSTER }}
91+
wait-for-service-stability: true

0 commit comments

Comments
 (0)