Skip to content

Commit

Permalink
feat: update dev cicd
Browse files Browse the repository at this point in the history
  • Loading branch information
ZZ3n committed Sep 12, 2024
1 parent b01a767 commit b5e4e95
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 16 deletions.
65 changes: 50 additions & 15 deletions .github/workflows/dev-deploy.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,61 @@
name: Dockerizing to Amazon ECR
# 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:
- main
- dev
workflow_dispatch:

env:
AWS_REGION: ap-northeast-2 # set this to your preferred AWS region, e.g. us-west-1
ECR_REPOSITORY: frontend-web # set this to your Amazon ECR repository name
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

# ECS_SERVICE: MY_ECS_SERVICE # set this to your Amazon ECS service name
# ECS_CLUSTER: MY_ECS_CLUSTER # set this to your Amazon ECS cluster name
# ECS_TASK_DEFINITION: MY_ECS_TASK_DEFINITION # set this to the path to your Amazon ECS task definition
# # file, e.g. .aws/task-definition.json
# CONTAINER_NAME: MY_CONTAINER_NAME # set this to the name of the container in the
# # containerDefinitions section of your task definition
permissions:
contents: read

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

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

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
Expand All @@ -46,15 +77,19 @@ jobs:
# 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 .
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 "::set-output name=image::$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: ${{ env.ECS_TASK_DEFINITION }}
task-definition-arn: ${{ env.ECS_TASK_DEFINITION_ARN }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.build-image.outputs.image }}

Expand Down
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ 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
Expand Down
2 changes: 1 addition & 1 deletion scripts/push-to-ecr.sh → scripts/push-to-ecr-dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ else
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 ..
docker build --platform linux/arm64 -t chunsik/dev/fe --target dev ..
if [ $? -eq 0 ]; then
echo -e "${GREEN}Docker image built successfully.${NC}"
else
Expand Down
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 b5e4e95

Please sign in to comment.