Skip to content

Commit

Permalink
feat: ci & cd
Browse files Browse the repository at this point in the history
  • Loading branch information
GwangjoGong committed Mar 30, 2023
1 parent 7b65acc commit 8f09fd4
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
.github
64 changes: 64 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: CD

on:
push:
branches:
- main

env:
ECR_REGISTRY: 078512149071.dkr.ecr.ap-northeast-2.amazonaws.com
ECR_REPOSITORY: uhdre-gamdi-web
EKS_CLUSTER_NAME: goormthon-cluster
AWS_REGION: ap-northeast-2
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
VITE_API_URL: ${{ secrets.VITE_API_URL }}

jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- 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: Checkout source code
uses: actions/checkout@v2

- name: get-npm-version
id: package-version
uses: martinbeentjes/npm-get-version-action@main

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build Docker image
id: build-image
run: |
docker build \
--build-arg VITE_API_URL=${{ env.VITE_API_URL }} \
-t ${{ env.ECR_REPOSITORY }}:${{ steps.package-version.outputs.current-version}} \
.
- name: Tag Docker iamge
id: tag-image
run: docker tag ${{ env.ECR_REPOSITORY }}:${{ steps.package-version.outputs.current-version}} ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ steps.package-version.outputs.current-version}}

- name: Push Docker image to Amazon ECR
id: push-image
run: docker push ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ steps.package-version.outputs.current-version}}

- name: Configure EKS
id: configure-eks
run: aws eks update-kubeconfig --region ${{ env.AWS_REGION }} --name ${{ env.EKS_CLUSTER_NAME }}

- name: Apply yaml
id: apply-yaml
env:
ECR_REGISTRY: 078512149071.dkr.ecr.ap-northeast-2.amazonaws.com
ECR_REPOSITORY: uhdre-gamdi-web
TAG: ${{ steps.package-version.outputs.current-version}}
run: envsubst < index.yaml | kubectl apply -f -
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI

on:
push:
branches: ['main']
pull_request:
branches: ['main']

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: '16.x'

- name: Install Deps
run: npm install

- name: Lint
run: npm run build
25 changes: 18 additions & 7 deletions DockerFile
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
FROM nginx
FROM node:alpine as build

RUN mkdir /app
ARG VITE_API_URL=${VITE_API_URL}
ENV VITE_API_URL=${VITE_API_URL}

WORKDIR /app

RUN mkdir ./build
COPY ./package.json ./

ADD ./dist ./build
RUN npm install

RUN rm /etc/nginx/conf.d/default.conf
COPY . .

COPY ./nginx.conf /etc/nginx/conf.d
RUN export VITE_API_URL=${VITE_API_URL} && npm run build

EXPOSE 80
FROM nginx:alpine

RUN rm -rf /usr/share/nginx/html/*

COPY --from=build /app/dist /usr/share/nginx/html

COPY ./nginx.conf /etc/nginx/conf.d/default.conf

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
64 changes: 64 additions & 0 deletions index.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
apiVersion: v1
kind: Namespace
metadata:
name: uhdre-gamdi-web
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: alb
namespace: uhdre-gamdi-web # 각자 서비스의 ns로 변경
annotations:
alb.ingress.kubernetes.io/scheme: internet-facing
alb.ingress.kubernetes.io/target-type: ip
spec:
ingressClassName: alb
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web-svc-alb
port:
number: 80
---
apiVersion: v1
kind: Service
metadata:
name: web-svc-alb
namespace: uhdre-gamdi-web # 각자 서비스의 ns로 변경
labels:
app: web
spec:
type: NodePort
selector:
app: web
ports:
- protocol: TCP
port: 80
targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deploy-alb
namespace: uhdre-gamdi-web # 각자 서비스의 ns로 변경
labels:
app: web
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: ${ECR_REGISTRY}/${ECR_REPOSITORY}:${TAG}
ports:
- containerPort: 80
5 changes: 3 additions & 2 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index index.html;
location / {
root /app/build;
index index.html;
try_files $uri $uri/ /index.html;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "uhdre-gamdi",
"private": true,
"version": "0.0.0",
"version": "0.0.1",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down

0 comments on commit 8f09fd4

Please sign in to comment.