Skip to content

Commit c14225f

Browse files
committed
Add server github actions.
1 parent f231f0a commit c14225f

3 files changed

Lines changed: 303 additions & 0 deletions

File tree

.github/workflows/README.md

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# ServiceStack mix GitHub Actions
2+
`release.yml` generated from `x mix release-ecr-aws`, this template in designed to help with automating CI deployments to AWS ECS and dedicated AWS ECS cluster.
3+
This is a cheap way to start without an AWS Application Load Balancer (ALB) and also be in a situation that will easier to add one once the web service needs additional scale or high availability.
4+
5+
## Overview
6+
`release.yml` is designed to work with a ServiceStack app templates deploying directly to a single server in a dedicated ECS cluster via templated GitHub Actions.
7+
8+
## Setup
9+
### Create unique ECS cluster
10+
For this setup, it is best to create a separate cluster as cluster will only have the single instance in it running.
11+
This pattern is to start from a good base with AWS ECS and automated CI deployment while avoiding the higher costs of needing to run an application load balancer (ALB).
12+
13+
If/when you can justify the cost of an ALB for easier scaling and zero downtime deployment, the GitHub Action `release.yml` can be slightly modified to be used with a re-created or different ECS Service that is configured to be used with an Application Load Balancer and Target Group.
14+
15+
### Elastic IP (optional)
16+
The reason you might want to register this first is because we are only running one EC2 instance and hosting our own `nginx-proxy` on the same instance as the applications.
17+
Since an `A` record will be pointing there, one advantage of not using an auto-assigned IP is that we can reassign the elastic IP if for what ever reason the instance goes down or is lost.
18+
19+
## Launch to EC2 Instance
20+
When launching the EC2 instance, you'll need to select an 'ECS optimized' AMI as the image used for your instance.
21+
### Choose AMI
22+
The easiest way to find the latest Amazon Linux 2 image for this is to go to the [AWS documentation for ECS-optimized AMIs and look up your region here](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html#ecs-optimized-ami-linux).
23+
24+
Using the AMI ID (starts with `ami-`) at the bottom, search in the 'Community AMIs' tab on the first step of the `Launch EC2 Instance` wizard.
25+
26+
### Choose Instance Type
27+
A t2.micro or larger will work fine, this pattern can be used to host multiple applications on the 1 server so if the number of applications gets larger, you might need a larger instance type.
28+
> Note this pattern is suitable for testing prototypes or low traffic applications as it is cost effective and makes it easy to bundle multiple apps onto 1 EC2 instance.
29+
30+
### Configure Instance
31+
Under `IAM role`, use the `ecsInstanceRole`, if this is not available, see [AWS documentation for the process of checking if it exists and creating it if needed](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/instance_IAM_role.html).
32+
33+
If you are *not* using your own generated Elastic IP, select `Enabled` for `Auto-assign Public IP`.
34+
35+
You will also want to add the following Userdata script (in the `Configure` step of the launch wizard) with your own `ECS_CLUSTER` value. This tells the ecs-agent running on the instance which ECS cluster the instance should join.
36+
37+
```bash
38+
#!/bin/bash
39+
cat <<EOS >/etc/ecs/ecs.config
40+
ECS_CLUSTER=my-cluster
41+
ECS_AVAILABLE_LOGGING_DRIVERS=["awslogs", "syslog"]
42+
ECS_ENABLE_CONTAINER_METADATA=true
43+
EOS
44+
```
45+
46+
Note down your cluster name as it will need to be used to create the cluster in ECS before it is visible.
47+
See [`ECS Container Agent Configuration`](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-config.html) for more information.
48+
49+
### Add Storage
50+
The default of 30gb is fine but take into account how large/how many applications you'll have running.
51+
52+
### Configure Security Groups
53+
You'll want to expose at least ports 80 and 443.
54+
55+
### Setup Docker-compose and nginx-proxy
56+
To let your server handle multiple ServiceStack applications and automate the generation and management of TLS certificates, an additional docker-compose file is provided via the `x mix` template, `nginx-proxy-compose.yml`. This docker-compose file is ready to run and can be copied to the deployment server.
57+
> This is done via docker-compose rather than via ECS itself for simplicity.
58+
59+
First you'll need to install `docker-compose`.
60+
61+
```bash
62+
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
63+
sudo chmod +x /usr/local/bin/docker-compose
64+
```
65+
Run `docker-compose --version` to confirm.
66+
67+
To copy you can use scp or just creating a new file via server text editor to copy the short YML file over. For this example, we are going to copy it straight to the ~/ (home) directory.
68+
69+
```bash
70+
scp -i <path to private ssh key> ./nginx-proxy-compose.yml ec2-user@<server_floating_ip>:~/nginx-proxy.compose.yml
71+
```
72+
73+
For example, once copied to remote `~/nginx-proxy-compose.yml`, the following command can be run on the remote server.
74+
75+
```
76+
docker-compose -f ~/nginx-proxy-compose.yml up -d
77+
```
78+
79+
This will run an nginx reverse proxy along with a companion container that will watch for additional containers in the same docker network and attempt to initialize them with valid TLS certificates. This includes containers created and managed by the ECS agent.
80+
> If the container doesn't have the environment variable `VIRTUAL_HOST` set, it will be ignored.
81+
82+
## GitHub Repository setup
83+
The `release.yml` assumes 6 secrets have been setup.
84+
85+
- AWS_ACCESS_KEY_ID - AWS access key for programmatic access to AWS APIs.
86+
- AWS_SECRET_ACCESS_KEY - AWS access secrets for programmatic access to AWS APIs.
87+
- AWS_REGION - default region for AWS API calls.
88+
- AWS_ECS_CLUSTER - Cluster name in ECS, this should match the value in your Userdata.
89+
- HOST_DOMAIN - Domain/sub-domain of your application, eg `vitepress-docs.example.com` .
90+
- LETSENCRYPT_EMAIL - Email address, required for Let's Encrypt automated TLS certificates.
91+
92+
These secrets can use the [GitHub CLI](https://cli.github.com/manual/gh_secret_set) for ease of creation. Eg, using the GitHub CLI the following can be set.
93+
94+
```bash
95+
gh secret set AWS_ACCESS_KEY_ID -b"<AWS_ACCESS_KEY_ID>"
96+
gh secret set AWS_SECRET_ACCESS_KEY -b"<AWS_SECRET_ACCESS_KEY>"
97+
gh secret set AWS_REGION -b"<AWS_REGION, eg us-east-1>"
98+
gh secret set AWS_ECS_CLUSTER -b"<AWS_ECS_CLUSTER, eg vitepress-docss>"
99+
gh secret set HOST_DOMAIN -b"<HOST_DOMAIN, eg vitepress-docs.example.com>"
100+
gh secret set LETSENCRYPT_EMAIL -b"<LETSENCRYPT_EMAIL, eg me@example.com>"
101+
```
102+
103+
These secrets are used to populate variables within GitHub Actions and other configuration files.
104+
105+
For the AWS access, a separate user specifically for deploying via GitHub Actions should be used.
106+
107+
The policies required for the complete initial setup will be:
108+
- `AmazonEC2ContainerRegistryFullAccess`
109+
- `AmazonECS_FullAccess`
110+
111+
Once the application is successfully deployed the first time, reduced access for both ECR and ECS can be used instead. For application updates, the GitHub Action can use the following policy.
112+
113+
```json
114+
{
115+
"Version": "2012-10-17",
116+
"Statement": [
117+
{
118+
"Sid": "VisualEditor0",
119+
"Effect": "Allow",
120+
"Action": [
121+
"ecr:GetRegistryPolicy",
122+
"ecr:PutImageTagMutability",
123+
"ecr:GetDownloadUrlForLayer",
124+
"ecr:DescribeRegistry",
125+
"ecr:GetAuthorizationToken",
126+
"ecr:ListTagsForResource",
127+
"ecr:UploadLayerPart",
128+
"ecr:ListImages",
129+
"ecr:PutImage",
130+
"ecr:UntagResource",
131+
"ecr:BatchGetImage",
132+
"ecr:CompleteLayerUpload",
133+
"ecr:DescribeImages",
134+
"ecr:TagResource",
135+
"ecr:DescribeRepositories",
136+
"ecr:InitiateLayerUpload",
137+
"ecr:BatchCheckLayerAvailability",
138+
"ecr:ReplicateImage",
139+
"ecr:GetRepositoryPolicy",
140+
"ecs:SubmitTaskStateChange",
141+
"ecs:UpdateContainerInstancesState",
142+
"ecs:RegisterContainerInstance",
143+
"ecs:DescribeTaskDefinition",
144+
"ecs:DescribeClusters",
145+
"ecs:ListServices",
146+
"ecs:UpdateService",
147+
"ecs:ListTasks",
148+
"ecs:ListTaskDefinitionFamilies",
149+
"ecs:RegisterTaskDefinition",
150+
"ecs:SubmitContainerStateChange",
151+
"ecs:StopTask",
152+
"ecs:DescribeServices",
153+
"ecs:ListContainerInstances",
154+
"ecs:DescribeContainerInstances",
155+
"ecs:DeregisterContainerInstance",
156+
"ecs:TagResource",
157+
"ecs:DescribeTasks",
158+
"ecs:UntagResource",
159+
"ecs:ListTaskDefinitions",
160+
"ecs:ListClusters"
161+
],
162+
"Resource": "*"
163+
}
164+
]
165+
}
166+
```
167+
> Further permission reduction can be done by reducing what resources can be accessed.
168+
> Application permissions can be controlled via `taskRoleArn`, see [AWS docs for details](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html).
169+
170+
## What's the process of the `release.yml`?
171+
172+
![](https://raw.githubusercontent.com/ServiceStack/docs/master/docs/images/mix/release-ecr-aws-diagram.png)

.github/workflows/build.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Build Server
2+
3+
on:
4+
pull_request: {}
5+
push:
6+
paths:
7+
- 'NextJsTemplateApi*'
8+
- 'Dockerfile'
9+
- 'NuGet.Config'
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-20.04
14+
steps:
15+
- name: checkout
16+
uses: actions/checkout@v2.0.0
17+
18+
- name: setup .net core
19+
uses: actions/setup-dotnet@v1.7.2
20+
with:
21+
dotnet-version: 5.0.100
22+
23+
- name: build
24+
run: dotnet build
25+
working-directory: .
26+
27+
- name: test
28+
run: |
29+
dotnet test
30+
if [ $? -eq 0 ]; then
31+
echo TESTS PASSED
32+
else
33+
echo TESTS FAILED
34+
exit 1
35+
fi
36+
working-directory: ./NextJsTemplateApi.Tests
37+

.github/workflows/release.yml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
name: Release Server
2+
on:
3+
release:
4+
types: [published]
5+
jobs:
6+
push_to_ecr:
7+
runs-on: ubuntu-20.04
8+
steps:
9+
- name: Checkout
10+
uses: actions/checkout@v2
11+
12+
- name: repository name fix
13+
run: echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
14+
15+
- name: Configure AWS credentials
16+
uses: aws-actions/configure-aws-credentials@v1
17+
with:
18+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
19+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
20+
aws-region: ${{ secrets.AWS_REGION }}
21+
22+
- name: Login to Amazon ECR
23+
id: login_ecr
24+
uses: aws-actions/amazon-ecr-login@v1
25+
26+
- name: Create ECR repo if not exists.
27+
env:
28+
ECR_REPOSITORY: ${{ env.image_repository_name }}
29+
run: aws ecr describe-repositories --repository-names ${ECR_REPOSITORY} || aws ecr create-repository --repository-name ${ECR_REPOSITORY}
30+
31+
- name: Build and push to ECR
32+
id: push_image_to_ecr
33+
uses: docker/build-push-action@v2.2.2
34+
with:
35+
file: Dockerfile
36+
context: .
37+
push: true
38+
tags: ${{ steps.login_ecr.outputs.registry }}/${{ env.image_repository_name }}:${{ github.event.release.tag_name }}
39+
40+
deploy_ecs:
41+
needs: push_to_ecr
42+
runs-on: ubuntu-20.04
43+
steps:
44+
- name: checkout
45+
uses: actions/checkout@v2
46+
47+
- name: Configure AWS credentials
48+
uses: aws-actions/configure-aws-credentials@v1
49+
with:
50+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
51+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
52+
aws-region: ${{ secrets.AWS_REGION }}
53+
54+
- name: Login to Amazon ECR
55+
id: login_ecr
56+
uses: aws-actions/amazon-ecr-login@v1
57+
58+
- name: Repository name fix and env values setup
59+
run: |
60+
echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
61+
echo "domain=${{ secrets.HOST_DOMAIN }}" >> $GITHUB_ENV
62+
echo "letsencrypt_email=${{ secrets.LETSENCRYPT_EMAIL }}" >> $GITHUB_ENV
63+
echo "app_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]' | tr -d . | cut -d'/' -f2)" >> $GITHUB_ENV
64+
echo "cluster_name=${{ secrets.AWS_ECS_CLUSTER }}" >> $GITHUB_ENV
65+
echo "image_url=${{ steps.login_ecr.outputs.registry }}/$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]'):${{ github.event.release.tag_name }}" >> $GITHUB_ENV
66+
echo "aws_region=${{ secrets.AWS_REGION }}" >> $GITHUB_ENV
67+
68+
- name: Populate task definition template
69+
uses: danielr1996/envsubst-action@1.0.0
70+
env:
71+
RELEASE_VERSION: ${{ github.event.release.tag_name }}
72+
APP_NAME: ${{ env.app_name }}
73+
IMAGE_URL: ${{ env.image_url }}
74+
HOST_DOMAIN: ${{ env.domain }}
75+
LETSENCRYPT_EMAIL: ${{ env.letsencrypt_email }}
76+
AWS_REGION: ${{ env.aws_region }}
77+
CLUSTER_NAME: ${{ env.cluster_name }}
78+
with:
79+
input: deploy/task-definition-template.json
80+
output: deploy/task-definition.json
81+
82+
- name: Create task definition if doesn't exist
83+
run: aws ecs describe-task-definition --task-definition ${{ env.app_name }} || aws ecs register-task-definition --cli-input-json file://deploy/task-definition.json
84+
85+
- name: Create ECS Service if not exists.
86+
run: aws ecs describe-services --cluster ${{ env.cluster_name }} --services ${{ env.app_name }} | jq '.services[0]' -e || aws ecs create-service --cluster ${{ env.cluster_name }} --service-name ${{ env.app_name }} --task-definition ${{ env.app_name }} --desired-count 1
87+
88+
- name: Deploy new revision of the task definition
89+
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
90+
with:
91+
task-definition: deploy/task-definition.json
92+
service: ${{ env.app_name }}
93+
cluster: ${{ env.cluster_name }}
94+
force-new-deployment: true

0 commit comments

Comments
 (0)