Java CI with Gradle #106
This file contains hidden or 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: [ "develop" ] | |
| pull_request: | |
| branches: [ "develop" ] | |
| permissions: | |
| contents: read | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout the repository | |
| uses: actions/checkout@v3 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v3 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| # gradle caching - ๋น๋ ์๊ฐ ํฅ์ | |
| - name: Gradle Caching | |
| uses: actions/cache@v3 | |
| with: | |
| path: | | |
| ~/.gradle/caches | |
| ~/.gradle/wrapper | |
| key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
| restore-keys: | | |
| ${{ runner.os }}-gradle- | |
| # ํ๊ฒฝ๋ณ yml ํ์ผ ์์ฑ(1) - application.yml | |
| - name: make application.yml | |
| if: | | |
| contains(github.ref, 'develop') || | |
| (github.event_name == 'pull_request') | |
| run: | | |
| mkdir ./src/main/resources # resources ํด๋ ์์ฑ | |
| cd ./src/main/resources # resources ํด๋๋ก ์ด๋ | |
| touch ./application.yml # application.yml ์์ฑ | |
| echo "${{ secrets.YML }}" > ./application.yml # github actions์์ ์ค์ ํ ๊ฐ์ application.yml ํ์ผ์ ์ฐ๊ธฐ | |
| shell: bash | |
| # ํ๊ฒฝ๋ณ yml ํ์ผ ์์ฑ(2) - dev | |
| - name: make application-dev.yml | |
| if: | | |
| contains(github.ref, 'develop') || | |
| (github.event_name == 'pull_request') | |
| run: | | |
| cd ./src/main/resources | |
| touch ./application-dev.yml | |
| echo "${{ secrets.YML_DEV }}" > ./application-dev.yml | |
| shell: bash | |
| # ํ๊ฒฝ๋ณ yml ํ์ผ ์์ฑ(3) - bucket | |
| - name: make application-bucket.yml | |
| if: | | |
| contains(github.ref, 'develop') || | |
| (github.event_name == 'pull_request') | |
| run: | | |
| cd ./src/main/resources | |
| touch ./application-dev.yml | |
| echo "${{ secrets.YML_BUCKET }}" > ./application-bucket.yml | |
| shell: bash | |
| # ํ๊ฒฝ๋ณ yml ํ์ผ ์์ฑ(4) - oauth | |
| - name: make application-oauth.yml | |
| if: | | |
| contains(github.ref, 'develop') || | |
| (github.event_name == 'pull_request') | |
| run: | | |
| cd ./src/main/resources | |
| touch ./application-oauth.yml | |
| echo "${{ secrets.YML_OAUTH }}" > ./application-oauth.yml | |
| shell: bash | |
| # ํ๊ฒฝ๋ณ yml ํ์ผ ์์ฑ(5) - jwt | |
| - name: make application-jwt.yml | |
| if: | | |
| contains(github.ref, 'develop') || | |
| (github.event_name == 'pull_request') | |
| run: | | |
| cd ./src/main/resources | |
| touch ./application-jwt.yml | |
| echo "${{ secrets.YML_JWT }}" > ./application-jwt.yml | |
| shell: bash | |
| # Gradle Build (test๋ ์ ์ธ) | |
| - name: Build with Gradle | |
| run: | | |
| chmod +x gradlew | |
| ./gradlew build -x test | |
| # ๋์ปค ํ๋ธ์ ๋ก๊ทธ์ธ | |
| - name: Docker Hub Login | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ secrets.DOCKER_USER_NAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Build Docker image | |
| run: docker build -t ${{ secrets.DOCKER_USER_NAME }}/wedit . | |
| - name: Push Docker image | |
| run: docker push ${{ secrets.DOCKER_USER_NAME }}/wedit | |
| deploy: | |
| runs-on: ubuntu-latest | |
| needs: build | |
| if: github.event_name == 'push' | |
| steps: | |
| - name: Deploy to EC2 | |
| uses: appleboy/[email protected] | |
| with: | |
| host: ${{ secrets.HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| port: 22 | |
| script: | | |
| EXISTING_CONTAINER_ID=$(sudo docker ps -q -f "publish=8080" -f "status=running") | |
| if [ ! -z "$EXISTING_CONTAINER_ID" ]; then | |
| sudo docker stop $EXISTING_CONTAINER_ID | |
| sudo docker rm $EXISTING_CONTAINER_ID | |
| fi | |
| EXISTING_CONTAINER_ID=$(sudo docker ps -q -f "status=exited") | |
| if [ ! -z "$EXISTING_CONTAINER_ID" ]; then | |
| sudo docker rm $EXISTING_CONTAINER_ID | |
| fi | |
| sudo docker pull ${{ secrets.DOCKER_USER_NAME }}/wedit | |
| sudo docker run --name spring -d -p 8080:8080 -e TZ=Asia/Seoul ${{ secrets.DOCKER_USER_NAME }}/wedit | |
| sudo docker image prune -a -f | |
| debug: true # Enable debugging output |