From 95b5fc21fcda63d38cda5df497f2dc18f81335ac Mon Sep 17 00:00:00 2001 From: light Date: Thu, 2 Nov 2023 11:48:58 +0900 Subject: [PATCH] Create cicd.yml --- .github/workflows/cicd.yml | 140 +++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 .github/workflows/cicd.yml diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml new file mode 100644 index 0000000..acbb598 --- /dev/null +++ b/.github/workflows/cicd.yml @@ -0,0 +1,140 @@ +name: controlG + +on: + push: + branches: [ "release" ] + + +permissions: + contents: read + +env: + BE_WORKING_DIR: ./be/controlG + NGINX_WORKING_DIR: ./nginx + APPLICATION_FILE_PATH: ./src/main/resources/application.yml + FE_WORKING_DIR: ./fe + FE_DOCKER_FILE_PATH: ./Dockerfile + +jobs: + + # Job[1]: BACKEND -> docker hub로 build & push + be-deploy: + runs-on: ubuntu-latest + services: + mysql_db: + image: mysql:8.0.23 + ports: + - 3306:3306 + env: + MYSQL_DATABASE: controlg + MYSQL_ROOT_PASSWORD: 1234 + redis: + image: redis:latest + ports: + - 6379:6379 + + defaults: + run: + working-directory: ${{ env.BE_WORKING_DIR }} + + + steps: + + # 1. Source 단계 + - name: Checkout Source Code + uses: actions/checkout@v3 + + # JDK 세팅 + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + # 2. yml 파일 변경 + - name: Change Spring Active Profile + run: | + touch '${{ env.APPLICATION_FILE_PATH }}' + echo '${{ secrets.APPLICATION }}' > '${{ env.APPLICATION_FILE_PATH }}' + # 3. build 단계 + - name: Grant execute permission for gradlew + run: chmod +x ./gradlew + + - name: Build with Gradle + run: ./gradlew clean build -Djasypt.encryptor.password=${{ secrets.JASYPT_SECRET }} + + + # 4. docker hub로 build & push + - name: docker build and push + run: | + docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} + docker build -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/control-g-team-5-be:latest . + docker push ${{ secrets.DOCKER_USERNAME }}/control-g-team-5-be:latest + fe-deploy: + runs-on: ubuntu-latest + defaults: + run: + working-directory: ${{ env.FE_WORKING_DIR }} + steps: + + # 1. Source 단계 + - name: Checkout Source Code + uses: actions/checkout@v3 + + # Node 세팅 + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: "18" + + # 2. 도커파일 생성 + - name: create Docker File + run: | + echo "${{ secrets.FE_DOCKER_FILE }}" > "${{ env.FE_DOCKER_FILE_PATH }}" + + + # 2. docker hub로 build & push + - name: docker build and push + run: | + docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} + docker build -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/control-g-team-5-fe:latest . + docker push ${{ secrets.DOCKER_USERNAME }}/control-g-team-5-fe:latest + + + # job[2]: nginx -> docker hub로 build & push + nginx-deploy: + runs-on: ubuntu-latest + defaults: + run: + working-directory: ${{ env.NGINX_WORKING_DIR }} + steps: + # 1. Source 단계 + - name: Checkout Source Code + uses: actions/checkout@v3 + + # 2. docker hub로 build & push + - name: docker build and push + run: | + docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} + docker build -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/control-g-team-5-nginx:latest . + docker push ${{ secrets.DOCKER_USERNAME }}/control-g-team-5-nginx:latest + + + # job[3]: EC2에서 docker 이미지 pull하고 실행 + SSH-Transfer: + needs: [ be-deploy, nginx-deploy , fe-deploy ] + runs-on: ubuntu-latest + steps: + - name: SSH Transfer + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.HOST }} + username: ubuntu + key: ${{ secrets.KEY }} + script: | + sudo docker-compose down -v + sudo docker pull ${{ secrets.DOCKER_USERNAME }}/control-g-team-5-be:latest + sudo docker pull ${{ secrets.DOCKER_USERNAME }}/control-g-team-5-fe:latest + sudo docker pull ${{ secrets.DOCKER_USERNAME }}/control-g-team-5-nginx:latest + sudo docker-compose up -d + sudo docker image prune -a -f