Skip to content

Commit 6382ef4

Browse files
committed
Add confirm about the Github Action deploy to AWS ECS
1 parent 1425c32 commit 6382ef4

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

lib/nimble_template/addons/github.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,14 @@ defmodule NimbleTemplate.Addons.Github do
5151

5252
project
5353
end
54+
55+
@impl true
56+
def do_apply(%Project{} = project, opts) when opts.github_action_deploy_aws_ecs do
57+
Generator.copy_file([
58+
{:eex, ".github/workflows/deploy_to_aws_ecs.yml.eex",
59+
".github/workflows/deploy_to_aws_ecs.yml"}
60+
])
61+
62+
project
63+
end
5464
end

lib/nimble_template/helpers/github.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ defmodule NimbleTemplate.GithubHelper do
99

1010
def generate_github_action?(),
1111
do: Mix.shell().yes?("\nDo you want to generate the Github Action workflow?")
12+
13+
def generate_github_action_deploy_aws?(),
14+
do: Mix.shell().yes?("\nDo you want to generate the Github Action to deploy to AWS ECS?")
1215
end

lib/nimble_template/variants/phoenix/template.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ defmodule NimbleTemplate.Phoenix.Template do
3838

3939
if generate_github_action?(),
4040
do: Addons.Github.apply(project, %{github_action: true})
41+
42+
if generate_github_action_deploy_aws?(),
43+
do: Addons.Github.apply(project, %{github_action_deploy_aws_ecs: true})
4144
end
4245

4346
if install_addon_prompt?("Oban"), do: Addons.Oban.apply(project)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Deploy to Amazon ECS
2+
3+
on:
4+
workflow_run:
5+
branches:
6+
- develop
7+
types:
8+
- completed
9+
workflow_dispatch:
10+
11+
env:
12+
DOCKER_REGISTRY_HOST: ${{ secrets.ECR_REGISTRY_HOST }}
13+
DOCKER_IMAGE: ${{ secrets.STAGING_ECR_REPO }}
14+
ECS_CLUSTER: ${{ secrets.STAGING_ECS_CLUSTER }}
15+
ECS_SERVICE: ${{ secrets.STAGING_ECS_SERVICE }}
16+
BRANCH_TAG: latest
17+
18+
jobs:
19+
deploy:
20+
name: Deploy to ECS
21+
runs-on: ubuntu-latest
22+
steps:
23+
- name: Cancel previous runs
24+
uses: styfle/[email protected]
25+
with:
26+
access_token: ${{ github.token }}
27+
28+
- name: Configure AWS credentials
29+
uses: aws-actions/configure-aws-credentials@v1
30+
with:
31+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
32+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
33+
aws-region: ${{ secrets.AWS_REGION }}
34+
35+
- uses: actions/[email protected]
36+
37+
- name: Login to Amazon ECR
38+
id: login-ecr
39+
uses: aws-actions/amazon-ecr-login@v1
40+
41+
- name: Build, tag, and push image to Amazon ECR
42+
id: build-image
43+
env:
44+
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
45+
ECR_REPOSITORY: $ECR_REPOSITORY
46+
IMAGE_TAG: ${{ github.sha }}
47+
run: |
48+
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
49+
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
50+
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
51+
52+
- name: Update Service
53+
run: aws ecs update-service --cluster $ECS_CLUSTER --service $ECS_SERVICE --force-new-deployment

0 commit comments

Comments
 (0)