From 8f09fd4fc93b02fceeb2bc9973417fddff49986b Mon Sep 17 00:00:00 2001 From: GwangjoGong Date: Thu, 30 Mar 2023 20:36:09 +0900 Subject: [PATCH] feat: ci & cd --- .dockerignore | 3 ++ .github/workflows/cd.yml | 64 ++++++++++++++++++++++++++++++++++++++++ .github/workflows/ci.yml | 26 ++++++++++++++++ DockerFile | 25 +++++++++++----- index.yaml | 64 ++++++++++++++++++++++++++++++++++++++++ nginx.conf | 5 ++-- package.json | 2 +- 7 files changed, 179 insertions(+), 10 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/cd.yml create mode 100644 .github/workflows/ci.yml create mode 100644 index.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..0353749 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +node_modules +dist +.github \ No newline at end of file diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000..e69593c --- /dev/null +++ b/.github/workflows/cd.yml @@ -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 - diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..f71b708 --- /dev/null +++ b/.github/workflows/ci.yml @@ -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 diff --git a/DockerFile b/DockerFile index da0547d..808478b 100644 --- a/DockerFile +++ b/DockerFile @@ -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 \ No newline at end of file +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;"] \ No newline at end of file diff --git a/index.yaml b/index.yaml new file mode 100644 index 0000000..0e46588 --- /dev/null +++ b/index.yaml @@ -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 \ No newline at end of file diff --git a/nginx.conf b/nginx.conf index 2d72658..c0daadb 100644 --- a/nginx.conf +++ b/nginx.conf @@ -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; } } \ No newline at end of file diff --git a/package.json b/package.json index 6b5c5d7..083a4ba 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "uhdre-gamdi", "private": true, - "version": "0.0.0", + "version": "0.0.1", "type": "module", "scripts": { "dev": "vite",