Skip to content

Merge pull request #6 from Alpha-mon/5-feat-github-action #5

Merge pull request #6 from Alpha-mon/5-feat-github-action

Merge pull request #6 from Alpha-mon/5-feat-github-action #5

Workflow file for this run

name: Deploy gradle in EC2
on:
push:
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'
## Create secret yml files before build
- name: create application-common, local, test.yml
shell: bash
run: |
touch ./src/main/resources/application-common.yml
echo "${{secrets.APPLICATION_COMMON}}" > ./src/main/resources/application-common.yml
touch ./src/main/resources/application-local.yml
echo "${{secrets.APPLICATION_LOCAL}}" > ./src/main/resources/application-local.yml
touch ./src/main/resources/application-test.yml
echo "${{secrets.APPLICATION_TEST}}" > ./src/main/resources/application-test.yml
## Gradle
- name: Cache gradle packages
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Grant execute permission for gradlew
run: chmod +x gradlew
## Build without test
- name: Build with Gradle
run: ./gradlew clean build -x test
## Docker
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
file: ./Dockerfile
platforms: linux/amd64
push: true
tags: ${{ secrets.DOCKERHUB_REPO }}:latest
deploy:
needs: build # Run after build
runs-on: ubuntu-latest
steps:
- name: connect EC2 instance and deploy docker images to main server
uses: appleboy/[email protected]
with:
host: ${{ secrets.AWS_EC2_HOST_IP }}
username: ${{ secrets.AWS_EC2_USERNAME }}
key: ${{ secrets.AWS_SSH_KEY }}
## kill container -> remove container -> remove image -> pull new image -> run
script: |
sudo docker kill ${{ secrets.PROJECT_NAME }}
sudo docker rm ${{ secrets.PROJECT_NAME }}
sudo docker rmi ${{ secrets.DOCKERHUB_REPO }}
sudo docker pull ${{ secrets.DOCKERHUB_REPO }}:latest
sudo docker run -d --name ${{ secrets.PROJECT_NAME }} -p 8080:8080 ${{ secrets.DOCKERHUB_REPO }}:latest