Update deploy.yml #8
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: Build & Deploy Backend (GHCR → EC2) | |
| on: | |
| push: | |
| branches: [ main ] | |
| permissions: | |
| contents: read | |
| packages: write | |
| jobs: | |
| build-push-deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: actions/setup-java@v4 | |
| with: | |
| distribution: temurin | |
| java-version: 17 | |
| - uses: actions/cache@v4 | |
| with: | |
| path: | | |
| ~/.gradle/caches | |
| ~/.gradle/wrapper | |
| key: gradle-${{ runner.os }}-${{ hashFiles('**/*.gradle*','**/gradle-wrapper.properties') }} | |
| - run: ./gradlew clean bootJar --no-daemon | |
| - uses: docker/setup-buildx-action@v3 | |
| - uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| push: true | |
| tags: | | |
| ghcr.io/dmu-debugvisual/debugvisual-backend:latest | |
| ghcr.io/dmu-debugvisual/debugvisual-backend:${{ github.sha }} | |
| - name: Deploy on EC2 (compose pull/up) | |
| env: | |
| HOST: ${{ secrets.EC2_HOST }} | |
| USER: ${{ secrets.EC2_USER }} | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "${{ secrets.EC2_KEY }}" > ~/.ssh/id_rsa | |
| sed -i 's/\r$//' ~/.ssh/id_rsa | |
| chmod 600 ~/.ssh/id_rsa | |
| ssh-keyscan -H "$HOST" >> ~/.ssh/known_hosts | |
| ssh "$USER@$HOST" "\ | |
| docker login ghcr.io -u ${{ github.actor }} -p ${{ secrets.GITHUB_TOKEN }} && \ | |
| cd ~/apps/debugvisual && \ | |
| docker compose pull && docker compose up -d && \ | |
| docker image prune -af || true" |