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 Main Dashboard | |
| on: | |
| push: | |
| branches: [ dev, main ] | |
| paths: | |
| - 'apps/main-dashboard/**' | |
| - 'Dockerfile.main-dashboard' | |
| - 'Dockerfile.main-dashboard.staging' | |
| - 'packages/shared-models/**' | |
| - '.github/workflows/deploy-main-dashboard.yml' | |
| pull_request: | |
| types: [ closed ] | |
| branches: [ main ] | |
| paths: | |
| - 'apps/main-dashboard/**' | |
| - 'Dockerfile.main-dashboard' | |
| - 'Dockerfile.main-dashboard.staging' | |
| - 'packages/shared-models/**' | |
| - '.github/workflows/deploy-main-dashboard.yml' | |
| workflow_dispatch: | |
| jobs: | |
| build-and-deploy: | |
| name: Build & Deploy Dashboard | |
| runs-on: ubuntu-latest | |
| if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.pull_request.merged == true) | |
| environment: | |
| name: ${{ github.ref_name == 'main' && 'production' || 'staging' }} | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Get commit info | |
| id: vars | |
| run: | | |
| echo "commit_id=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT | |
| echo "branch=${{ github.ref_name }}" >> $GITHUB_OUTPUT | |
| - name: Build, Push & Deploy | |
| uses: appleboy/ssh-action@v1.0.3 | |
| with: | |
| host: ${{ secrets.SERVER_HOST }} | |
| username: ${{ secrets.SERVER_USER }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| script: | | |
| set -e | |
| # Variables | |
| REPO_DIR="/root/idem" | |
| BRANCH="${{ steps.vars.outputs.branch }}" | |
| COMMIT_ID="${{ steps.vars.outputs.commit_id }}" | |
| # Different tags for staging and production | |
| if [ "$BRANCH" = "main" ] || [ "${{ github.event_name }}" = "pull_request" ]; then | |
| IMAGE_TAG="ghcr.io/idem-ai/idem-main-dashboard:$COMMIT_ID" | |
| else | |
| IMAGE_TAG="ghcr.io/idem-ai/idem-main-dashboard:$COMMIT_ID-staging" | |
| fi | |
| echo "Branch: $BRANCH" | |
| echo "Commit: $COMMIT_ID" | |
| echo "Image: $IMAGE_TAG" | |
| # Update repository | |
| cd $REPO_DIR | |
| # Clean git locks and corrupted index (parallel workflows) | |
| rm -f .git/index.lock | |
| rm -f .git/index | |
| # Retry loop for git operations (avoid parallel conflicts) | |
| for i in {1..5}; do | |
| sleep $((RANDOM % 3 + 1)) | |
| if git checkout -f $BRANCH && git pull origin $BRANCH; then | |
| break | |
| fi | |
| echo "Retry $i/5..." | |
| rm -f .git/index.lock .git/index | |
| done | |
| # Build image | |
| echo "Building Docker image..." | |
| docker build -f Dockerfile.main-dashboard -t $IMAGE_TAG . | |
| # Push image | |
| echo "Pushing image to registry..." | |
| docker push $IMAGE_TAG | |
| # Determine environment and deploy | |
| if [ "$BRANCH" = "main" ] || [ "${{ github.event_name }}" = "pull_request" ]; then | |
| # Production deployment | |
| COMPOSE_FILE="docker-compose.prod.yml" | |
| SERVICE_NAME="idem" | |
| echo "Updating $COMPOSE_FILE..." | |
| sed -i "s|image: ghcr.io/idem-ai/idem-main-dashboard:.*|image: $IMAGE_TAG|" $COMPOSE_FILE | |
| echo "Deploying to production..." | |
| docker-compose -f $COMPOSE_FILE pull $SERVICE_NAME | |
| docker-compose -f $COMPOSE_FILE up -d $SERVICE_NAME | |
| else | |
| # Staging deployment | |
| COMPOSE_FILE="docker-compose.staging.yml" | |
| SERVICE_NAME="idem-staging" | |
| echo "Updating $COMPOSE_FILE..." | |
| sed -i "s|image: ghcr.io/idem-ai/idem-main-dashboard:.*|image: $IMAGE_TAG|" $COMPOSE_FILE | |
| echo "Deploying to staging..." | |
| docker-compose -f $COMPOSE_FILE pull $SERVICE_NAME | |
| docker-compose -f $COMPOSE_FILE up -d $SERVICE_NAME | |
| fi | |
| # Cleanup | |
| echo "Cleaning up old images..." | |
| docker image prune -f | |
| echo "Dashboard deployed successfully!" |