Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL="postgresql://postgres:123@localhost:5434/nest?schema=public"
JWT_SECRET = 'super-secret'
JWT_SECRET = 'super-secret'
PORT=3000
ENV_MESSAGE="Hello! I am NestJS"
4 changes: 3 additions & 1 deletion .env.test
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
DATABASE_URL="postgresql://postgres:123@localhost:5435/nest?schema=public"
JWT_SECRET = 'super-secret'
JWT_SECRET = 'super-secret'
PORT=3000
ENV_MESSAGE="Hello! I am NestJS"
66 changes: 57 additions & 9 deletions .github/workflows/railway-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -22,25 +22,49 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4

- name: Sanitize branch name
id: sanitize
run: |
BRANCH_NAME="${{ github.head_ref }}"
# 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
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.event.pull_request.number }}'
environment_variables: |
{
"DATABASE_URL": "postgres://username:password@hostname/db"
}
preview_environment_name: '${{ steps.sanitize.outputs.sanitized }}'
# environment_variables: |
# {
# "DATABASE_URL": "postgres://username:password@hostname/db"
# }
branch_name: ${{ github.head_ref }}

- name: Post deployment info
run:
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: |
Expand All @@ -66,7 +90,7 @@ jobs:
${marker} <!-- This marker helps identify the comment for future updates -->
🚀 **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 }})

---

Expand Down Expand Up @@ -104,12 +128,36 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4

- name: Sanitize branch name
id: sanitize
run: |
BRANCH_NAME="${{ github.head_ref }}"
# 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
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.event.pull_request.number }}'
preview_environment_name: '${{ steps.sanitize.outputs.sanitized }}'
branch_name: ${{ github.head_ref }}
cleanup: 'true'
2 changes: 1 addition & 1 deletion src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { Controller, Get } from '@nestjs/common';
export class AppController {
@Get()
getHello(): string {
return 'hello';
return process.env.ENV_MESSAGE || 'hello';
}
}