Skip to content

Commit

Permalink
chore:ci/cd, docker init
Browse files Browse the repository at this point in the history
  • Loading branch information
codernesty committed Aug 18, 2024
1 parent de91a6a commit f4f22e0
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 103 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Deploy to AWS App Runner

# Trigger this workflow on any push to the 'main' branch
on:
push:
branches:
- master

jobs:
# Job for deploying the backend service
deploy-backend:
name: Deploy Backend to AWS App Runner
runs-on: ubuntu-latest # Run on the latest version of Ubuntu

steps:
# Step 1: Checkout the code from the repository
- name: Checkout code
uses: actions/checkout@v3 # GitHub Action to checkout the code

# Step 2: Set up AWS CLI with necessary credentials
- name: Set up AWS CLI
uses: aws-actions/configure-aws-credentials@v2 # GitHub Action to configure AWS credentials
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} # AWS Access Key ID stored as a GitHub secret
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} # AWS Secret Access Key stored as a GitHub secret
aws-region: us-east-1 # AWS Region stored as a GitHub secret

# Step 3: Deploy the backend service to AWS App Runner
- name: Deploy Backend
run: |
# AWS CLI command to update the backend service in AWS App Runner
aws apprunner update-service \
--service-arn $(aws apprunner list-services --query "ServiceSummaryList[?ServiceName=='${{ secrets.APP_RUNNER_SERVICE_NAME_BACKEND }}'].ServiceArn | [0]" --output text) \
--source-configuration SourceCodeRepository={"RepositoryUrl": "${{ secrets.REPOSITORY_URL_BACKEND }}", "SourceCodeVersion": {"Type": "BRANCH", "Value": "master"}}
# Job for deploying the frontend service
deploy-frontend:
name: Deploy Frontend to AWS App Runner
runs-on: ubuntu-latest # Run on the latest version of Ubuntu

steps:
# Step 1: Checkout the code from the repository
- name: Checkout code
uses: actions/checkout@v3 # GitHub Action to checkout the code

# Step 2: Set up AWS CLI with necessary credentials
- name: Set up AWS CLI
uses: aws-actions/configure-aws-credentials@v2 # GitHub Action to configure AWS credentials
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} # AWS Access Key ID stored as a GitHub secret
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} # AWS Secret Access Key stored as a GitHub secret
aws-region: ${{ secrets.AWS_REGION }} # AWS Region stored as a GitHub secret

# Step 3: Deploy the frontend service to AWS App Runner
- name: Deploy Frontend
run: |
# AWS CLI command to update the frontend service in AWS App Runner
aws apprunner update-service \
--service-arn $(aws apprunner list-services --query "ServiceSummaryList[?ServiceName=='${{ secrets.APP_RUNNER_SERVICE_NAME_FRONTEND }}'].ServiceArn | [0]" --output text) \
--source-configuration SourceCodeRepository={"RepositoryUrl": "${{ secrets.REPOSITORY_URL_FRONTEND }}", "SourceCodeVersion": {"Type": "BRANCH", "Value": "master"}}
97 changes: 38 additions & 59 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: CI Checks (Linting and Tests)
# GitHub Actions Workflow to test a full-stack project (Node.js frontend and Python backend)

# Trigger workflow on push to master or pull requests targeting master
on:
push:
branches:
Expand All @@ -13,101 +14,79 @@ on:
- synchronize
- ready_for_review

# Handle concurrency to cancel in-progress runs if a new one starts
concurrency:
group: tests-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

env:
LINGUAPHOTO_ENVIRONMENT: local
JWT_SECRET: test
AWS_ACCESS_KEY_ID: test
AWS_SECRET_ACCESS_KEY: test
AWS_ENDPOINT_URL_DYNAMODB: http://localhost:8000
AWS_REGION: us-east-1

jobs:
backend-tests:
run-tests:
# Set job timeout to 10 minutes
timeout-minutes: 10
runs-on: ubuntu-latest

steps:
# Step 1: Check out the repository
- name: Check out repository
uses: actions/checkout@v3

# Step 2: Set up Node.js environment
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: "20.10.0"

# Step 3: Restore cache (Node modules and mypy cache)
- name: Restore cache
id: restore-cache
uses: actions/cache/restore@v3
with:
path: |
${{ env.pythonLocation }}
.mypy_cache/
key: backend-tests-${{ github.event.pull_request.base.sha || github.sha }}
frontend/node_modules/
key: tests-${{ github.sha }}-${{ hashFiles('frontend/package-lock.json') }}-${{ hashFiles('linguaphoto/requirements.txt') }}
restore-keys: |
backend-tests-
tests-${{ github.sha }}-
tests-
# Step 4: Install Node.js packages
- name: Install Node packages
working-directory: frontend
run: npm install

# Step 5: Build the frontend
- name: Build frontend
working-directory: frontend
run: npm run build

# Step 6: Set up Python environment
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11"

- name: Install Python package
# Step 7: Install Python packages (including dev dependencies)
- name: Install Python dependencies
run: |
pip install --upgrade --upgrade-strategy eager -e '.[dev]'
cd linguaphoto # Navigate to the linguaphoto directory
pip install -r requirements.txt # Install Python dependencies
# Step 8: Run static code checks (linters, type checkers, etc.)
- name: Run static checks
run: |
mkdir -p .mypy_cache
make static-checks
# Step 9: Run unit tests for the backend
- name: Run unit tests
run: |
make test-backend
run: make test-backend

# Step 10: Save cache (only on the master branch)
- name: Save cache
uses: actions/cache/save@v3
if: github.ref == 'refs/heads/master'
with:
path: |
${{ env.pythonLocation }}
.mypy_cache/
key: ${{ steps.restore-cache.outputs.cache-primary-key }}

frontend-tests:
timeout-minutes: 10
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: "20"

- name: Restore cache
id: restore-cache
uses: actions/cache/restore@v3
with:
path: |
node_modules/
key: frontend-tests-${{ github.event.pull_request.base.sha || github.sha }}
restore-keys: |
frontend-tests-
- name: Install Node package
working-directory: frontend
run: |
npm install
- name: Run tests
run: |
make test-frontend
- name: Save cache
uses: actions/cache/save@v3
if: github.ref == 'refs/heads/master'
with:
path: |
node_modules/
key: ${{ steps.restore-cache.outputs.cache-primary-key }}
frontend/node_modules/
key: tests-${{ github.sha }}-${{ hashFiles('frontend/package-lock.json') }}-${{ hashFiles('linguaphoto/requirements.txt') }}
42 changes: 42 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@


services:
frontend:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- "80:80"
depends_on:
- backend

backend:
build:
context: ./linguaphoto
dockerfile: Dockerfile
ports:
- "8000:8000"
environment:
DEBUG: "true"
AWS_REGION: " us-east-1" # Environment variables for DynamoDB
AWS_ACCESS_KEY_ID: "your-access-key-id"
AWS_SECRET_ACCESS_KEY: "your-secret-access-key"
DYNAMODB_ENDPOINT: "http://dynamodb:8002" # Local DynamoDB endpoint
REDIS_URL: "redis://redis:6379" # Redis connection URL

dynamodb:
image: amazon/dynamodb-local
ports:
- "8002:8000"

dynamodb-admin:
image: aaronshaf/dynamodb-admin
ports:
- "8001:8001"
environment:
- DYNAMO_ENDPOINT=http://dynamodb:8000

redis:
image: redis
ports:
- "6379:6379"
12 changes: 7 additions & 5 deletions frontend/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# .dockerignore

# Only add the build directory to Docker.
*
!build/
node_modules
build
dist
.git
Dockerfile
README.md
.env
29 changes: 29 additions & 0 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Use an official Node.js runtime as a parent image
FROM node:20.10.0-alpine AS build

# Set the working directory in the container
WORKDIR /src

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Build the React app
RUN npm run build

# Use a lightweight web server to serve the React app
FROM nginx:1.25-alpine

# Copy the build output to Nginx's html directory
COPY --from=build /src/build /usr/share/nginx/html

# Expose port 80 to the outside world
EXPOSE 80

# Start Nginx when the container launches
CMD ["nginx", "-g", "daemon off;"]
44 changes: 28 additions & 16 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f4f22e0

Please sign in to comment.