Created
June 22, 2025 03:35
-
-
Save alfanzain/767e678965a68b9d93c97b8b4138a3de to your computer and use it in GitHub Desktop.
Github Action Deploy on Push
This file contains hidden or 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 Staging | |
| # Trigger the workflow on every push to the 'main' branch | |
| on: | |
| push: | |
| branches: [main] | |
| jobs: | |
| deploy: | |
| # This job runs on the latest Ubuntu virtual machine provided by GitHub | |
| runs-on: ubuntu-latest | |
| steps: | |
| # Step 1: Checkout the repository code at the current commit | |
| - name: Checkout Commit | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ github.sha }} # Checkout the exact commit that triggered the workflow | |
| # Step 2: SSH into the VPS and run deployment commands | |
| - name: SSH and Deploy on VPS | |
| env: | |
| # These are stored securely in your GitHub repo's Secrets settings | |
| PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} # Private SSH key for authentication | |
| SSH_HOST: ${{ secrets.SSH_HOST }} # VPS host/IP | |
| SSH_USERNAME: ${{ secrets.SSH_USERNAME }} # Username for SSH login | |
| run: | | |
| # Write the private key to a file so it can be used by the ssh command | |
| echo "$PRIVATE_KEY" > private_key.pem | |
| # Secure the key file so SSH doesn't complain | |
| chmod 600 private_key.pem | |
| # Connect to the VPS and run deployment commands | |
| ssh -o StrictHostKeyChecking=no -i private_key.pem $SSH_USERNAME@$SSH_HOST << 'EOF' | |
| # Go to the project directory on the server | |
| cd /directory/to/project | |
| git pull origin main | |
| # Laravel things. You can do like `npm build`, run with `pm2` or something | |
| php artisan config:cache | |
| php artisan config:clear | |
| EOF | |
| # Remove the private key file after use to keep the runner secure | |
| rm -f private_key.pem |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment