Deploy #33
Workflow file for this run
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 | |
on: | |
workflow_dispatch: | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_TAG: latest | |
FRONTEND_PATH: ./frontend | |
BACKEND_PATH: ./backend | |
DOCKER_COMPOSE_FILE: docker-compose.prod.yml | |
# TODO: добавить кэш для зависимостей и фронта, и бэка | |
jobs: | |
container-registry-login: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
outputs: | |
repository_name_lowercase: ${{ steps.formatting.outputs.lowercase }} | |
meta_tags: ${{ steps.meta.outputs.tags }} | |
meta_labels: ${{ steps.meta.outputs.labels }} | |
steps: | |
- name: Get repository code | |
uses: actions/checkout@v4 | |
- name: Log in container registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.REGISTRY }}/${{ github.repository }} | |
- name: Get repository name in lowercase | |
id: formatting | |
uses: ASzc/change-string-case-action@v5 | |
with: | |
string: ${{ github.repository }} | |
publish-frontend-image: | |
needs: [container-registry-login] | |
runs-on: ubuntu-latest | |
# permissions: | |
# contents: read | |
# packages: write | |
outputs: | |
image-name: ${{ steps.image-name.outputs.IMAGE_NAME }} | |
steps: | |
- name: Get repository code | |
uses: actions/checkout@v4 | |
- name: Output Frontend Docker image name | |
id: image-name | |
run: echo "IMAGE_NAME=${{ env.REGISTRY }}/${{ needs.container-registry-login.outputs.repository_name_lowercase }}-frontend:${{ env.IMAGE_TAG }}" >> "$GITHUB_OUTPUT" | |
- name: Build and push Frontend Docker image | |
uses: docker/build-push-action@v5 | |
with: | |
context: ${{ env.FRONTEND_PATH }} | |
file: ${{ env.FRONTEND_PATH }}/Dockerfile | |
push: true | |
tags: ${{ needs.container-registry-login.outputs.meta_tags }},${{ steps.image-name.outputs.IMAGE_NAME }} | |
labels: ${{ needs.container-registry-login.outputs.meta_labels }} | |
publish-backend-image: | |
needs: [container-registry-login] | |
runs-on: ubuntu-latest | |
# permissions: | |
# contents: read | |
# packages: write | |
outputs: | |
image-name: ${{ steps.image-name.outputs.IMAGE_NAME }} | |
steps: | |
- name: Get repository code | |
uses: actions/checkout@v4 | |
- name: Output Backend Docker image name | |
id: image-name | |
run: echo "IMAGE_NAME=${{ env.REGISTRY }}/${{ needs.container-registry-login.outputs.repository_name_lowercase }}-backend:${{ env.IMAGE_TAG }}" >> "$GITHUB_OUTPUT" | |
- name: Build and push Backend Docker image | |
uses: docker/build-push-action@v5 | |
with: | |
context: ${{ env.BACKEND_PATH }} | |
file: ${{ env.BACKEND_PATH }}/Dockerfile | |
push: true | |
tags: ${{ needs.container-registry-login.outputs.meta_tags }},${{ steps.image-name.outputs.IMAGE_NAME }} | |
labels: ${{ needs.container-registry-login.outputs.meta_labels }} | |
pull-frontend-image: | |
needs: [publish-frontend-image] | |
runs-on: ubuntu-latest | |
steps: | |
# - name: Get repository code | |
# uses: actions/checkout@v4 | |
- name: Pull latest Frontend Docker image | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SERVER_IP }} | |
username: ${{ secrets.SERVER_USERNAME }} | |
password: ${{ secrets.SERVER_PASSWORD }} | |
script: docker pull ${{ needs.publish-frontend-image.outputs.image-name }} | |
pull-backend-image: | |
needs: [publish-backend-image] | |
runs-on: ubuntu-latest | |
steps: | |
# - name: Get repository code | |
# uses: actions/checkout@v4 | |
- name: Pull latest Backend Docker image | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SERVER_IP }} | |
username: ${{ secrets.SERVER_USERNAME }} | |
password: ${{ secrets.SERVER_PASSWORD }} | |
script: docker pull ${{ needs.publish-backend-image.outputs.image-name }} | |
deploy: | |
needs: [publish-frontend-image, publish-backend-image, pull-frontend-image, pull-backend-image] | |
runs-on: ubuntu-latest | |
steps: | |
# - name: Get repository code | |
# uses: actions/checkout@v4 | |
- name: Stop docker compose | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SERVER_IP }} | |
username: ${{ secrets.SERVER_USERNAME }} | |
password: ${{ secrets.SERVER_PASSWORD }} | |
script: docker compose -f /tmp/${{ env.DOCKER_COMPOSE_FILE }} down | |
- name: Copy docker compose to remote server | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SERVER_IP }} | |
username: ${{ secrets.SERVER_USERNAME }} | |
password: ${{ secrets.SERVER_PASSWORD }} | |
source: ${{ env.DOCKER_COMPOSE_FILE }} | |
target: /tmp | |
- name: Run docker compose | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SERVER_IP }} | |
username: ${{ secrets.SERVER_USERNAME }} | |
password: ${{ secrets.SERVER_PASSWORD }} | |
script: | | |
NOTISEND_TOKEN=${{ secrets.NOTISEND_TOKEN }} \ | |
FRONT_IMAGE=${{ needs.publish-frontend-image.outputs.image-name }} \ | |
BACK_IMAGE=${{ needs.publish-backend-image.outputs.image-name }} \ | |
docker compose -f /tmp/${{ env.DOCKER_COMPOSE_FILE }} up -d |