name: deploy

on:
  # 원래 여기에 push 이벤트가 들어갔어야 하는데 `workflow_dispatch`라는 키워드가 들어갔죠?
  # 즉, "workflow를 수동으로 trigger시킬게"라는 의미입니다.
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: touch ./src/main/resources/application.properties
      - run: echo "${{ secrets.APPLICATION }}" > ./src/main/resources/application.properties
      - name: Set up JDK 11
        uses: actions/setup-java@v3
        with:
          java-version: 11
          distribution: 'temurin'
          cache: 'gradle'

      # Gradle wrapper 파일 실행 권한주기
      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      # Gradle Cache 추가
      - name: Cache Gradle packages
        id: cache-npm
        uses: actions/cache@v3
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
          restore-keys: |
            ${{ runner.os }}-gradle-
      - if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }}
        name: List the state of node modules
        continue-on-error: true
        run: npm list

      - name: Get Github action IP
        id: ip
        uses: haythem/[email protected]

      - name: Setting environment variables
        run: |
          echo "AWS_DEFAULT_REGION=ap-northeast-2" >> $GITHUB_ENV
          echo "AWS_SG_NAME=daemo_Security" >> $GITHUB_ENV

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v2
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ap-northeast-2

      - name: Add Github Actions IP to AWS Security group
        run: |
          aws ec2 authorize-security-group-ingress --group-name ${{ env.AWS_SG_NAME }} --protocol tcp --port 3306 --cidr ${{ steps.ip.outputs.ipv4 }}/32
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: ap-northeast-2

      - name: Setup and execute Gradle 'build' task
        uses: gradle/gradle-build-action@v2
        with:
          arguments: clean build
          cache-read-only: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/develop' }}

      - name: Remove Github Actions IP from AWS security group
        if: ${{ always() }}
        run: |
          aws ec2 revoke-security-group-ingress --group-name ${{ env.AWS_SG_NAME }} --protocol tcp --port 3306 --cidr ${{ steps.ip.outputs.ipv4 }}/32
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: ap-northeast-2

      - name: Upload JAR
        uses: actions/upload-artifact@v3
        with:
          name: artifact
          path: build/libs/hello-spring-0.0.1-SNAPSHOT.jar

  deploy:
    needs: build
    name: Deploy
    runs-on: ubuntu-latest
    steps:
      - name: Download JAR
        uses: actions/download-artifact@v3
        with:
          name: artifact
      #Deploy the artifact (JAR) into AWS Beanstalk
      - name: Deploy to EB
        uses: einaregilsson/beanstalk-deploy@v21
        with:
          aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          #aws_session_token: ${{ secrets.AWS_SESSION_TOKEN }}
          use_existing_version_if_available: false
          application_name: GitHub_Action_Test
          environment_name: GitHubActionTest-env
          version_label: ${{github.SHA}}
          region: ap-northeast-2
          deployment_package: hello-spring-0.0.1-SNAPSHOT.jar

수동으로 eb deploy하는 부분(workflow_dispatch)

Untitled

git 변경사항

Untitled

기존 eb

Untitled

새로 배포한 eb

Untitled

GitHub Action success

Untitled

생성된 Artifacts - hello-spring-0.0.1-SNAPSHOT.jar

Untitled

aws beanstalk 생성된 이벤트 로그

Untitled