배포 준비 #3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This workflow uses actions that are not certified by GitHub. | |
# They are provided by a third-party and are governed by | |
# separate terms of service, privacy policy, and support | |
# documentation. | |
# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time | |
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle | |
name: Java CI with Gradle | |
on: | |
push: | |
branches: [ "main" ] | |
pull_request: | |
branches: [ "main" ] | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v2 | |
with: | |
java-version: '17' #Java 버전 기임 | |
distribution: 'temurin' | |
# Spring Boot Build | |
- name: Spring Boot Build | |
run: ./gradlew clean build -x test #test 제외 build | |
# Docker Image Build | |
- name: Docker Image Build | |
run: docker build -t hisdhifslhdf/favorite-place . #image명 기입 | |
# DockerHub Login | |
- name: Docker Hub Login | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
# Docker Hub push | |
- name: docker Hub push | |
run: docker push hisdhifslhdf/favorite-place | |
# GET GitHub IP (5) | |
- name: get GitHub IP | |
id: ip | |
uses: haythem/[email protected] | |
# Configure AWS Credentials - AWS 접근 권한 취득(IAM) | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ap-northeast-2 # AWS EC2 지역명 기입 | |
# Add github ip to AWS | |
- name: Add GitHub IP to AWS | |
run: | | |
echo "GitHub Runner IP: ${{ steps.ip.outputs.ipv4 }}" | |
aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 | |
# AWS EC2 Server Connect & Docker 명령어 실행 (8) | |
- name: AWS EC2 Connection | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USERNAME }} | |
password: ${{ secrets.EC2_PASSWORD }} | |
port: ${{ secrets.EC2_SSH_PORT }} | |
timeout: 60s | |
script: | #Docker Image명 기입, port 기입 | |
sudo docker stop favorite-place | |
sudo docker rm favorite-place | |
sudo docker run -it -d -p 8080:8080 --name favorite-place hisdhifslhdf/favorite-place | |
# REMOVE Github IP FROM security group (9) | |
- name: Remove IP FROM security group | |
run: | | |
aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 |