Create deploy.yml #1
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
yamlCopy code | ||
name: Deploy Spring Boot Application to EC2 | ||
on: | ||
push: | ||
branches: | ||
- main | ||
jobs: | ||
build_and_deploy: | ||
name: Build and Deploy | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout Code | ||
uses: actions/checkout@v2 | ||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v2 | ||
with: | ||
java-version: '17' | ||
- name: Cache Gradle packages | ||
uses: actions/cache@v2 | ||
with: | ||
path: ~/.gradle/caches | ||
key: gradle-${{ runner.os }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | ||
restore-keys: | | ||
gradle-${{ runner.os }} | ||
- name: Build with Gradle | ||
run: ./gradlew build -x test | ||
- name: Archive JAR | ||
uses: actions/upload-artifact@v2 | ||
with: | ||
name: issuetracker | ||
path: build/libs/*.jar | ||
- 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 | ||
- name: Transfer JAR to EC2 | ||
run: | | ||
scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa \ | ||
build/libs/*.jar ubuntu@${{ secrets.EC2_HOST }}:/home/ubuntu/issuetracker.jar | ||
- name: Deploy on EC2 | ||
run: | | ||
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ubuntu@${{ secrets.EC2_HOST }} << 'EOF' | ||
# Stop existing application | ||
pkill -f 'java.*issuetracker.jar' | ||
# Start new application | ||
export DB_USERNAME=${{ secrets.DB_USERNAME }} | ||
export DB_PASSWORD=${{ secrets.DB_PASSWORD }} | ||
nohup java -jar /home/ubuntu/issuetracker.jar > /dev/null 2>&1 & | ||
EOF |