Skip to content

deploy

deploy #23

Workflow file for this run

name: deploy
on:
workflow_dispatch:
inputs:
confirm:
description: 'Type "yes" to confirm manual build & deploy on master'
required: true
default: 'no'
env:
APP_NAME: kartel
MAIN_PATH: /home/mfirhas/kartel
HTTP_PORT: 3000
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
name: Build
runs-on: ubuntu-latest
if: github.event.inputs.confirm == 'yes'
permissions:
contents: read
packages: write
steps:
- name: Checkout codebase
uses: actions/checkout@v6
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels)
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
type=raw,value=${{ inputs.tag }},enable=${{ inputs.tag != '' }}
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
APP_NAME=${{ env.APP_NAME }}
PORT=${{ env.HTTP_PORT }}
deploy:
name: Deploy
runs-on: ubuntu-latest
needs: build
if: github.event.inputs.confirm == 'yes'
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Copy docker-compose.yml
uses: appleboy/scp-action@v1
with:
host: ${{ secrets.VPS_HOST }}
username: ${{ secrets.VPS_USERNAME }}
key: ${{ secrets.VPS_SSH_KEY }}
port: ${{ secrets.VPS_PORT }}
source: "docker-compose.yml"
target: "~/${{ env.APP_NAME }}"
- name: Deployment
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.VPS_HOST }}
username: ${{ secrets.VPS_USERNAME }}
key: ${{ secrets.VPS_SSH_KEY }}
port: ${{ secrets.VPS_PORT }}
script: |
cp ~/${{ env.APP_NAME }}/docker-compose.yml ${{ env.MAIN_PATH }}/docker-compose.yml
chown mfirhas:mfirhas ${{ env.MAIN_PATH }}/docker-compose.yml
chmod 640 ${{ env.MAIN_PATH }}/docker-compose.yml
MAIN_PATH="${{ env.MAIN_PATH }}"
GHCR_PAT="${{ secrets.GHCR_PAT }}"
GHCR_USERNAME="${{ secrets.GHCR_USERNAME }}"
su - mfirhas << EOF
cd $MAIN_PATH
echo $GHCR_PAT | docker login ghcr.io -u $GHCR_USERNAME --password-stdin
echo "pulling docker image..."
docker compose pull
echo "tearing down current running container..."
docker compose down
echo "starting up from new image..."
docker compose up -d --remove-orphans
sleep 5
echo "docker status"
docker compose ps
echo "docker logs"
docker compose logs --tail=10 kartel
echo "clean up old images"
docker image prune -f
echo "πŸŽ‰πŸŽ‰πŸŽ‰ DEPLOYMENT COMPLETE πŸŽ‰πŸŽ‰πŸŽ‰"
EOF