Feat/#20 CI/CD 기능 추가 #3
Workflow file for this run
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: | |
pull_request: | |
branches: [ "main" ] | |
jobs: | |
build_app: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
# Configure Gradle for optimal use in GiHub Actions, including caching of downloaded dependencies. | |
# See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md | |
- name: Setup Gradle2 | |
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0 | |
- name: Build with Gradle Wrapper | |
run: | | |
cd src/main/resources | |
echo "${{ secrets.APPLICATION }}" > ./application.yml | |
- name: build with gradle | |
run: | | |
chmod +x gradlew | |
./gradlew clean build -x test | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Docker Build Image | |
run : | | |
docker build -t nowsopt . | |
docker tag nowsopt ${{ secrets.DOCKER_REGISTRY_URI }} | |
- name: Login to ECR | |
uses: docker/login-action@v3 | |
with: | |
registry: 471112612111.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com | |
username: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
password: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
- name : docker image to Registry | |
run : | | |
docker push ${{ secrets.DOCKER_REGISTRY_URI }} | |
deploy_app: | |
needs : build_app | |
name: connect to EC2 | |
runs-on: ubuntu-latest | |
if: github.event.pull_request.merged == true | |
steps: | |
- name: Setup SSH for EC2 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.REMOTE_IP }} | |
username: ${{ secrets.AWS_USER }} | |
key: ${{ secrets.AWS_KEY }} | |
script: | | |
aws ecr get-login-password --region ap-southeast-2 | docker login --username AWS --password-stdin 471112612111.dkr.ecr.ap-southeast-2.amazonaws.com | |
docker stop nowsopt | |
docker rm nowsopt | |
docker rmi ${{ secrets.DOCKER_REGISTRY_URI }} | |
docker pull ${{ secrets.DOCKER_REGISTRY_URI }} | |
docker run \ | |
-d -p 8080:8080 -p 5432:5432 \ | |
--name nowsopt ${{ secrets.DOCKER_REGISTRY_URI }} | |
aws ecr batch-delete-image \ | |
--repository-name nowsopt \ | |
--image-ids imageTag=latest |