|
| 1 | +name: Deploy Staging or Prod - Entire Application |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + branch: |
| 7 | + description: "Select a Branch to deploy" |
| 8 | + required: true |
| 9 | + default: "main" |
| 10 | + environment: |
| 11 | + description: "Select environment to deploy (staging or prod)" |
| 12 | + required: true |
| 13 | + default: "staging" |
| 14 | + email: |
| 15 | + description: "Admin email for deployment" |
| 16 | + required: true |
| 17 | + default: "saravana.k.r@gmail.com" |
| 18 | + commit_id: |
| 19 | + description: "Commit ID to deploy (leave blank for latest on branch)" |
| 20 | + required: false |
| 21 | + default: "" |
| 22 | + |
| 23 | +env: |
| 24 | + STAGING_HOST: staging.visualaiagentsbuilder.com |
| 25 | + PROD_HOST: visualaiagentsbuilder.com |
| 26 | + |
| 27 | +jobs: |
| 28 | + build-and-push: |
| 29 | + runs-on: ubuntu-latest |
| 30 | + |
| 31 | + steps: |
| 32 | + - name: Checkout selected branch |
| 33 | + uses: actions/checkout@v4 |
| 34 | + with: |
| 35 | + ref: ${{ github.event.inputs.branch }} |
| 36 | + |
| 37 | + - name: Checkout specific commit if provided |
| 38 | + if: ${{ github.event.inputs.commit_id != '' }} |
| 39 | + run: | |
| 40 | + git fetch --depth=1 origin ${{ github.event.inputs.commit_id }} |
| 41 | + git checkout ${{ github.event.inputs.commit_id }} |
| 42 | + echo "✅ Checked out commit ${{ github.event.inputs.commit_id }}" |
| 43 | +
|
| 44 | + - name: Determine Commit ID |
| 45 | + id: commit |
| 46 | + run: | |
| 47 | + if [ -n "${{ github.event.inputs.commit_id }}" ]; then |
| 48 | + echo "commit_id=${{ github.event.inputs.commit_id }}" >> $GITHUB_OUTPUT |
| 49 | + else |
| 50 | + COMMIT_ID=$(git rev-parse --short HEAD) |
| 51 | + echo "commit_id=$COMMIT_ID" >> $GITHUB_OUTPUT |
| 52 | + fi |
| 53 | +
|
| 54 | + - name: Determine Branch Name |
| 55 | + id: branch |
| 56 | + run: | |
| 57 | + BRANCH_NAME=$(echo "${{ github.event.inputs.branch }}" | tr '/' '-') |
| 58 | + echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT |
| 59 | + |
| 60 | + - name: Install sshpass |
| 61 | + run: sudo apt-get update && sudo apt-get install -y sshpass |
| 62 | + |
| 63 | + - name: Create GitHub Deployment |
| 64 | + env: |
| 65 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 66 | + run: | |
| 67 | + COMMIT_ID=${{ steps.commit.outputs.commit_id }} |
| 68 | + ENVIRONMENT=${{ github.event.inputs.environment }} |
| 69 | + BRANCH=${{ github.event.inputs.branch }} |
| 70 | + ACTOR=${{ github.actor }} |
| 71 | +
|
| 72 | + echo "📦 Creating deployment for $ENVIRONMENT commit=$COMMIT_ID branch=$BRANCH" |
| 73 | +
|
| 74 | + DEPLOYMENT_ID=$(gh api repos/${{ github.repository }}/deployments \ |
| 75 | + -F ref=$COMMIT_ID \ |
| 76 | + -F environment=$ENVIRONMENT \ |
| 77 | + -F description="Deployment by $ACTOR from branch $BRANCH with commit $COMMIT_ID" \ |
| 78 | + -F auto_merge=false --jq '.id') |
| 79 | +
|
| 80 | + echo "DEPLOYMENT_ID=$DEPLOYMENT_ID" >> $GITHUB_ENV |
| 81 | +
|
| 82 | + - name: Mark Deployment In Progress |
| 83 | + env: |
| 84 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 85 | + run: | |
| 86 | + gh api repos/${{ github.repository }}/deployments/${{ env.DEPLOYMENT_ID }}/statuses \ |
| 87 | + -F state=in_progress \ |
| 88 | + -F description="Deployment started and is in progress" |
| 89 | +
|
| 90 | + - name: Set up Docker Buildx |
| 91 | + uses: docker/setup-buildx-action@v3 |
| 92 | + |
| 93 | + - name: Log in to Docker Hub |
| 94 | + uses: docker/login-action@v3 |
| 95 | + with: |
| 96 | + username: forwardemailforaifirstdesk |
| 97 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 98 | + |
| 99 | + - name: Build Docker Image (for staging) |
| 100 | + if: ${{ github.event.inputs.environment == 'staging' }} |
| 101 | + run: | |
| 102 | + COMMIT_ID=${{ steps.commit.outputs.commit_id }} |
| 103 | + BRANCH_NAME=${{ steps.branch.outputs.branch_name }} |
| 104 | + echo "Using branch=$BRANCH_NAME commit=$COMMIT_ID" |
| 105 | +
|
| 106 | + echo "=== Building Staging Image ===" |
| 107 | + DOCKER_BUILDKIT=1 \ |
| 108 | + VITE_AUTO_LOGIN=false \ |
| 109 | + VITE_CLERK_AUTH_ENABLED=true \ |
| 110 | + VITE_CLERK_PUBLISHABLE_KEY=${{ secrets.CLERK_PUBLISHABLE_KEY }} \ |
| 111 | + make docker_build TAG=langflow:staging-$BRANCH_NAME-$COMMIT_ID |
| 112 | +
|
| 113 | + docker tag langflow:staging-$BRANCH_NAME-$COMMIT_ID forwardemailforaifirstdesk/langflow:staging-$BRANCH_NAME-$COMMIT_ID |
| 114 | + docker push forwardemailforaifirstdesk/langflow:staging-$BRANCH_NAME-$COMMIT_ID |
| 115 | +
|
| 116 | + echo "=== Building Prod Image ===" |
| 117 | + DOCKER_BUILDKIT=1 \ |
| 118 | + VITE_AUTO_LOGIN=false \ |
| 119 | + VITE_CLERK_AUTH_ENABLED=true \ |
| 120 | + VITE_CLERK_PUBLISHABLE_KEY=${{ secrets.PROD_CLERK_PUBLISHABLE_KEY }} \ |
| 121 | + make docker_build TAG=langflow:prod-$BRANCH_NAME-$COMMIT_ID |
| 122 | + |
| 123 | + docker tag langflow:prod-$BRANCH_NAME-$COMMIT_ID forwardemailforaifirstdesk/langflow:prod-$BRANCH_NAME-$COMMIT_ID |
| 124 | + docker push forwardemailforaifirstdesk/langflow:prod-$BRANCH_NAME-$COMMIT_ID |
| 125 | +
|
| 126 | + - name: Cleanup old Docker Hub tags (skip for prod) |
| 127 | + if: ${{ github.event.inputs.environment == 'staging' }} |
| 128 | + run: | |
| 129 | + #!/usr/bin/env bash |
| 130 | + set -euo pipefail |
| 131 | +
|
| 132 | + DOCKERHUB_USERNAME="forwardemailforaifirstdesk" |
| 133 | + DOCKERHUB_PASSWORD="${{ secrets.DOCKERHUB_TOKEN }}" |
| 134 | + REPOSITORY="forwardemailforaifirstdesk/langflow" |
| 135 | +
|
| 136 | + echo "=== Getting Docker Hub token ===" |
| 137 | + TOKEN=$(curl -s -H "Content-Type: application/json" -X POST \ |
| 138 | + -d '{"username": "'$DOCKERHUB_USERNAME'", "password": "'$DOCKERHUB_PASSWORD'"}' \ |
| 139 | + https://hub.docker.com/v2/users/login/ | jq -r .token) |
| 140 | +
|
| 141 | + if [ -z "$TOKEN" ] || [ "$TOKEN" == "null" ]; then |
| 142 | + echo "❌ Failed to authenticate" |
| 143 | + exit 1 |
| 144 | + fi |
| 145 | + echo "✅ Authentication successful" |
| 146 | +
|
| 147 | + echo "=== Fetching tags ===" |
| 148 | + RESPONSE=$(curl -s -H "Authorization: JWT $TOKEN" \ |
| 149 | + "https://hub.docker.com/v2/repositories/$REPOSITORY/tags?page_size=100") |
| 150 | +
|
| 151 | + TAGS=$(echo "$RESPONSE" | jq -r '.results[] | .name') |
| 152 | +
|
| 153 | + if [ -z "$TAGS" ]; then |
| 154 | + echo "ℹ️ No tags found in repository $REPOSITORY. Nothing to delete." |
| 155 | + exit 0 |
| 156 | + fi |
| 157 | +
|
| 158 | + echo "Found tags: $TAGS" |
| 159 | +
|
| 160 | + for TAG in $TAGS; do |
| 161 | + COMMIT_ID=${{ steps.commit.outputs.commit_id }} |
| 162 | + BRANCH_NAME=${{ steps.branch.outputs.branch_name }} |
| 163 | + if [[ "$TAG" == "staging-$BRANCH_NAME-$COMMIT_ID" || "$TAG" == "prod-$BRANCH_NAME-$COMMIT_ID" ]]; then |
| 164 | + echo "⏭️ Skipping current branch+commit tag: $TAG" |
| 165 | + continue |
| 166 | + fi |
| 167 | +
|
| 168 | + echo "Deleting old tag: $TAG" |
| 169 | + DELETE_RESPONSE=$(curl -s -w "HTTPSTATUS:%{http_code}" \ |
| 170 | + -X DELETE -H "Authorization: JWT $TOKEN" \ |
| 171 | + "https://hub.docker.com/v2/repositories/$REPOSITORY/tags/$TAG/") |
| 172 | +
|
| 173 | + HTTP_CODE=$(echo $DELETE_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://') |
| 174 | +
|
| 175 | + if [ "$HTTP_CODE" = "204" ]; then |
| 176 | + echo " ✅ Successfully deleted tag $TAG" |
| 177 | + else |
| 178 | + echo " ❌ Failed to delete tag $TAG (HTTP: $HTTP_CODE)" |
| 179 | + fi |
| 180 | +
|
| 181 | + sleep 1 |
| 182 | + done |
| 183 | +
|
| 184 | + echo "Note: Docker Hub will automatically clean up unreferenced images within 24-48 hours" |
| 185 | +
|
| 186 | + - name: If env is staging Move script to VM |
| 187 | + if: ${{ github.event.inputs.environment == 'staging' }} |
| 188 | + uses: appleboy/scp-action@v0.1.7 |
| 189 | + with: |
| 190 | + host: ${{env.STAGING_HOST}} |
| 191 | + username: root |
| 192 | + password: ${{ secrets.VM_PASSWORD }} |
| 193 | + source: "./deploy/deploy-stage-prod.sh,./.env-file" |
| 194 | + target: /root/ |
| 195 | + |
| 196 | + - name: If env is prod Move script to VM |
| 197 | + if: ${{ github.event.inputs.environment == 'prod' }} |
| 198 | + uses: appleboy/scp-action@v0.1.7 |
| 199 | + with: |
| 200 | + host: ${{env.PROD_HOST}} |
| 201 | + username: root |
| 202 | + password: ${{ secrets.PROD_VM_PASSWORD }} |
| 203 | + source: "./deploy/deploy-stage-prod.sh,./.env-file" |
| 204 | + target: /root/ |
| 205 | + |
| 206 | + - name: If env is staging Run script on VM |
| 207 | + if: ${{ github.event.inputs.environment == 'staging' }} |
| 208 | + uses: appleboy/ssh-action@v1.1.0 |
| 209 | + with: |
| 210 | + host: ${{env.STAGING_HOST}} |
| 211 | + username: root |
| 212 | + password: ${{ secrets.VM_PASSWORD }} |
| 213 | + script: | |
| 214 | + chmod +x /root/deploy/deploy-stage-prod.sh |
| 215 | + /root/deploy/deploy-stage-prod.sh \ |
| 216 | + --image forwardemailforaifirstdesk/langflow:staging-${{ steps.branch.outputs.branch_name }}-${{ steps.commit.outputs.commit_id }} \ |
| 217 | + --domain ${{env.STAGING_HOST}} \ |
| 218 | + --email ${{ github.event.inputs.email }} \ |
| 219 | + --env-file /root/.env-file |
| 220 | +
|
| 221 | + - name: If env is prod Run script on VM |
| 222 | + if: ${{ github.event.inputs.environment == 'prod' }} |
| 223 | + uses: appleboy/ssh-action@v1.1.0 |
| 224 | + with: |
| 225 | + host: ${{env.PROD_HOST}} |
| 226 | + username: root |
| 227 | + password: ${{ secrets.PROD_VM_PASSWORD }} |
| 228 | + script: | |
| 229 | + chmod +x /root/deploy/deploy-stage-prod.sh |
| 230 | + /root/deploy/deploy-stage-prod.sh \ |
| 231 | + --image forwardemailforaifirstdesk/langflow:prod-${{ steps.branch.outputs.branch_name }}-${{ steps.commit.outputs.commit_id }} \ |
| 232 | + --domain ${{env.PROD_HOST}} \ |
| 233 | + --email ${{ github.event.inputs.email }} \ |
| 234 | + --env-file /root/.env-file |
| 235 | +
|
| 236 | + - name: Copy deploy_status.env from VM |
| 237 | + run: | |
| 238 | + sshpass -p "${{ secrets.VM_PASSWORD }}" scp -o StrictHostKeyChecking=no \ |
| 239 | + root@staging.visualaiagentsbuilder.com:/root/deploy_status.env \ |
| 240 | + $GITHUB_WORKSPACE/deploy_status.env |
| 241 | +
|
| 242 | + - name: Update GitHub Deployment Status with Docker info |
| 243 | + if: always() |
| 244 | + env: |
| 245 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 246 | + run: | |
| 247 | + source "${GITHUB_WORKSPACE}/deploy_status.env" |
| 248 | + gh api repos/${{ github.repository }}/deployments/${{ env.DEPLOYMENT_ID }}/statuses \ |
| 249 | + -F state=$FINAL_STATUS \ |
| 250 | + -F description="$FINAL_DESCRIPTION" |
0 commit comments