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

name: cicd

jobs:
  build:

    name: Deploy
    runs-on: ubuntu-latest

    steps:
    - name: checkout src
      uses: actions/checkout@v2

    - name: Set up JDK 11
      uses: actions/setup-java@v2
      with:
        java-version: '11'
        distribution: 'adopt'

    - name: Grant execute permission for gradlew
      run: chmod 777 gradlew
      shell: bash

    - name: Clean build test with Gradle # clean build test
      run: ./gradlew clean build test
      shell: bash

    - name: Make zip file # zip 파일 생성
      run: zip -r ./springboot-blog.zip .
      shell: bash

    - name: Configure AWS credentials # AWS 자격 증명
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }}
        aws-region: ${{ secrets.AWS_REGION }}

    - name: Upload to S3 # S3 업로드
      run: aws s3 cp --region ${{ secrets.AWS_REGION }} ./springboot-blog.zip s3://${{ secrets.AWS_S3_BUCKET }}/springboot-blog.zip?$GITHUB_SHA

    - name: Code Deploy # CodeDeploy에 배포 요청
      run: aws deploy create-deployment --application-name ${{ secrets.AWS_CODEDEPLOY_NAME }} --deployment-config-name CodeDeployDefault.AllAtOnce --deployment-group-name ${{ secrets.AWS_CODEDEPLOY_GROUP }} --s3-location bucket=${{ secrets.AWS_S3_BUCKET }},bundleType=zip,key=springboot-blog.zip?$GITHUB_SHA