From 2f64ab918166def70a024f16fe961eb0fd637a41 Mon Sep 17 00:00:00 2001 From: Duy Tran Date: Wed, 10 Dec 2025 21:17:48 +0700 Subject: [PATCH 1/3] chore(): update show environment as branch name --- .github/workflows/railway-preview.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/railway-preview.yml b/.github/workflows/railway-preview.yml index e39144b..b850619 100644 --- a/.github/workflows/railway-preview.yml +++ b/.github/workflows/railway-preview.yml @@ -10,7 +10,7 @@ permissions: issues: write concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number }} + group: ${{ github.workflow }}-${{ github.head_ref }} cancel-in-progress: true jobs: @@ -28,7 +28,7 @@ jobs: railway_api_token: ${{ secrets.RAILWAY_API_TOKEN }} project_id: ${{ secrets.RAILWAY_PROJECT_ID }} environment_name: 'production' - preview_environment_name: 'pr-${{ github.event.pull_request.number }}' + preview_environment_name: 'pr-${{ github.head_ref }}' environment_variables: | { "DATABASE_URL": "postgres://username:password@hostname/db" @@ -40,7 +40,7 @@ jobs: echo "Preview deployed at ${{ steps.deploy.outputs.service_domain }}" - name: Post or update deployment comment - if: ${{ steps.test-action.outputs.service_domain != '' }} + if: ${{ steps.deploy.outputs.service_domain != '' }} uses: actions/github-script@v7 with: script: | @@ -66,7 +66,7 @@ jobs: ${marker} 🚀 **Deployment success!** - - \`web\` deployed at [${{ steps.test-action.outputs.service_domain }}](https://${{ steps.test-action.outputs.service_domain }}) + - \`web\` deployed at [${{ steps.deploy.outputs.service_domain }}](https://${{ steps.deploy.outputs.service_domain }}) --- @@ -110,6 +110,6 @@ jobs: railway_api_token: ${{ secrets.RAILWAY_API_TOKEN }} project_id: ${{ secrets.RAILWAY_PROJECT_ID }} environment_name: production - preview_environment_name: 'pr-${{ github.event.pull_request.number }}' + preview_environment_name: 'pr-${{ github.head_ref }}' branch_name: ${{ github.head_ref }} cleanup: 'true' \ No newline at end of file From 7ea92975c22178720e4ce0c5422a7410992d5177 Mon Sep 17 00:00:00 2001 From: Duy Tran Date: Wed, 10 Dec 2025 21:20:22 +0700 Subject: [PATCH 2/3] chore(): update feature name --- .github/workflows/railway-preview.yml | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/.github/workflows/railway-preview.yml b/.github/workflows/railway-preview.yml index b850619..f734c91 100644 --- a/.github/workflows/railway-preview.yml +++ b/.github/workflows/railway-preview.yml @@ -22,13 +22,21 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 + - name: Sanitize branch name + id: sanitize + run: | + BRANCH_NAME="${{ github.head_ref }}" + # Convert to lowercase, replace invalid chars with hyphens, remove consecutive hyphens, trim hyphens + SANITIZED=$(echo "$BRANCH_NAME" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]/-/g' | sed 's/-\+/-/g' | sed 's/^-\|-$//g') + echo "sanitized=$SANITIZED" >> $GITHUB_OUTPUT + - name: Deploy to Railway uses: ayungavis/railway-preview-deploy@v1.0.2 with: railway_api_token: ${{ secrets.RAILWAY_API_TOKEN }} project_id: ${{ secrets.RAILWAY_PROJECT_ID }} environment_name: 'production' - preview_environment_name: 'pr-${{ github.head_ref }}' + preview_environment_name: 'pr-${{ steps.sanitize.outputs.sanitized }}' environment_variables: | { "DATABASE_URL": "postgres://username:password@hostname/db" @@ -104,12 +112,20 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 + - name: Sanitize branch name + id: sanitize + run: | + BRANCH_NAME="${{ github.head_ref }}" + # Convert to lowercase, replace invalid chars with hyphens, remove consecutive hyphens, trim hyphens + SANITIZED=$(echo "$BRANCH_NAME" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]/-/g' | sed 's/-\+/-/g' | sed 's/^-\|-$//g') + echo "sanitized=$SANITIZED" >> $GITHUB_OUTPUT + - name: Cleanup Railway Environment uses: ayungavis/railway-preview-deploy@v1.0.2 with: railway_api_token: ${{ secrets.RAILWAY_API_TOKEN }} project_id: ${{ secrets.RAILWAY_PROJECT_ID }} environment_name: production - preview_environment_name: 'pr-${{ github.head_ref }}' + preview_environment_name: 'pr-${{ steps.sanitize.outputs.sanitized }}' branch_name: ${{ github.head_ref }} cleanup: 'true' \ No newline at end of file From f54d8064a3df92c21530ca20f1e156b37e0d85bf Mon Sep 17 00:00:00 2001 From: Duy Tran Date: Wed, 10 Dec 2025 21:23:49 +0700 Subject: [PATCH 3/3] chore(): update branch name --- .github/workflows/railway-preview.yml | 44 +++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/.github/workflows/railway-preview.yml b/.github/workflows/railway-preview.yml index f734c91..1bc12b1 100644 --- a/.github/workflows/railway-preview.yml +++ b/.github/workflows/railway-preview.yml @@ -26,8 +26,24 @@ jobs: id: sanitize run: | BRANCH_NAME="${{ github.head_ref }}" - # Convert to lowercase, replace invalid chars with hyphens, remove consecutive hyphens, trim hyphens - SANITIZED=$(echo "$BRANCH_NAME" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]/-/g' | sed 's/-\+/-/g' | sed 's/^-\|-$//g') + # Create a short hash of the branch name for uniqueness + HASH=$(echo -n "$BRANCH_NAME" | sha256sum | cut -c1-8) + # Sanitize branch name: lowercase, alphanumeric and hyphens only, max 20 chars + SANITIZED_BRANCH=$(echo "$BRANCH_NAME" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]/-/g' | sed 's/-\+/-/g' | sed 's/^-\|-$//g' | cut -c1-20 | sed 's/-$//') + # Combine: pr-{short-branch}-{hash} but limit total to 30 chars + if [ -n "$SANITIZED_BRANCH" ] && [[ "$SANITIZED_BRANCH" =~ ^[a-z] ]]; then + # Use branch name if valid and starts with letter + SANITIZED="${SANITIZED_BRANCH}-${HASH}" + else + # Fallback to PR number with hash + SANITIZED="pr${{ github.event.pull_request.number }}-${HASH}" + fi + # Ensure it starts with a letter and limit to 30 characters + if [[ ! "$SANITIZED" =~ ^[a-z] ]]; then + SANITIZED="e${SANITIZED}" + fi + SANITIZED="${SANITIZED:0:30}" + SANITIZED="${SANITIZED%-}" echo "sanitized=$SANITIZED" >> $GITHUB_OUTPUT - name: Deploy to Railway @@ -36,7 +52,7 @@ jobs: railway_api_token: ${{ secrets.RAILWAY_API_TOKEN }} project_id: ${{ secrets.RAILWAY_PROJECT_ID }} environment_name: 'production' - preview_environment_name: 'pr-${{ steps.sanitize.outputs.sanitized }}' + preview_environment_name: '${{ steps.sanitize.outputs.sanitized }}' environment_variables: | { "DATABASE_URL": "postgres://username:password@hostname/db" @@ -116,8 +132,24 @@ jobs: id: sanitize run: | BRANCH_NAME="${{ github.head_ref }}" - # Convert to lowercase, replace invalid chars with hyphens, remove consecutive hyphens, trim hyphens - SANITIZED=$(echo "$BRANCH_NAME" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]/-/g' | sed 's/-\+/-/g' | sed 's/^-\|-$//g') + # Create a short hash of the branch name for uniqueness + HASH=$(echo -n "$BRANCH_NAME" | sha256sum | cut -c1-8) + # Sanitize branch name: lowercase, alphanumeric and hyphens only, max 20 chars + SANITIZED_BRANCH=$(echo "$BRANCH_NAME" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]/-/g' | sed 's/-\+/-/g' | sed 's/^-\|-$//g' | cut -c1-20 | sed 's/-$//') + # Combine: pr-{short-branch}-{hash} but limit total to 30 chars + if [ -n "$SANITIZED_BRANCH" ] && [[ "$SANITIZED_BRANCH" =~ ^[a-z] ]]; then + # Use branch name if valid and starts with letter + SANITIZED="${SANITIZED_BRANCH}-${HASH}" + else + # Fallback to PR number with hash + SANITIZED="pr${{ github.event.pull_request.number }}-${HASH}" + fi + # Ensure it starts with a letter and limit to 30 characters + if [[ ! "$SANITIZED" =~ ^[a-z] ]]; then + SANITIZED="e${SANITIZED}" + fi + SANITIZED="${SANITIZED:0:30}" + SANITIZED="${SANITIZED%-}" echo "sanitized=$SANITIZED" >> $GITHUB_OUTPUT - name: Cleanup Railway Environment @@ -126,6 +158,6 @@ jobs: railway_api_token: ${{ secrets.RAILWAY_API_TOKEN }} project_id: ${{ secrets.RAILWAY_PROJECT_ID }} environment_name: production - preview_environment_name: 'pr-${{ steps.sanitize.outputs.sanitized }}' + preview_environment_name: '${{ steps.sanitize.outputs.sanitized }}' branch_name: ${{ github.head_ref }} cleanup: 'true' \ No newline at end of file