Skip to content

Commit a3a9765

Browse files
authored
Add CD with github actions (#704)
1 parent add255f commit a3a9765

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

.github/workflows/cd.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: CD
2+
3+
on:
4+
workflow_dispatch:
5+
# push:
6+
# Uncomment and add the necessary branches to enable automatic deployment on AWS
7+
# branches:
8+
# - main
9+
10+
jobs:
11+
deploy:
12+
name: Deploy on AWS ECS
13+
runs-on: ubuntu-latest
14+
environment:
15+
name: ${{ github.ref == 'refs/heads/main' && 'production' || github.ref_name }}
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
20+
- name: Configure AWS credentials
21+
uses: aws-actions/configure-aws-credentials@v1
22+
with:
23+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
24+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
25+
aws-region: ${{ vars.AWS_REGION }}
26+
27+
- name: Set up Docker Buildx
28+
uses: docker/setup-buildx-action@v2
29+
30+
- name: Login to Amazon ECR
31+
uses: aws-actions/amazon-ecr-login@v1
32+
33+
- name: Build and push Docker image
34+
id: build-and-push
35+
uses: docker/build-push-action@v4
36+
with:
37+
context: .
38+
push: true
39+
tags: ${{ steps.login-ecr.outputs.registry }}/${{ vars.ECR_REPOSITORY }}:${{ github.sha }}
40+
41+
- name: Get the image digest
42+
id: image-digest
43+
run: echo "image=${{ steps.login-ecr.outputs.registry }}/${{ vars.ECR_REPOSITORY }}:${{ github.sha }}" >> $GITHUB_OUTPUT
44+
45+
- name: Download task definition
46+
run: |
47+
aws ecs describe-task-definition --task-definition ${{ vars.ECS_TASK_DEFINITION }} --query taskDefinition > ${{ vars.ECS_TASK_DEFINITION_PATH }}
48+
49+
- name: Fill in the new image ID in the Amazon ECS task definition
50+
id: task-def
51+
uses: aws-actions/amazon-ecs-render-task-definition@v1
52+
with:
53+
task-definition: ${{ vars.ECS_TASK_DEFINITION_PATH }}
54+
container-name: ${{ vars.CONTAINER_NAME }}
55+
image: ${{ steps.image-digest.outputs.image }}
56+
57+
- name: Deploy Amazon ECS task definition
58+
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
59+
with:
60+
task-definition: ${{ steps.task-def.outputs.task-definition }}
61+
service: ${{ vars.ECS_SERVICE }}
62+
cluster: ${{ vars.ECS_CLUSTER }}
63+
wait-for-service-stability: true

docs/cd_with_aws.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Continuous Deployment with AWS
2+
3+
This document explains how to set up Continuous Deployment (CD) with AWS using GitHub Actions.
4+
5+
## Prerequisites
6+
7+
Before you start, make sure you have the following:
8+
9+
1. **AWS Account**: You need an AWS account. Sign up [here](https://aws.amazon.com/).
10+
11+
2. **Amazon ECR (Elastic Container Registry) Setup**:
12+
- Create a new repository in Amazon ECR.
13+
- Note down the repository URI, which will be used in the GitHub Actions workflow.
14+
15+
3. **AWS Credentials**:
16+
- AWS Access Key ID
17+
- AWS Secret Access Key
18+
- These credentials should have permission to interact with ECR and ECS.
19+
20+
4. **Create Environments**:
21+
22+
The GitHub Actions workflow will automatically deploy to the correct environment based on the branch being pushed to. The branch `main` will always be linked to the `production` environment, while other branches will use their own names as the environment. All environments added in GitHub must have the same name as the branches.
23+
24+
5. **GitHub Repository Setup**:
25+
- **Environment Secrets**: Add the following secrets to your GitHub environments (these are specific to each environment and not set at the repository level):
26+
- `AWS_ACCESS_KEY_ID`: Your AWS Access Key ID.
27+
- `AWS_SECRET_ACCESS_KEY`: Your AWS Secret Access Key.
28+
- **Environment Variables**: Add the following variables to your GitHub environments:
29+
- `AWS_REGION`: The region where your ECR and ECS are set up (e.g., `us-east-1`).
30+
- `ECR_REPOSITORY`: The name of your ECR repository.
31+
- `ECS_TASK_DEFINITION`: The ARN of your ECS task definition.
32+
- `ECS_TASK_DEFINITION_PATH`: The path to your ECS task definition file.
33+
- `CONTAINER_NAME`: The name of the container defined in your ECS task definition.
34+
- `ECS_SERVICE`: The name of your ECS service.
35+
- `ECS_CLUSTER`: The name of your ECS cluster.
36+
37+
6. **GitHub Actions Workflow**:
38+
To set up the GitHub Actions workflow for continuous deployment to AWS, you need to modify the existing cd.yml file in the .github/workflows directory of your GitHub repository.
39+
40+
Uncomment the branches section under `on: push:` and add the necessary branches to enable automatic deployment. For example:
41+
42+
```yaml
43+
on:
44+
push:
45+
branches:
46+
- main
47+
- dev

0 commit comments

Comments
 (0)