From 08027bce8e86ba4388c7446d34d484633a2aed49 Mon Sep 17 00:00:00 2001 From: moonjin-kim <69244467+moonjin-kim@users.noreply.github.com> Date: Tue, 26 Mar 2024 17:12:54 +0900 Subject: [PATCH] Create gradle-publish.yml --- .github/workflows/gradle-publish.yml | 71 ++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 .github/workflows/gradle-publish.yml diff --git a/.github/workflows/gradle-publish.yml b/.github/workflows/gradle-publish.yml new file mode 100644 index 0000000..1da9504 --- /dev/null +++ b/.github/workflows/gradle-publish.yml @@ -0,0 +1,71 @@ +name: Deploy to AWS EC2 + +on: + push: + branches: [ "main" ] + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Get Github Actions IP + id: ip + uses: haythem/public-ip@v1.2 + + - name: Configure AWS Credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.EC2_ACCESS_KEY }} + aws-secret-access-key: ${{ secrets.EC2_SECRET_KEY }} + aws-region: ${{ secrets.EC2_REGION }} + + - name: Add Github Actions IP to Security group + run: | + aws ec2 authorize-security-group-ingress --group-id ${{ secrets.EC2_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 + + - name: Set up JDK 17 + uses: actions/setup-java@v2 + with: + java-version: '17' + distribution: 'temurin' + + - name: Set up Docker Environment + run: | + touch ./.env + echo "DB_CONNECTION_URL=${{ secrets.MYSQL_DB_URL }}" >> .env + echo "USER_NAME=${{ secrets.DB_USER }}" >> .env + echo "PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env + echo "FILE_STORAGE_DIR=${{ secrets.FILE_PATH }}" >> .env + echo "SECRET_KEY=${{ secrets.SECRET_KEY }}" >> .env + echo "ACCESS_EXPIRE=${{ secrets.ACCESS_EXPIRE }}" >> .env + echo "REFRESH_EXPIRE=${{ secrets.REFRESH_EXPIRE }}" >> .env + shell: bash + + # Gradle build + - name: Build with Gradle + run: ./gradlew bootJar + + - name: web docker build and push + run: | + docker login -u ${{ secrets.DOCKER_ID }} -p ${{ secrets.DOCKER_PASSWORD }} + docker build -t ${{ secrets.DOCKER_REPO }}/planit-server . + docker push ${{ secrets.DOCKER_REPO }}/planit-server + + - name: Deploy to EC2 + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USER }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + script: | + sudo docker rm -f $(docker ps -qa) + sudo docker pull ${{ secrets.DOCKER_REPO }}/planit-server + docker-compose up -d + docker image prune -f + + - name: Remove Github Actions IP From Security Group + run: | + aws ec2 revoke-security-group-ingress --group-id ${{ secrets.EC2_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32