Create README.md #18
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
name: Java CI with Gradle | |
on: | |
push: | |
branches: [ "main" ] | |
permissions: | |
contents: read | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
env: | |
working-directory: ./ | |
APPLICATION: ${{ secrets.APPLICATION_SECRET }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set up JDK 11 | |
uses: actions/setup-java@v2 | |
with: | |
java-version: '11' | |
distribution: 'temurin' | |
- name: Cache Gradle dependencies | |
uses: actions/cache@v2 | |
with: | |
path: | | |
~/.gradle/caches | |
~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
- name: Create application.yml | |
run: | | |
mkdir -p ./src/main/resources | |
echo "${APPLICATION}" > ./src/main/resources/application.yml | |
- name: Upload application.yml artifact | |
uses: actions/upload-artifact@v2 | |
with: | |
name: application.yml | |
path: ./src/main/resources/application.yml | |
- name: Grant execute permission for gradlew | |
run: chmod +x gradlew | |
working-directory: ${{ env.working-directory }} | |
- name: Build with Gradle | |
run: ./gradlew clean build --exclude-task test | |
working-directory: ${{ env.working-directory }} | |
- name: Cleanup Gradle Cache | |
if: always() | |
run: | | |
rm -f ~/.gradle/caches/modules-2/modules-2.lock | |
rm -f ~/.gradle/caches/modules-2/gc.properties | |
- name: Docker build and push | |
run: | | |
docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} | |
docker build -t ${{ secrets.PROJECT_NAME }} . | |
docker tag ${{ secrets.PROJECT_NAME }} ${{ secrets.DOCKERHUB_REPO }}:${{ github.sha }} | |
docker push ${{ secrets.DOCKERHUB_REPO }}:${{ github.sha }} | |
- name: Deploy to EC2 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ec2-user | |
key: ${{ secrets.PRIVATE_KEY }} | |
envs: GITHUB_SHA | |
script: | | |
docker rmi $(docker images -q) | |
docker pull ${{ secrets.DOCKERHUB_REPO }}:${{ github.sha }} | |
docker tag ${{ secrets.DOCKERHUB_REPO }}:${{ github.sha }} ${{ secrets.PROJECT_NAME }} | |
docker stop ${{ secrets.PROJECT_NAME }} || true | |
docker rm ${{ secrets.PROJECT_NAME }} || true | |
docker run -d --name ${{ secrets.PROJECT_NAME }} -p 8082:8080 ${{ secrets.PROJECT_NAME }} |