diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml new file mode 100644 index 0000000..beba430 --- /dev/null +++ b/.github/workflows/gradle.yml @@ -0,0 +1,55 @@ +name: CD with Gradle + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + - name: make application yml + run: | + cd ./src/main/resources + touch ./application.yml + echo "${{ secrets.APPLICATION_YML }}" > ./application.yml + shell: bash + + - name: Build with Gradle + run: | + chmod +x ./gradlew + ./gradlew clean build -x test + + - name: Docker build & push to docker repo + run: | + docker login -u ${{ secrets.DOCKER_USERNAME_YUB }} -p ${{ secrets.DOCKER_PASSWORD_YUB }} + docker build -f Dockerfile -t "${{ secrets.DOCKER_USERNAME_YUB }}/hanafun:latest" . + docker push "${{ secrets.DOCKER_USERNAME_YUB }}/hanafun:latest" + + - name: Deploy to server + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.EC2_HOST }} + username: ec2-user + key: ${{ secrets.EC2_SSH_KEY }} + script: | + sudo docker ps + sudo docker pull "${{ secrets.DOCKER_USERNAME_YUB }}/hanafun:latest" + sudo docker stop hanafun + sudo docker rm hanafun + sudo docker run -d -p 8080:8080 --name hanafun "${{ secrets.DOCKER_USERNAME_YUB }}/hanafun:latest" + sudo docker image prune -f \ No newline at end of file