github actions for deployment #1
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: Deployment Dev | ||
on: | ||
push: | ||
branches: | ||
- main | ||
workflow_dispatch: | ||
inputs: | ||
env_file: | ||
description: 'Environment file to use (e.g., .env, .env_prod)' | ||
required: true | ||
default: '.env' | ||
jobs: | ||
build-and-deploy: | ||
name: Build and Deployment | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- name: Checkout Code | ||
uses: actions/checkout@v2 | ||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v1 | ||
- name: Log in to GitHub Container Registry | ||
uses: docker/login-action@v1 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
- name: Build and Push Docker Image | ||
uses: docker/build-push-action@v2 | ||
with: | ||
context: ../ | ||
file: ../Dockerfile | ||
push: true | ||
tags: ghcr.io/${{ github.actor }}/launchpad:main | ||
build-args: | ||
ENV_FILE: ${{ github.event.inputs.env_file }} | ||
# Deploy to Server | ||
- name: SSH & Deploy | ||
uses: appleboy/ssh-action@master | ||
with: | ||
host: ${{ secrets.SSH_SERVER_IP }} | ||
username: ${{ secrets.SSH_SERVER_USER }} | ||
key: ${{ secrets.SSH_PRIVATE_KEY }} | ||
script: | | ||
# Stop and remove existing containers | ||
docker-compose -f docker-compose.yml down | ||
# Remove dangling images | ||
docker rmi $(docker images -f "dangling=true" -q) | ||
# Pull and run the new image | ||
docker-compose -f docker-compose.yml pull | ||
GITHUB_USERNAME=${{ github.actor }} docker-compose up -d --build | ||
exit 0; |