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

Improve CD workflow #888

Merged
merged 6 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
26 changes: 22 additions & 4 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name: CD
on:
workflow_dispatch:
# push:
# Uncomment and add the necessary branches to enable automatic deployment on AWS
# branches:
# - main
# Uncomment and add the necessary branches to enable automatic deployment on AWS
# branches:
# - main
blacksam07 marked this conversation as resolved.
Show resolved Hide resolved

jobs:
deploy:
Expand Down Expand Up @@ -36,7 +36,9 @@ jobs:
with:
context: .
push: true
tags: ${{ steps.login-ecr.outputs.registry }}/${{ vars.ECR_REPOSITORY }}:${{ github.sha }}
tags: |
${{ steps.login-ecr.outputs.registry }}/${{ vars.ECR_REPOSITORY }}:${{ github.sha }}
${{ steps.login-ecr.outputs.registry }}/${{ vars.ECR_REPOSITORY }}:latest

- name: Get the image digest
id: image-digest
Expand All @@ -46,13 +48,29 @@ jobs:
run: |
aws ecs describe-task-definition --task-definition ${{ vars.ECS_TASK_DEFINITION }} --query taskDefinition > ${{ vars.ECS_TASK_DEFINITION_PATH }}

- name: Download Parameter Store Values
id: ssm-download
run: |
PARAMETERS_JSON=$(aws ssm describe-parameters --query "Parameters[?contains(Name, 'backend')].{Name:Name,ARN:ARN}" --output json | jq -c '.')
Copy link
Contributor

Choose a reason for hiding this comment

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

Shoulds we use contains or maybe only check if it starts with that key? Just thinking that maybe some other keys are named backend and we end up pulling them as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I use contains because the name convention that Devops use is project_name/(backend|frontend)/var_name and I think is a good way to search, which scenery do you think could be retrieve keys that I don't want

echo "parameters=${PARAMETERS_JSON}" >> $GITHUB_OUTPUT

- name: Format SSM Parameters
id: format-secrets
run: |
FORMATTED_SECRETS=$(ruby bin/format_secrets.rb)
echo "formatted_secrets=${FORMATTED_SECRETS}" >> $GITHUB_OUTPUT
env:
SSM_PARAMETERS_JSON: "${{ steps.ssm-download.outputs.parameters }}"

- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ vars.ECS_TASK_DEFINITION_PATH }}
container-name: ${{ vars.CONTAINER_NAME }}
image: ${{ steps.image-digest.outputs.image }}
secrets: |
${{ steps.format-secrets.outputs.formatted_secrets }}

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
Expand Down
11 changes: 11 additions & 0 deletions bin/format_secrets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env ruby
blacksam07 marked this conversation as resolved.
Show resolved Hide resolved
require 'json'

data = JSON.parse(ENV['SSM_PARAMETERS_JSON'])
blacksam07 marked this conversation as resolved.
Show resolved Hide resolved

formatted_secrets = data.map do |param|
param_name = param['Name'].split('/').last.upcase
"#{param_name}=#{param['ARN']}"
end.join('\n')

puts formatted_secrets
35 changes: 30 additions & 5 deletions docs/cd_with_aws.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ Before you start, make sure you have the following:

1. **AWS Account**: You need an AWS account. Sign up [here](https://aws.amazon.com/).

2. **Amazon ECR (Elastic Container Registry) Setup**:
2. **Amazon ECR (Elastic Container Registry) Setup**:

- Create a new repository in Amazon ECR.
- Note down the repository URI, which will be used in the GitHub Actions workflow.

3. **AWS Credentials**:
3. **AWS Credentials**:

- AWS Access Key ID
- AWS Secret Access Key
- These credentials should have permission to interact with ECR and ECS.
- These credentials should have permission to interact with ECR and ECS and Parameter Store.

4. **Create Environments**:

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.

5. **GitHub Repository Setup**:

- **Environment Secrets**: Add the following secrets to your GitHub environments (these are specific to each environment and not set at the repository level):
- `AWS_ACCESS_KEY_ID`: Your AWS Access Key ID.
- `AWS_SECRET_ACCESS_KEY`: Your AWS Secret Access Key.
Expand All @@ -34,7 +37,28 @@ Before you start, make sure you have the following:
- `ECS_SERVICE`: The name of your ECS service.
- `ECS_CLUSTER`: The name of your ECS cluster.

6. **GitHub Actions Workflow**:
6. **Brief Guide to Configure AWS Systems Manager Parameter Store for GitHub Actions Workflow**:

- **Access AWS Systems Manager**:

- Log in to your AWS console.
- Navigate to **Systems Manager** and select **Parameter Store**.

- **Create Parameters**:

- Click **Create parameter**.
- Fill out the details:
- **Name**: Provide a unique and valid name (e.g., `/rails_api_base/backend/service_api_key`).
blacksam07 marked this conversation as resolved.
Show resolved Hide resolved
- **Type**: Choose `SecureString` for sensitive data.
- **Value**: Enter the parameter value (e.g., a password or secret).
- Click **Create parameter**.

- **Integrate with GitHub Actions**:

- Make sure the AWS credentials stored in GitHub Secrets (`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`) have the appropriate permissions for Paramters Store.
- The workflow automatically access to the defined Parameters Store and push inside the `secrets:` of the Definition Task.

7. **GitHub Actions Workflow**:
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.

Uncomment the branches section under `on: push:` and add the necessary branches to enable automatic deployment. For example:
Expand All @@ -44,4 +68,5 @@ Before you start, make sure you have the following:
push:
branches:
- main
- dev
- dev
```
Loading