Deploy to VM #65
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 to VM | |
on: | |
push: | |
branches: | |
- master | |
workflow_dispatch: | |
inputs: | |
version: | |
description: 'Manual version override (optional)' | |
required: false | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Automatic Semantic Versioning | |
id: semver | |
if: github.event_name == 'push' | |
uses: anothrNick/[email protected] | |
env: | |
GITHUB_TOKEN: ${{ secrets.ACTIONS_TOKEN }} | |
DEFAULT_BUMP: patch | |
RELEASE_BRANCHES: master | |
TAG_PREFIX: v | |
- name: Set Version | |
run: echo "VERSION=${{ steps.semver.outputs.new_tag || github.event.inputs.version || 'latest' }}" >> $GITHUB_ENV | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to DockerHub | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
file: ./Dockerfile | |
push: true | |
tags: ${{ secrets.DOCKERHUB_USERNAME }}/backend:${{ env.VERSION }} | |
- name: Create app deirectory if doesn't exist yet | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.VM_HOST }} | |
username: ${{ secrets.VM_USERNAME }} | |
key: ${{ secrets.VM_PRIVATE_KEY }} | |
passphrase: ${{ secrets.VM_SSH_PASSPHRASE }} | |
script: | | |
mkdir -p ~/rivnegray | |
- name: Copy docker-compose file | |
uses: appleboy/scp-action@master | |
with: | |
host: ${{ secrets.VM_HOST }} | |
username: ${{ secrets.VM_USERNAME }} | |
key: ${{ secrets.VM_PRIVATE_KEY }} | |
passphrase: ${{ secrets.VM_SSH_PASSPHRASE }} | |
source: "docker-compose.app.yml" | |
target: "~/rivnegray/" | |
overwrite: true | |
- name: Deploy to VM | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.VM_HOST }} | |
username: ${{ secrets.VM_USERNAME }} | |
key: ${{ secrets.VM_PRIVATE_KEY }} | |
passphrase: ${{ secrets.VM_SSH_PASSPHRASE }} | |
script: | | |
export BACKEND_IMAGE_TAG=${{ env.VERSION }} | |
cd ~/rivnegray/ | |
docker-compose -f docker-compose.app.yml down | |
docker-compose -f docker-compose.app.yml up -d |