Update deploy.yml #14
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: Deploy Frontend and Backend | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
service: [frontend, backend] | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Cache Docker layers | |
uses: actions/cache@v3 | |
with: | |
path: /tmp/.buildx-cache | |
key: ${{ runner.os }}-buildx-${{ matrix.service }}-${{ github.sha }} | |
restore-keys: | | |
${{ runner.os }}-buildx-${{ matrix.service }}- | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Build and push Docker image | |
run: | | |
docker buildx build --cache-from type=local,src=/tmp/.buildx-cache --cache-to type=local,dest=/tmp/.buildx-cache-new --push --tag devruby/l2s-${{ matrix.service }}:latest ./${{ matrix.service }} | |
mv /tmp/.buildx-cache-new /tmp/.buildx-cache | |
deploy: | |
runs-on: ubuntu-latest | |
needs: build | |
strategy: | |
matrix: | |
service: [frontend, backend] | |
steps: | |
- name: Deploy to server | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SERVER_HOST }} | |
username: ${{ secrets.SERVER_USER }} | |
key: ${{ secrets.SERVER_PRIVATE_KEY }} | |
script: | | |
sudo docker stop l2s-${{ matrix.service }} || true | |
sudo docker rm l2s-${{ matrix.service }} || true | |
sudo docker image rm devruby/l2s-${{ matrix.service }} || true | |
sudo docker pull devruby/l2s-${{ matrix.service }}:latest | |
if [ "${{ matrix.service }}" == "frontend" ]; then | |
sudo docker run -d --name l2s-${{ matrix.service }} -p 3001:3000 devruby/l2s-${{ matrix.service }}:latest | |
else | |
sudo docker run -d --name l2s-${{ matrix.service }} -v l2s-data:/workspace/data -p 8001:8000 devruby/l2s-${{ matrix.service }}:latest | |
fi |