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: Auto Deploy in VPS after Push in Production | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
name: Deploy project | |
permissions: | |
deployments: write | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v2 | |
with: | |
ref: main | |
token: ${{ secrets.PERSONAL_GITHUB_TOKEN }} | |
- name: Create GitHub deployment | |
uses: chrnorm/deployment-action@v2 | |
id: deployment | |
with: | |
token: '${{ github.token }}' | |
environment-url: ${{ vars.MY_APP }} | |
environment: production | |
- name: Set up SSH Key and Deploy my App on Server | |
uses: appleboy/ssh-action@master | |
env: | |
GITHUB_TOKEN: ${{ secrets.PERSONAL_GITHUB_TOKEN }} | |
with: | |
host: ${{ secrets.VPS_IP }} | |
username: ${{ secrets.VPS_USERNAME }} | |
password: ${{ secrets.VPS_PASSWORD }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
port: 22 | |
script: | | |
# Set up GitHub authentication for pulling updates | |
git config --global url."https://${{ secrets.PERSONAL_GITHUB_TOKEN }}:@github.com".insteadOf "https://github.com" | |
# Navigate to project directory (assuming your docker-compose.yml is in the root) | |
cd ~/casino-roulette | |
# Pull the latest code from the main branch | |
git pull origin main | |
# Ensure Docker Compose is installed (if not already) | |
# if ! command -v docker-compose &> /dev/null; then | |
# echo "Docker Compose not found, installing..." | |
# sudo curl -L "https://github.com/docker/compose/releases/download/$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")')" -o /usr/local/bin/docker-compose | |
# sudo chmod +x /usr/local/bin/docker-compose | |
# fi | |
# Build and start/restart containers with Docker Compose | |
sudo docker compose down # Stop existing containers, if any | |
sudo docker compose pull # Pull new images if they have changed | |
sudo docker compose up -d --build # Rebuild and start containers in detached mode | |
- name: Update deployment Status (success) | |
if: success() | |
uses: chrnorm/deployment-status@v2 | |
with: | |
token: '${{ github.token }}' | |
environment-url: ${{ vars.MY_APP }} | |
state: 'success' | |
deployment-id: ${{ steps.deployment.outputs.deployment_id }} | |
- name: Update deployment status (failure) | |
if: failure() | |
uses: chrnorm/deployment-status@v2 | |
with: | |
token: '${{ github.token }}' | |
environment-url: ${{ vars.MY_APP }} | |
state: 'failure' | |
deployment-id: ${{ steps.deployment.outputs.deployment_id }} |