Skip to content

Commit

Permalink
feat: 배포자동화 Github Actions 워크플로우 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
SSung023 committed May 31, 2024
1 parent b6c895f commit ee72b7d
Showing 1 changed file with 28 additions and 33 deletions.
61 changes: 28 additions & 33 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
name: Build and Deploy to EC2

on:
push:
branches: [ "production", "feat/194-deployment-automation" ] ## 나중에 production으로 변경하기
pull_request:
branches: [ "production" ]

env:
AWS_REGION: ap-northeast-2
AWS_S3_BUCKET: gitget-deploy-bucket
AWS_CODE_DEPLOY_APPLICATION: GitGet-Application-CD
AWS_CODE_DEPLOY_GROUP: GitGet-Deployment-Group

jobs:
build-docker-image:
deploy:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v4
Expand All @@ -24,7 +34,6 @@ jobs:
touch ./application.yml
touch ./application-common.yml
touch ./application-prod.yml
echo "${{ secrets.APPLICATION }}" > ./application.yml
echo "${{ secrets.COMMON }}" > ./application-common.yml
echo "${{ secrets.PROD }}" > ./application-prod.yml
Expand All @@ -42,37 +51,23 @@ jobs:
run: chmod +x ./gradlew
shell: bash

- name: Build with Gradle
run: ./gradlew clean build
- name: Build and Test
run: ./gradlew build test

- name: docker login
uses: docker /login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Make zip file
run: zip -r ./$GITHUB_SHA.zip .
shell: bash

- name: docker image build
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/gitget-application .
- name: AWS credential 설정
uses: aws-actions/configure-aws-credentials@v1
with:
aws-region: ${{ env.AWS_REGION }}
aws-access-key-id: ${{ secrets.CICD_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.CICD_SECRET_KEY }}

- name: dockerhub push
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/gitget-application

run-docker-image-on-ec2:
needs: build-docker-image
runs-on: self-hosted
steps:
- name: execute deploy.sh
run: sh /home/ec2-user/deploy.sh
- name: Upload to S3
run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://$AWS_S3_BUCKET/$GITHUB_SHA.zip

# Slack Webhook 설정
- name: action-slack
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
author_name: Backend
fields: repo,commit,message,author
mention: here
if_mention: failure,cancelled
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
if: always()
- name: EC2에 배포
run: aws deploy create-deployment --application-name ${{ env.AWS_CODE_DEPLOY_APPLICATION }} --deployment-config-name CodeDeployDefault.AllAtOnce --deployment-group-name ${{ env.AWS_CODE_DEPLOY_GROUP }} --s3-location bucket=$AWS_S3_BUCKET,key=$GITHUB_SHA.zip,bundleType=zip

0 comments on commit ee72b7d

Please sign in to comment.