|
| 1 | +name: 🚀 Deploy NGINX HTTPS Reverse Proxy 🔐 |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["Deployment VPS"] |
| 6 | + types: |
| 7 | + - completed |
| 8 | + |
| 9 | +jobs: |
| 10 | + deploy: |
| 11 | + if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.head_branch == 'main' }} |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: 📥 Checkout code |
| 16 | + uses: actions/checkout@v3 |
| 17 | + |
| 18 | + - name: 🔧 Setup and load environment |
| 19 | + uses: ./.github/actions/setup-and-load-env |
| 20 | + with: |
| 21 | + DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} |
| 22 | + PACKAGE_NAME: ${{ secrets.PACKAGE_NAME }} |
| 23 | + PACKAGE_VERSION: ${{ secrets.PACKAGE_VERSION }} |
| 24 | + EMAIL: ${{ secrets.EMAIL }} |
| 25 | + BASE_URL: ${{ secrets.BASE_URL }} |
| 26 | + PORT: ${{ secrets.PORT }} |
| 27 | + IMAGE_TAG: ${{ secrets.IMAGE_TAG }} |
| 28 | + DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} |
| 29 | + GIT_TOKEN: ${{ secrets.EXECUTE_ME_GITHUB_TOKEN }} |
| 30 | + VPS_HOST: ${{ secrets.VPS_HOST }} |
| 31 | + VPS_USER: ${{ secrets.VPS_USER }} |
| 32 | + VPS_SSH_PRIVATE_KEY: ${{ secrets.VPS_SSH_PRIVATE_KEY }} |
| 33 | + |
| 34 | + - name: 📋 Verify environment variables |
| 35 | + run: | |
| 36 | + echo "Package name: $PACKAGE_NAME" |
| 37 | + echo "Package version: $PACKAGE_VERSION" |
| 38 | + echo "Docker image: $IMAGE_TAG" |
| 39 | + echo "✅ Environment variables are accessible" |
| 40 | +
|
| 41 | + - name: 🔐 Setup SSH |
| 42 | + run: | |
| 43 | + mkdir -p ~/.ssh |
| 44 | + chmod 700 ~/.ssh |
| 45 | + echo "${{secrets.VPS_SSH_PRIVATE_KEY}}" | tr -d '\r' > ~/.ssh/deploy_key |
| 46 | + chmod 600 ~/.ssh/deploy_key |
| 47 | + ssh-keyscan -H ${{secrets.VPS_HOST}} >> ~/.ssh/known_hosts |
| 48 | +
|
| 49 | + cat > ~/.ssh/config << EOF |
| 50 | + Host deploy-server |
| 51 | + HostName ${{secrets.VPS_HOST}} |
| 52 | + User ${{secrets.VPS_USER}} |
| 53 | + IdentityFile ~/.ssh/deploy_key |
| 54 | + StrictHostKeyChecking no |
| 55 | + EOF |
| 56 | + chmod 600 ~/.ssh/config |
| 57 | +
|
| 58 | + - name: 🚀 Test SSH Connection |
| 59 | + run: ssh deploy-server "echo '✅ SSH connection successful'" |
| 60 | + |
| 61 | + - name: 📁 Debug scripts directory |
| 62 | + run: ls -al ./scripts |
| 63 | + |
| 64 | + - name: 🧪 Run NGINX Setup Script on VPS |
| 65 | + run: | |
| 66 | + echo "🚀 Preparing to run setup-nginx.sh on VPS" |
| 67 | +
|
| 68 | + ssh deploy-server "bash -s" <<EOF |
| 69 | + set -e |
| 70 | + cd ~/${{secrets.PACKAGE_NAME}} |
| 71 | +
|
| 72 | + for file in scripts/generate-self-signed-cert.sh scripts/setup-nginx.sh; do |
| 73 | + if [ ! -f "$file" ]; then |
| 74 | + echo "❌ $file not found. Will copy from runner." |
| 75 | + exit 10 |
| 76 | + else |
| 77 | + echo "✅ $(basename "$file") found on VPS" |
| 78 | + fi |
| 79 | + done |
| 80 | + EOF |
| 81 | +
|
| 82 | + # Check exit code; if 10, then copy scripts directory |
| 83 | + if [ $? -eq 10 ]; then |
| 84 | + echo "📤 Copying scripts directory to VPS..." |
| 85 | + scp -r ./scripts deploy-server:~/${{secrets.PACKAGE_NAME}}/ |
| 86 | + fi |
| 87 | +
|
| 88 | + echo "🔐 Generate CERT" |
| 89 | + ssh deploy-server "cd ~/${{secrets.PACKAGE_NAME}}/scripts && chmod +x generate-self-signed-cert.sh && ./generate-self-signed-cert.sh" |
| 90 | +
|
| 91 | + echo "🚀 Running setup-nginx.sh on VPS..." |
| 92 | + ssh deploy-server "cd ~/${{secrets.PACKAGE_NAME}}/scripts && chmod +x setup-nginx.sh && ./setup-nginx.sh" |
0 commit comments