Skip to content

Commit 24bc69d

Browse files
authored
Merge branch 'development' into unitTestingforQuestions
2 parents 534e1dc + 57c794b commit 24bc69d

File tree

9 files changed

+892
-338
lines changed

9 files changed

+892
-338
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
---
2+
title: AWS Frontend Deploy Workflow
3+
---
4+
5+
# AWS Frontend Deploy Workflow
6+
7+
This document describes the purpose and structure of the GitHub Actions workflow defined in `.github/workflows/aws-frontend-deploy.yml`.
8+
9+
## Overview
10+
11+
This workflow automates the process of building, pushing, and deploying the frontend application to AWS. It is triggered manually via the GitHub Actions UI using `workflow_dispatch`:
12+
13+
```yaml
14+
on:
15+
workflow_dispatch: # Manual trigger from GitHub Actions UI
16+
inputs:
17+
env:
18+
type: choice
19+
description: "AWS Incubator Env"
20+
options: # Selectable environment options
21+
- dev
22+
- prod
23+
ref:
24+
description: "Branch, Tag, or SHA" # Code reference to deploy
25+
required: true
26+
```
27+
28+
Users can select the environment (`dev` or `prod`) and specify a branch, tag, or SHA to deploy.
29+
30+
## Environment Variables
31+
32+
The workflow sets several environment variables for use throughout the jobs:
33+
34+
```yaml
35+
env:
36+
AWS_SHARED_CLUSTER: incubator-prod # Target ECS cluster name
37+
AWS_APP_NAME: vrms-frontend # Application name for tagging and service
38+
AWS_REGION: us-west-2 # AWS region for deployment
39+
DOCKERFILE: Dockerfile.prod # Dockerfile used for build
40+
DOCKER_PATH: client # Path to frontend source and Dockerfile
41+
```
42+
43+
Each of these environment variables is set at the top level of the workflow and is available to all jobs and steps. Here is a description of each:
44+
45+
- `AWS_SHARED_CLUSTER`: The name of the AWS ECS cluster to which the frontend will be deployed. In this workflow, it is set to `incubator-prod`. _Might be sourced from your AWS infrastructure naming conventions or deployment environment._
46+
- `AWS_APP_NAME`: The application name used for tagging Docker images and identifying the service in AWS. Here, it is set to `vrms-frontend`. _Might be sourced from your project or repository name._
47+
- `AWS_REGION`: The AWS region where resources are deployed. Set to `us-west-2` (Oregon). _Might be sourced from your AWS account's preferred deployment region._
48+
- `DOCKERFILE`: The Dockerfile used for building the frontend image. Set to `Dockerfile.prod`, indicating a production-ready build. _Might be sourced from your repository's Docker configuration._
49+
- `DOCKER_PATH`: The path to the directory containing the Dockerfile and frontend source code. Set to `client`. _Might be sourced from your repository structure._
50+
51+
## Jobs
52+
53+
### 1. `setup_env`
54+
55+
This job checks out the code and sets up environment-specific variables for the deployment:
56+
57+
```yaml
58+
jobs:
59+
setup_env:
60+
name: Set-up environment
61+
runs-on: ubuntu-latest
62+
steps:
63+
- name: Debug Action
64+
uses: hmarr/debug-action@v2 # Prints debug info to logs
65+
- name: Checkout
66+
uses: actions/checkout@v3 # Checks out code at specified ref
67+
with:
68+
ref: ${{ github.event.inputs.ref }} # Uses user-specified ref
69+
- name: Set AWS Env & Image Tag per workflow
70+
# Get short SHA of current commit
71+
# if -- action is triggered manually
72+
# Get environment input from workflow dispatch
73+
# Get ref input from workflow dispatch
74+
# Set AWS_APPENV for later steps
75+
# Set IMAGE_TAG for later steps
76+
# fi
77+
run: |
78+
SHORT_SHA=$(git rev-parse --short HEAD)
79+
if [[ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]]; then
80+
INPUT_ENV=${{ github.event.inputs.env }}
81+
INPUT_REF=${{ github.event.inputs.ref }}
82+
echo AWS_APPENV="$AWS_APP_NAME"-$INPUT_ENV >> $GITHUB_ENV
83+
echo IMAGE_TAG=$SHORT_SHA >> $GITHUB_ENV
84+
fi
85+
```
86+
87+
This job outputs the application environment and image tag for use in subsequent jobs.
88+
89+
### 2. `build`
90+
91+
This job builds the Docker image for the frontend and pushes it to Amazon ECR:
92+
93+
```yaml
94+
build:
95+
name: Build & Push Docker Image
96+
runs-on: ubuntu-latest
97+
permissions:
98+
id-token: write # Needed for OIDC authentication to AWS
99+
needs: [setup_env] # Waits for environment setup
100+
steps:
101+
- name: Checkout
102+
uses: actions/checkout@v3 # Checks out code at specified ref
103+
with:
104+
ref: ${{ github.event.inputs.ref }}
105+
- name: Setup Node.js
106+
uses: actions/setup-node@v3 # Sets up Node.js for build
107+
with:
108+
node-version: 18 # Uses Node.js v18
109+
cache: "npm" # Enables npm caching
110+
- name: Configure AWS credentials
111+
uses: aws-actions/configure-aws-credentials@v3 # Sets AWS credentials for CLI
112+
with:
113+
role-to-assume: arn:aws:iam::035866691871:role/incubator-cicd-vrms # IAM role for deploy
114+
role-session-name: incubator-cicd-vrms-gha # Session name for audit
115+
aws-region: us-west-2 # AWS region
116+
- name: Login to Amazon ECR
117+
id: login-ecr
118+
uses: aws-actions/amazon-ecr-login@v1 # Authenticates Docker to ECR
119+
- name: Build, tag, and push the image to Amazon ECR
120+
id: build-push-image
121+
env:
122+
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} # ECR registry URL
123+
ECR_REPOSITORY: ${{ env.AWS_APP_NAME }} # ECR repo name
124+
run: |
125+
ls # List files for debug
126+
cd ./${{ env.DOCKER_PATH }} # Enter frontend directory
127+
docker build \
128+
-f ${{ env.DOCKERFILE }} \ # Use production Dockerfile
129+
-t ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ needs.setup_env.outputs.IMAGE_TAG }} \ # Tag with image SHA
130+
-t ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ github.event.inputs.env }} \ # Tag with environment
131+
.
132+
docker image push --all-tags ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }} # Push all tags
133+
```
134+
135+
### 3. `deploy`
136+
137+
This job deploys the new Docker image to AWS ECS by forcing a new deployment of the ECS service:
138+
139+
```yaml
140+
deploy:
141+
name: Deploy to AWS ECS
142+
runs-on: ubuntu-latest
143+
needs: [setup_env, build] # Waits for setup and build jobs
144+
permissions:
145+
id-token: write # Needed for OIDC authentication to AWS
146+
steps:
147+
- name: Configure AWS credentials
148+
uses: aws-actions/configure-aws-credentials@v3 # Sets AWS credentials for CLI
149+
with:
150+
role-to-assume: arn:aws:iam::035866691871:role/incubator-cicd-vrms # IAM role for deploy
151+
role-session-name: incubator-cicd-vrms-gha # Session name for audit
152+
aws-region: us-west-2 # AWS region
153+
- name: Restart ECS Service
154+
id: redeploy-service
155+
env:
156+
SERVICE_NAME: ${{env.AWS_APP_NAME}}-${{ github.event.inputs.env }} # ECS service name
157+
run: |
158+
aws ecs update-service --force-new-deployment --service $SERVICE_NAME --cluster $AWS_SHARED_CLUSTER # Triggers ECS redeploy
159+
```
160+
161+
## Repository Checkout and Working Directory
162+
163+
When this workflow runs, it uses the `actions/checkout@v3` action to clone the entire repository. The initial working directory for all steps is the root of the repository.
164+
165+
Before building the Docker image, the workflow explicitly changes into the `client` directory using:
166+
167+
```bash
168+
cd ./${{ env.DOCKER_PATH }}
169+
```
170+
171+
This means that for the Docker build step, the working directory is `client/`, and the Dockerfile path `Dockerfile.prod` refers to `client/Dockerfile.prod`.
172+
173+
**Summary:**
174+
175+
- The workflow clones the entire repository.
176+
- The working directory starts at the repo root.
177+
- The workflow changes into the `client` directory before building the Docker image.
178+
- The Docker build context and Dockerfile are both relative to the `client` directory.
179+
180+
## Summary
181+
182+
This workflow provides a manual, environment-aware deployment pipeline for the frontend application, leveraging Docker, Amazon ECR, and ECS. It ensures that only the specified code reference is built and deployed, and that deployments are traceable and auditable via GitHub Actions.
Lines changed: 92 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,118 @@
11
name: Frontend Build and Deploy
22
on:
3-
workflow_dispatch:
3+
workflow_dispatch: # Manual trigger from GitHub Actions UI
44
inputs:
55
env:
66
type: choice
7-
description: 'AWS Incubator Env'
8-
options:
9-
- dev
10-
- prod
7+
description: "AWS Incubator Env"
8+
options: # Selectable environment options
9+
- dev
10+
- prod
1111
ref:
12-
description: 'Branch, Tag, or SHA'
12+
description: "Branch, Tag, or SHA" # Code reference to deploy
1313
required: true
1414
env:
15+
# Target ECS cluster name
1516
AWS_SHARED_CLUSTER: incubator-prod
16-
AWS_APP_NAME: vrms-client
17+
# Application name for tagging and service
18+
AWS_APP_NAME: vrms-frontend
19+
# AWS region for deployment
1720
AWS_REGION: us-west-2
18-
DOCKERFILE: client/Dockerfile.prod
21+
# Dockerfile used for build (located in client/)
22+
DOCKERFILE: Dockerfile.prod
23+
# Path to frontend source and Dockerfile
1924
DOCKER_PATH: client
2025
jobs:
2126
setup_env:
22-
name: Set-up environment
27+
name: Set-up environment
2328
runs-on: ubuntu-latest
2429
steps:
25-
- name: Debug Action
26-
uses: hmarr/debug-action@v2
27-
- name: Checkout
28-
uses: actions/checkout@v3
29-
with:
30-
ref: ${{ github.event.inputs.ref }}
31-
- name: Set AWS Env & Image Tag per workflow
32-
run: |
33-
SHORT_SHA=$(git rev-parse --short HEAD)
34-
if [[ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]]; then
35-
INPUT_ENV=${{ github.event.inputs.env }}; INPUT_REF=${{ github.event.inputs.ref }}
36-
echo AWS_APPENV="$AWS_APP_NAME"-$INPUT_ENV >> $GITHUB_ENV
37-
echo IMAGE_TAG=$SHORT_SHA >> $GITHUB_ENV
38-
fi
30+
- name: Debug Action
31+
uses: hmarr/debug-action@v2 # Prints debug info to logs
32+
- name: Checkout
33+
uses: actions/checkout@v3 # Checks out code at specified ref
34+
with:
35+
ref: ${{ github.event.inputs.ref }} # Uses user-specified ref
36+
# Get short SHA of current commit
37+
# Only run if triggered manually
38+
# Get environment input from workflow dispatch
39+
# Get ref input from workflow dispatch
40+
# Set AWS_APPENV for later steps
41+
# Set IMAGE_TAG for later steps
42+
- name: Set AWS Env & Image Tag per workflow
43+
run: |
44+
SHORT_SHA=$(git rev-parse --short HEAD)
45+
if [[ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]]; then
46+
INPUT_ENV=${{ github.event.inputs.env }}; INPUT_REF=${{ github.event.inputs.ref }}
47+
echo AWS_APPENV="$AWS_APP_NAME"-$INPUT_ENV >> $GITHUB_ENV
48+
echo IMAGE_TAG=$SHORT_SHA >> $GITHUB_ENV
49+
fi
3950
outputs:
4051
AWS_APPENV: ${{ env.AWS_APPENV }}
4152
IMAGE_TAG: ${{ env.IMAGE_TAG }}
4253
build:
4354
name: Build & Push Docker Image
4455
runs-on: ubuntu-latest
45-
needs: [setup_env]
56+
permissions:
57+
id-token: write # Needed for OIDC authentication to AWS
58+
needs: [setup_env] # Waits for environment setup
4659
steps:
47-
- name: Checkout
48-
uses: actions/checkout@v3
49-
with:
50-
ref: ${{ github.event.inputs.ref }}
51-
- name: Checkout
52-
uses: actions/setup-node@v3
53-
with:
54-
node-version: 18
55-
cache: 'npm'
56-
- name: Configure AWS credentials
57-
uses: aws-actions/configure-aws-credentials@v2
58-
with:
59-
aws-access-key-id: ${{ secrets.INCUBATOR_AWS_ACCESS_KEY_ID }}
60-
aws-secret-access-key: ${{ secrets.INCUBATOR_AWS_SECRET_ACCESS_KEY }}
61-
aws-region: ${{ env.AWS_REGION }}
62-
- name: Login to Amazon ECR
63-
id: login-ecr
64-
uses: aws-actions/amazon-ecr-login@v1
65-
- name: Init Docker Cache
66-
uses: jpribyl/[email protected]
67-
with:
68-
key: ${{ github.workflow }}-2-{hash}
69-
restore-keys: |
70-
${{ github.workflow }}-2-
71-
- name: Build & Push Image to ECR
72-
uses: kciter/aws-ecr-action@v3
73-
with:
74-
access_key_id: ${{ secrets.INCUBATOR_AWS_ACCESS_KEY_ID }}
75-
secret_access_key: ${{ secrets.INCUBATOR_AWS_SECRET_ACCESS_KEY }}
76-
account_id: ${{ secrets.INCUBATOR_AWS_ACCOUNT_ID }}
77-
repo: ${{ needs.setup_env.outputs.AWS_APPENV }}
78-
region: ${{ env.AWS_REGION }}
79-
tags: latest,${{ needs.setup_env.outputs.IMAGE_TAG }}
80-
dockerfile: ${{ env.DOCKERFILE }}
81-
path: ${{ env.DOCKER_PATH }}
60+
- name: Checkout
61+
uses: actions/checkout@v3 # Checks out code at specified ref
62+
with:
63+
ref: ${{ github.event.inputs.ref }}
64+
- name: Setup Node.js
65+
uses: actions/setup-node@v3 # Sets up Node.js for build
66+
with:
67+
node-version: 18 # Uses Node.js v18
68+
cache: "npm" # Enables npm caching
69+
- name: Configure AWS credentials
70+
uses: aws-actions/configure-aws-credentials@v3 # Sets AWS credentials for CLI
71+
with:
72+
role-to-assume: arn:aws:iam::035866691871:role/incubator-cicd-vrms # IAM role for deploy
73+
role-session-name: incubator-cicd-vrms-gha # Session name for audit
74+
aws-region: us-west-2 # AWS region
75+
- name: Login to Amazon ECR
76+
id: login-ecr
77+
uses: aws-actions/amazon-ecr-login@v1 # Authenticates Docker to ECR
78+
- name: Build, tag, and push the image to Amazon ECR
79+
id: build-push-image
80+
env:
81+
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} # ECR registry URL
82+
ECR_REPOSITORY: ${{ env.AWS_APP_NAME }} # ECR repo name
83+
# List files for debug
84+
# Enter frontend directory for Docker build context
85+
# Build Docker image using production Dockerfile
86+
# Tag image with short SHA
87+
# Tag image with environment (dev/prod)
88+
# Use current directory as build context
89+
# Push all tags for this image to ECR
90+
run: |
91+
ls
92+
cd ./${{ env.DOCKER_PATH }}
93+
docker build \
94+
-f ${{ env.DOCKERFILE }} \
95+
-t ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ needs.setup_env.outputs.IMAGE_TAG }} \
96+
-t ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ github.event.inputs.env }} \
97+
.
98+
docker image push --all-tags ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}
8299
deploy:
83100
name: Deploy to AWS ECS
84101
runs-on: ubuntu-latest
85-
needs: [setup_env, build]
102+
needs: [setup_env, build] # Waits for setup and build jobs
103+
permissions:
104+
id-token: write # Needed for OIDC authentication to AWS
86105
steps:
87-
- name: Configure AWS credentials
88-
uses: aws-actions/configure-aws-credentials@v2
89-
with:
90-
aws-access-key-id: ${{ secrets.INCUBATOR_AWS_ACCESS_KEY_ID }}
91-
aws-secret-access-key: ${{ secrets.INCUBATOR_AWS_SECRET_ACCESS_KEY }}
92-
aws-region: ${{ env.AWS_REGION }}
93-
- name: Login to Amazon ECR
94-
id: login-ecr
95-
uses: aws-actions/amazon-ecr-login@v1
96-
- name: Pull Task Definition & write to file
97-
id: aws-task-definition
98-
run: |
99-
aws ecs describe-task-definition \
100-
--task-definition ${{ needs.setup_env.outputs.AWS_APPENV }} \
101-
--query taskDefinition | \
102-
jq 'del(.taskDefinitionArn,.revision,.status,.registeredBy,.registeredAt,.compatibilities,.requiresAttributes)' > task-def.json
103-
- name: Interpolate new Docker Image into Task Definition
104-
id: task-definition
105-
uses: aws-actions/amazon-ecs-render-task-definition@v1
106-
with:
107-
task-definition: task-def.json
108-
container-name: ${{ needs.setup_env.outputs.AWS_APPENV }}
109-
image: ${{ steps.login-ecr.outputs.registry }}/${{ needs.setup_env.outputs.AWS_APPENV }}:${{ needs.setup_env.outputs.IMAGE_TAG }}
110-
- name: Deploy Amazon ECS
111-
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
112-
with:
113-
task-definition: ${{ steps.task-definition.outputs.task-definition }}
114-
service: ${{ needs.setup_env.outputs.AWS_APPENV }}
115-
cluster: ${{ env.AWS_SHARED_CLUSTER }}
116-
wait-for-service-stability: true
117-
wait-for-minutes: 5 minutes
118-
106+
- name: Configure AWS credentials
107+
uses: aws-actions/configure-aws-credentials@v3 # Sets AWS credentials for CLI
108+
with:
109+
role-to-assume: arn:aws:iam::035866691871:role/incubator-cicd-vrms # IAM role for deploy
110+
role-session-name: incubator-cicd-vrms-gha # Session name for audit
111+
aws-region: us-west-2 # AWS region
112+
- name: Restart ECS Service
113+
id: redeploy-service
114+
env:
115+
SERVICE_NAME: ${{env.AWS_APP_NAME}}-${{ github.event.inputs.env }} # ECS service name
116+
# Force a new deployment of the ECS service to use the latest Docker image
117+
run: |
118+
aws ecs update-service --force-new-deployment --service $SERVICE_NAME --cluster $AWS_SHARED_CLUSTER

0 commit comments

Comments
 (0)