Optimize build&deploy step #15
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-and-deploy: | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
service: [frontend, backend] | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Setup Docker Buildx | |
uses: docker/setup-buildx-action@v1 | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Cache Docker layers | |
uses: actions/cache@v2 | |
with: | |
path: /tmp/.buildx-cache | |
key: ${{ runner.os }}-buildx-${{ matrix.service }}-${{ github.sha }} | |
restore-keys: | | |
${{ runner.os }}-buildx-${{ matrix.service }}- | |
- name: Build and push Docker image | |
run: | | |
docker buildx create --use | |
docker buildx build --push --tag devruby/l2s-${{ matrix.service }}:latest --cache-from=type=local,src=/tmp/.buildx-cache --cache-to=type=local,mode=max,dest=/tmp/.buildx-cache ./${{ matrix.service }} | |
- 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 }}:latest || true | |
sudo docker pull devruby/l2s-${{ matrix.service }}:latest | |
if [ "${{ matrix.service }}" = "frontend" ]; then | |
sudo docker run -d --name l2s-frontend -p 3001:3000 devruby/l2s-frontend:latest | |
else | |
sudo docker run -d --name l2s-backend -v l2s-data:/workspace/data -p 8001:8000 devruby/l2s-backend:latest | |
fi |