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

Add the Github Action deploy to AWS ECS #125

Draft
wants to merge 15 commits into
base: develop
Choose a base branch
from
12 changes: 12 additions & 0 deletions .github/wiki/Addon:-Github-AWS-ECS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Environment Variables

To use this workflow, you need to config these variables in the [GitHub Actions secrets](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets) of the repository:

- `AWS_REGION`
- `AWS_ACCESS_KEY_ID`
- `AWS_SECRET_ACCESS_KEY`
- `ECS_CLUSTER`
- `ECS_SERVICE`
- `ECS_TASK_DEFINITION`
- `ECS_CONTAINER_NAME`
- `ECR_REPOSITORY`
5 changes: 5 additions & 0 deletions .github/wiki/Addon:-Github-Wiki.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Environment Variables

To use this workflow, you need to config these variables in the [GitHub Actions secrets](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets) of the repository:

- `WIKI_ACTION_TOKEN`: Generate [personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) to allow action push the markdown content files in `.github/wiki` to Github Wiki section.
6 changes: 6 additions & 0 deletions .github/wiki/_Sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
- [[Docker]]
- [[Testing]]

## Add-ons

- Github
- [[Addon:-Github-AWS-ECS]]
- [[Addon:-Github-Wiki]]

## Release

- [[Release]]
Expand Down
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ create_mix_project:
# Y - in response to Will you host this project on Github?
# Y - in response to Do you want to generate the .github/ISSUE_TEMPLATE and .github/PULL_REQUEST_TEMPLATE?
# Y - in response to Do you want to generate the Github Action workflows: Test?
# Y - in response to Do you want to generate the Github Action workflows: Deploy to Heroku?
# Y - in response to Do you want to generate the .github/.workflow/README file?
# Y - in response to Do you want to publish a Github Wiki for this project? You'd need to manually create the first Github Wiki Page and set the GH_TOKEN and GH_EMAIL secret for this to properly function.
# Y - in response to Do you want to generate the Github Action workflows: Deploy to Heroku?
# Y - in response to Do you want to generate the Github Action workflows: Deploy to AWS ECS?
# Y - in response to Would you like to add the Oban addon?
# Y - in response to Would you like to add the ExVCR addon?
common_addon_prompts = Y\nY\nY\nY\nY\nY\nY\nY\n
common_addon_prompts = Y\nY\nY\nY\nY\nY\nY\nY\nY\n

# Y - in response to Would you like to add the SVG Sprite addon?
# Y - in response to Would you like to add the Dart Sass addon?
Expand All @@ -36,8 +37,9 @@ live_addon_prompts =
# Y - in response to Do you want to generate the Github Action workflow: Test?
# Y - in response to Do you want to generate the .github/.workflow/README file?
# Y - in response to Do you want to publish a Github Wiki for this project? You'd need to manually create the first Github Wiki Page and set the GH_TOKEN and GH_EMAIL secret for this to properly function.
# Y - in response to Do you want to generate the Github Action to deploy to AWS ECS?
# Y - in response to Would you like to add the Mimic addon?
mix_addon_prompts = Y\nY\nY\nY\nY\nY\n
mix_addon_prompts = Y\nY\nY\nY\nY\nY\nY\n

post_setup_addon_prompts =

Expand Down
16 changes: 14 additions & 2 deletions lib/nimble_template/addons/github.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,17 @@ defmodule NimbleTemplate.Addons.Github do
%{
github_workflows_readme: true,
with_test_workflow?: with_test_workflow?,
with_deploy_to_heroku_workflow?: with_deploy_to_heroku_workflow?
with_deploy_to_heroku_workflow?: with_deploy_to_heroku_workflow?,
with_deploy_to_aws_ecs_workflow?: with_deploy_to_aws_ecs_workflow?
}
) do
Generator.copy_file(
[
{:eex, ".github/workflows/README.md.eex", ".github/workflows/README.md"}
],
with_test_workflow?: with_test_workflow?,
with_deploy_to_heroku_workflow?: with_deploy_to_heroku_workflow?
with_deploy_to_heroku_workflow?: with_deploy_to_heroku_workflow?,
with_deploy_to_aws_ecs_workflow?: with_deploy_to_aws_ecs_workflow?
)

project
Expand Down Expand Up @@ -98,6 +100,16 @@ defmodule NimbleTemplate.Addons.Github do
project
end

@impl true
def do_apply(%Project{mix_project?: false} = project, %{github_action_deploy_aws_ecs: true}) do
Generator.copy_file([
{:eex, ".github/workflows/deploy_to_aws_ecs.yml.eex",
".github/workflows/deploy_to_aws_ecs.yml"}
])

project
end

@impl true
def do_apply(%Project{} = project, %{github_wiki: true}) do
project
Expand Down
3 changes: 3 additions & 0 deletions lib/nimble_template/helpers/github.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ defmodule NimbleTemplate.GithubHelper do
def generate_github_action_deploy_heroku?(),
do: Mix.shell().yes?("\nDo you want to generate the Github Action workflows: Deploy to Heroku?")

def generate_github_action_deploy_aws_ecs?(),
do: Mix.shell().yes?("\nDo you want to generate the Github Action workflows: Deploy to AWS ECS?")

def has_github_wiki_directory?(), do: File.dir?(".github/wiki/")
end
12 changes: 10 additions & 2 deletions lib/nimble_template/templates/variants/phoenix/template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,24 @@ defmodule NimbleTemplate.Templates.Phoenix.Template do
if generate_github_action_deploy_heroku?,
do: Addons.Github.apply(project, %{github_action_deploy_heroku: true})

generate_github_action_deploy_aws_ecs? = generate_github_action_deploy_aws_ecs?()

if generate_github_action_deploy_aws_ecs?,
do: Addons.Github.apply(project, %{github_action_deploy_aws_ecs: true})

if generate_github_workflows_readme?(),
do:
Addons.Github.apply(project, %{
github_workflows_readme: true,
with_test_workflow?: generate_github_action_test?,
with_deploy_to_heroku_workflow?: generate_github_action_deploy_heroku?
})
with_deploy_to_heroku_workflow?: generate_github_action_deploy_heroku?,
with_deploy_to_aws_ecs_workflow?: generate_github_action_deploy_aws_ecs?
})

if generate_github_wiki?(),
do: Addons.Github.apply(project, %{github_wiki: true})

project
end

defp apply_phoenix_variant_setup(%Project{api_project?: true} = project),
Expand Down
15 changes: 13 additions & 2 deletions priv/templates/nimble_template/.github/workflows/README.md.eex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The following workflows are supported.

- A pre-generated [Heroku App](https://devcenter.heroku.com/articles/creating-apps)
- A Heroku API key. It can be generated under your [Account Settings](https://dashboard.heroku.com/account#api-key)
- Three Heroku config vars:
- Three Heroku config vars:
- **DATABASE_URL**: It will be created automatically when the [PostgreSQL add-on](https://elements.heroku.com/addons/heroku-postgresql) is added.
- **PHX_HOST**: if your app name is `acme`, the value of this var is: `acme.herokuapp.com`
- **HEALTH_PATH**: Health path (eg: "/_health")
Expand All @@ -31,8 +31,19 @@ The following workflows are supported.
socket "/socket", HelloWeb.UserSocket,
websocket: [timeout: 45_000],
longpoll: false
...
...
```

otherwise, leaving it set to `false` as default.
<% end %>


<%= if with_deploy_to_aws_ecs_workflow? do %>
- [Deploy to AWS ECS](#deploy-to-aws-ecs-workflow-usage-instruction)
<% end %>

<%= if with_deploy_to_aws_ecs_workflow? do %>
## Deploy to AWS ECS usage instruction

### Requirements
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Deploy to Amazon ECS

on:
push:
branches:
- main
- develop

env:
ECS_CLUSTER: ${{ secrets.ECS_CLUSTER }}
ECS_SERVICE: ${{ secrets.ECS_SERVICE }}
ECS_TASK_DEFINITION: ${{ secrets.ECS_TASK_DEFINITION }}
ECS_CONTAINER_NAME: ${{ secrets.ECS_CONTAINER_NAME }}
ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY }}

jobs:
deploy:
name: Deploy to ECS
runs-on: ubuntu-latest
steps:
- name: Cancel previous runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- name: Checkout
uses: actions/[email protected]

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ env.ECR_REPOSITORY }}
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"

- name: Log out of Amazon ECR
if: always()
run: docker logout ${{ steps.login-ecr.outputs.registry }}

- name: Download task definition
run: |
aws ecs describe-task-definition --task-definition ${{ env.ECS_TASK_DEFINITION }} \
--query taskDefinition > task-definition.json

- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@97587c9d45a4930bf0e3da8dd2feb2a463cf4a3a
with:
task-definition: task-definition.json
container-name: ${{ env.ECS_CONTAINER_NAME }}
image: ${{ steps.build-image.outputs.image }}

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@100cd113ebe287f1550436363834e6f2b2b4c16f
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
USER_NAME: github-wiki-workflow
USER_EMAIL: ${{ secrets.GH_EMAIL }}
secrets:
USER_TOKEN: ${{ secrets.GH_TOKEN }}
USER_TOKEN: ${{ secrets.WIKI_ACTION_TOKEN }}
15 changes: 15 additions & 0 deletions test/nimble_template/addons/github_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -547,4 +547,19 @@ defmodule NimbleTemplate.Addons.GithubTest do
end)
end
end

describe "#apply/2 with web_project and github_action_deploy_to_aws_ecs option" do
test "copies the .github/workflows/deploy_to_aws_ecs.yml file", %{
project: project,
test_project_path: test_project_path
} do
project = %{project | api_project?: false, web_project?: true}

in_test_project(test_project_path, fn ->
Addons.Github.apply(project, %{github_action_deploy_aws_ecs: true})

assert_file(".github/workflows/deploy_to_aws_ecs.yml")
end)
end
end
end