R
Railwayβ€’13mo ago
Sang Dang

How to get DATABASE_URL in Github Actions

When we create a PR, Railway automatic makes a new environment, says myproject-pr-40, inside we have Postgres service with new temporary DATABASE_URL variable, we want to have that value when running our Github Actions, is it possible?
name: Prisma Deploy

on:
pull_request:
paths:
- packages/database/prisma/**
types:
- opened
- synchronize

jobs:
prisma-deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup pnpm
uses: pnpm/action-setup@v2

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18
cache: "pnpm"

- name: Install dependencies
run: pnpm install

- name: Deploy Migrations
run: pnpm run migrate:deploy
env:
DATABASE_URL: ... <-- How to get Railway Postgres DATABASE_URL in `myproject-pr-40` environment here?
name: Prisma Deploy

on:
pull_request:
paths:
- packages/database/prisma/**
types:
- opened
- synchronize

jobs:
prisma-deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup pnpm
uses: pnpm/action-setup@v2

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18
cache: "pnpm"

- name: Install dependencies
run: pnpm install

- name: Deploy Migrations
run: pnpm run migrate:deploy
env:
DATABASE_URL: ... <-- How to get Railway Postgres DATABASE_URL in `myproject-pr-40` environment here?
Any hint is greatly appreciated. πŸ™
30 Replies
Percy
Percyβ€’13mo ago
Project ID: 28677a80-2aaf-46d7-8fff-e2898fa09eb1
Sang Dang
Sang Dangβ€’13mo ago
28677a80-2aaf-46d7-8fff-e2898fa09eb1
techmatic007
techmatic007β€’13mo ago
New>database >postgres
Sang Dang
Sang Dangβ€’13mo ago
@techmatic007 I don't understand what do you mean? I already have Postgres service running.
techmatic007
techmatic007β€’13mo ago
If you added postgres service on Railway project then add environment DATABASE_URL . It will automatically show postgres click on it.
Sang Dang
Sang Dangβ€’13mo ago
Thanks for quick response. The service already has the DATABASE_URL variable. I would like to get that value from inside our Github Actions.
techmatic007
techmatic007β€’13mo ago
oh I understand,idk @Brody will help you
techmatic007
techmatic007β€’13mo ago
BTW you can get url from postgres plugin variables.
Sang Dang
Sang Dangβ€’13mo ago
The struggle I'm facing is, we have Prisma, sometimes it has schema changes => migration need to be deployed. Our project monorepo structure:
./
/apps
/dashboard
/server
/packages
/database <-- has prisma here
/others
./
/apps
/dashboard
/server
/packages
/database <-- has prisma here
/others
For now, everything related to server works great. And we would like to have the prisma migrate deploy runs somewhere after the PR triggered. Thanks, but I do not want to manually copy/paste for every single PR I saw we have variables command in railway CLI, will play with it a bit
Brody
Brodyβ€’13mo ago
I very much do not appreciate being volunteered out like that, don't do that again please
Sang Dang
Sang Dangβ€’13mo ago
For now I use the CLI to get the value, but I face new issue, somehow it get wrong variable from production environment!
name: Prisma Preview

on:
pull_request:
branches:
- master
paths:
- packages/database/prisma/**
types:
- opened
- synchronize

env:
PR_NUMBER: ${{ github.event.number }}
RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}

jobs:
prisma-preview:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup pnpm
uses: pnpm/action-setup@v2

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18
cache: "pnpm"

- name: Install dependencies
run: pnpm i --frozen-lockfile

- name: Install Railway
run: npm i -g @railway/cli

- name: Activate Railway environment
run: railway environment myproject-pr-$PR_NUMBER

- name: Get Railway variables
id: railway
run: |
DATABASE_URL=$(railway variables -s server --json | sed -n 's/.*"DATABASE_URL": "\(.*\)".*/\1/p')
echo "DATABASE_URL=$DATABASE_URL" >> "$GITHUB_OUTPUT"

- name: Deploy Migrations
run: pnpm run migrate:deploy
env:
DATABASE_URL: ${{ steps.railway.outputs.DATABASE_URL }}
name: Prisma Preview

on:
pull_request:
branches:
- master
paths:
- packages/database/prisma/**
types:
- opened
- synchronize

env:
PR_NUMBER: ${{ github.event.number }}
RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}

jobs:
prisma-preview:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup pnpm
uses: pnpm/action-setup@v2

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18
cache: "pnpm"

- name: Install dependencies
run: pnpm i --frozen-lockfile

- name: Install Railway
run: npm i -g @railway/cli

- name: Activate Railway environment
run: railway environment myproject-pr-$PR_NUMBER

- name: Get Railway variables
id: railway
run: |
DATABASE_URL=$(railway variables -s server --json | sed -n 's/.*"DATABASE_URL": "\(.*\)".*/\1/p')
echo "DATABASE_URL=$DATABASE_URL" >> "$GITHUB_OUTPUT"

- name: Deploy Migrations
run: pnpm run migrate:deploy
env:
DATABASE_URL: ${{ steps.railway.outputs.DATABASE_URL }}
I checked the Github Actions output, it shows that the step Activate Railway environment run correctly. When I use the railway CLI locally, I can see the railway variables -s server return correct variable values for each environment. But in Github Actions, for some reason it always get production no matter what I try.
Brody
Brodyβ€’13mo ago
i have an incredably small amount of knowledge with gh actions, the best advice i could give would be to add some railway status commands here and there
Sang Dang
Sang Dangβ€’13mo ago
Thanks @Brody. I think this is because I use RAILWAY_TOKEN generated from production environment, making the railway variables -s server command ignores everything else. If this is the case then it's so hard for us to make Github Action to work with auto generated environment, as the token has to be generated manually.
Brody
Brodyβ€’13mo ago
that makes sense, can you use a global account token in this case?
Sang Dang
Sang Dangβ€’13mo ago
😍 oh do we have it? let me try
Brody
Brodyβ€’13mo ago
Sang Dang
Sang Dangβ€’13mo ago
😒 railway CLI uses only project token.
Sang Dang
Sang Dangβ€’13mo ago
Look like that global account token only used for GraphQL API at the moment sadcat
Brody
Brodyβ€’13mo ago
railway cli, at least on the desktop does use a global token, i guess just not that global token
Sang Dang
Sang Dangβ€’13mo ago
I can't find any place with another global token sadcat
Brody
Brodyβ€’13mo ago
best I could think of would be to pull it from the cli's config file on your desktop
Sang Dang
Sang Dangβ€’13mo ago
thanks @Brody for your patience, I got the user.token inside config.json but it failed with this error:
Login state is corrupt. Please logout and login back in.

Caused by:
failed to parse header value
Login state is corrupt. Please logout and login back in.

Caused by:
failed to parse header value
Brody
Brodyβ€’13mo ago
can you add a log to see what railway cli version github is installing?
Sang Dang
Sang Dangβ€’13mo ago
I ran npm info inside github action because the npm install does not show the version, I guess they get the same πŸ€·β€β™‚οΈ
Brody
Brodyβ€’13mo ago
3.3.1 is the latest
Sang Dang
Sang Dangβ€’13mo ago
I have to use the API token and query then manually find the IDs, it works for now.
- name: Install jq
run: sudo apt-get install -y jq

- name: Get Railway variables
id: railway
run: |
ENVIRONMENTS=$(curl --request POST \
--url https://backboard.railway.app/graphql/v2 \
--header "Authorization: Bearer $RAILWAY_API_TOKEN" \
--header "Content-Type: application/json" \
--data '{ "query": "query { environments(projectId: \"'$PROJECT_ID'\") { edges { node { id name }}}}" }')
ENVIRONMENT_ID=$(echo "$ENVIRONMENTS" | jq -r '.data.environments.edges[] | select(.node.name == "myproject-pr-'$PR_NUMBER'") | .node.id')
VARIABLES=$(curl --request POST \
--url https://backboard.railway.app/graphql/v2 \
--header "Authorization: Bearer $RAILWAY_API_TOKEN" \
--header "Content-Type: application/json" \
--data '{ "query": "query {variables: variables(projectId: \"'$PROJECT_ID'\" environmentId: \"'$ENVIRONMENT_ID'\" pluginId: \"'$PLUGIN_ID'\")}" }')
DATABASE_URL=$(echo "$VARIABLES" | jq -r '.data.variables.DATABASE_URL')
echo "DATABASE_URL=$DATABASE_URL" >> "$GITHUB_OUTPUT"

- name: Deploy Migrations
run: pnpm run migrate:deploy
env:
DATABASE_URL: ${{ steps.railway.outputs.DATABASE_URL }}
- name: Install jq
run: sudo apt-get install -y jq

- name: Get Railway variables
id: railway
run: |
ENVIRONMENTS=$(curl --request POST \
--url https://backboard.railway.app/graphql/v2 \
--header "Authorization: Bearer $RAILWAY_API_TOKEN" \
--header "Content-Type: application/json" \
--data '{ "query": "query { environments(projectId: \"'$PROJECT_ID'\") { edges { node { id name }}}}" }')
ENVIRONMENT_ID=$(echo "$ENVIRONMENTS" | jq -r '.data.environments.edges[] | select(.node.name == "myproject-pr-'$PR_NUMBER'") | .node.id')
VARIABLES=$(curl --request POST \
--url https://backboard.railway.app/graphql/v2 \
--header "Authorization: Bearer $RAILWAY_API_TOKEN" \
--header "Content-Type: application/json" \
--data '{ "query": "query {variables: variables(projectId: \"'$PROJECT_ID'\" environmentId: \"'$ENVIRONMENT_ID'\" pluginId: \"'$PLUGIN_ID'\")}" }')
DATABASE_URL=$(echo "$VARIABLES" | jq -r '.data.variables.DATABASE_URL')
echo "DATABASE_URL=$DATABASE_URL" >> "$GITHUB_OUTPUT"

- name: Deploy Migrations
run: pnpm run migrate:deploy
env:
DATABASE_URL: ${{ steps.railway.outputs.DATABASE_URL }}
I hope Railway can allow us to use personal token (or API token) in CLI so it can query variables from all environments.
Brody
Brodyβ€’13mo ago
that would be ideal, but I love your persistence and problem solving abilities
Sang Dang
Sang Dangβ€’13mo ago
thanks πŸ˜„ I will mark this as solved for now, but would be happy to revisit when CLI has updated
nflxshaki
nflxshakiβ€’13mo ago
I copied my postgres url from railway and added it to my github secrets for the project. You can call the env variable inside of actions using ${{secrets.DATABASE_URL}} I had to upgrade to github pro plan to use environment variables as well
Sang Dang
Sang Dangβ€’13mo ago
Thanks Shaki, but for automatically generated environment, the DATABASE_URL is different from the one in production environment. And I don’t want to manually copy/paste for every single new PR.