Skip to content

Commit

Permalink
Merge branch '100-hours-a-week:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
moolmin authored Sep 13, 2024
2 parents 694dea6 + 90e7573 commit 5073f7a
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 0 deletions.
102 changes: 102 additions & 0 deletions .github/workflows/dev-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# This workflow will build and push a new container image to Amazon ECR,
# and then will deploy a new task definition to Amazon ECS, when there is a push to the "main" branch.
#
# To use this workflow, you will need to complete the following set-up steps:
#
# 1. Create an ECR repository to store your images.
# For example: `aws ecr create-repository --repository-name my-ecr-repo --region us-east-2`.
# Replace the value of the `ECR_REPOSITORY` environment variable in the workflow below with your repository's name.
# Replace the value of the `AWS_REGION` environment variable in the workflow below with your repository's region.
#
# 2. Create an ECS task definition, an ECS cluster, and an ECS service.
# For example, follow the Getting Started guide on the ECS console:
# https://us-east-2.console.aws.amazon.com/ecs/home?region=us-east-2#/firstRun
# Replace the value of the `ECS_SERVICE` environment variable in the workflow below with the name you set for the Amazon ECS service.
# Replace the value of the `ECS_CLUSTER` environment variable in the workflow below with the name you set for the cluster.
#
# 3. Store your ECS task definition as a JSON file in your repository.
# The format should follow the output of `aws ecs register-task-definition --generate-cli-skeleton`.
# Replace the value of the `ECS_TASK_DEFINITION` environment variable in the workflow below with the path to the JSON file.
# Replace the value of the `CONTAINER_NAME` environment variable in the workflow below with the name of the container
# in the `containerDefinitions` section of the task definition.
#
# 4. Store an IAM user access key in GitHub Actions secrets named `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`.
# See the documentation for each action used below for the recommended IAM policies for this IAM user,
# and best practices on handling the access key credentials.

name: Deploy to Amazon ECS

on:
push:
branches:
- dev
workflow_dispatch:

env:
AWS_REGION: ap-northeast-2 # set this to your preferred AWS region, e.g. us-west-1
ECR_REPOSITORY: chunsik/dev/fe # set this to your Amazon ECR repository name
ECS_SERVICE: chunsik-dev-fe-service # set this to your Amazon ECS service name
ECS_CLUSTER: ChunsikDevCluster # set this to your Amazon ECS cluster name

# 원본 Amazon ECS Task ARN
ECS_TASK_DEFINITION_ARN: arn:aws:ecs:ap-northeast-2:202533511551:task-definition/chunsik-dev-fe-task:3

# set this to the name of the container in the containerDefinitions section of your task definition
CONTAINER_NAME: dev-fe-container

permissions:
contents: read

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
environment: dev

steps:
- name: Checkout
uses: actions/checkout@v4

- 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: ${{ env.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 }}
IMAGE_TAG: ${{ github.sha }}
run: |
# Build a docker container and
# push it to ECR so that it can
# be deployed to ECS.
docker build \
-t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG \
--build-arg NEXT_PUBLIC_API_URL=${{ vars.NEXT_PUBLIC_API_URL_PROD }} \
--build-arg NEXT_PUBLIC_GA_ID=${{ vars.NEXT_PUBLIC_GA_ID_PROD }} \
.
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition-arn: ${{ env.ECS_TASK_DEFINITION_ARN }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.build-image.outputs.image }}

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true
49 changes: 49 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
FROM node:18 AS pre
WORKDIR /app
RUN apt-get update && \
apt-get install -y python3 make g++ build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev && \
apt-get clean

# 1. Install dependencies only when needed
FROM pre AS deps
WORKDIR /app
RUN npm install -g node-gyp
COPY package.json yarn.lock* ./
RUN yarn install --frozen-lockfile

# 2. Rebuild the source code only when needed
FROM pre AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN yarn build

# 3. Production image, copy all the files you need to run the app
FROM node:18-alpine AS runner
WORKDIR /app

# Install curl for health check
RUN apk update && apk add --no-cache curl

ENV NODE_ENV production

# Build arguments for secrets
ARG NEXT_PUBLIC_API_URL
ARG NEXT_PUBLIC_GA_ID

# Set environment variables using the build arguments
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_GA_ID=$NEXT_PUBLIC_GA_ID

# Copy built assets from builder stage
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json

# Expose port
EXPOSE 3000

# Start the Next.js app
CMD ["yarn", "start"]
46 changes: 46 additions & 0 deletions scripts/push-to-ecr-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

# 색상 코드 정의
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # 색상 초기화

ECR_URL=202533511551.dkr.ecr.ap-northeast-2.amazonaws.com

echo "Step 1: Logging into ECR at $ECR_URL"
aws ecr get-login-password --region ap-northeast-2 --profile default | docker login --username AWS --password-stdin $ECR_URL
if [ $? -eq 0 ]; then
echo -e "${GREEN}Successfully logged into ECR.${NC}"
else
echo -e "${RED}Failed to log into ECR.${NC}" >&2
exit 1
fi

echo "Step 2: Building Docker image for platform linux/arm64 with tag chunsik/dev/fe"
docker build --platform linux/arm64 -t chunsik/dev/fe --target dev ..
if [ $? -eq 0 ]; then
echo -e "${GREEN}Docker image built successfully.${NC}"
else
echo -e "${RED}Failed to build Docker image.${NC}" >&2
exit 1
fi

echo "Step 3: Tagging Docker image with ECR URL $ECR_URL"
docker tag chunsik/dev/fe:latest $ECR_URL/chunsik/dev/fe:latest
if [ $? -eq 0 ]; then
echo -e "${GREEN}Docker image tagged successfully.${NC}"
else
echo -e "${RED}Failed to tag Docker image.${NC}" >&2
exit 1
fi

echo "Step 4: Pushing Docker image to ECR repository $ECR_URL"
docker push $ECR_URL/chunsik/dev/fe:latest
if [ $? -eq 0 ]; then
echo -e "${GREEN}Docker image pushed to ECR successfully.${NC}"
else
echo -e "${RED}Failed to push Docker image to ECR.${NC}" >&2
exit 1
fi

echo -e "${GREEN}All steps completed successfully.${NC}"
46 changes: 46 additions & 0 deletions scripts/push-to-ecr-prod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

# 색상 코드 정의
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # 색상 초기화

ECR_URL=202533511551.dkr.ecr.ap-northeast-2.amazonaws.com

echo "Step 1: Logging into ECR at $ECR_URL"
aws ecr get-login-password --region ap-northeast-2 --profile default | docker login --username AWS --password-stdin $ECR_URL
if [ $? -eq 0 ]; then
echo -e "${GREEN}Successfully logged into ECR.${NC}"
else
echo -e "${RED}Failed to log into ECR.${NC}" >&2
exit 1
fi

echo "Step 2: Building Docker image for platform linux/arm64 with tag chunsik/prod/fe"
docker build --platform linux/arm64 -t chunsik/prod/fe ..
if [ $? -eq 0 ]; then
echo -e "${GREEN}Docker image built successfully.${NC}"
else
echo -e "${RED}Failed to build Docker image.${NC}" >&2
exit 1
fi

echo "Step 3: Tagging Docker image with ECR URL $ECR_URL"
docker tag chunsik/prod/fe:latest $ECR_URL/chunsik/prod/fe:latest
if [ $? -eq 0 ]; then
echo -e "${GREEN}Docker image tagged successfully.${NC}"
else
echo -e "${RED}Failed to tag Docker image.${NC}" >&2
exit 1
fi

echo "Step 4: Pushing Docker image to ECR repository $ECR_URL"
docker push $ECR_URL/chunsik/prod/fe:latest
if [ $? -eq 0 ]; then
echo -e "${GREEN}Docker image pushed to ECR successfully.${NC}"
else
echo -e "${RED}Failed to push Docker image to ECR.${NC}" >&2
exit 1
fi

echo -e "${GREEN}All steps completed successfully.${NC}"

0 comments on commit 5073f7a

Please sign in to comment.