Merge pull request #9 from THIP-TextHip/chore/#8-ci_cd #17
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: CD with Gradle and Docker | |
| on: | |
| push: | |
| branches: | |
| - 'develop' | |
| permissions: | |
| contents: read | |
| env: | |
| RESOURCE_PATH: src/main/resources | |
| COMPOSE_PATH: /home/ec2-user/compose | |
| APP_PORT: 8000 | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: ☕️ set up JDK 17 | |
| uses: actions/setup-java@v3 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| server-id: github | |
| settings-path: ${{ github.workspace }} | |
| - name: 🧾 Create application.yml from secret | |
| run: | | |
| mkdir -p ${{ env.RESOURCE_PATH }} | |
| echo "${{ secrets.APPLICATION_YML_DEV }}" > ${{ env.RESOURCE_PATH }}/application.yml | |
| shell: bash | |
| - name: 🐘 Cache Gradle dependencies | |
| uses: actions/cache@v3 | |
| with: | |
| path: | | |
| ~/.gradle/caches | |
| ~/.gradle/wrapper | |
| key: gradle-${{ runner.os }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
| restore-keys: | | |
| gradle-${{ runner.os }}- | |
| - name: 👏🏻 grant execute permission for gradlew | |
| run: chmod +x gradlew | |
| - name: 🐘 build with Gradle (without test) | |
| run: ./gradlew clean build -x test --stacktrace | |
| - name: 🐳 Docker build & push | |
| run: | | |
| docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} | |
| docker build --build-arg PORT=${{env.APP_PORT}} -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }} . | |
| docker push ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }} | |
| - name: 🚀 deploy to server | |
| uses: appleboy/ssh-action@master | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USERNAME }} | |
| key: ${{ secrets.EC2_KEY }} | |
| port: ${{ secrets.EC2_PORT }} | |
| envs: GITHUB_SHA | |
| script: | | |
| echo "🗂️ Change Directory to Compose Path" | |
| cd ${{ env.COMPOSE_PATH }} | |
| echo "✋🏻Stopping existing container and Cleaning up old images" | |
| sudo docker-compose down --rmi all | |
| sudo docker ps -a | |
| echo "🥳 Pulling new image" | |
| sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }} | |
| echo "🌱 Starting new container" | |
| sudo docker-compose up -d | |
| sudo docker image prune -f |