a year ago
I run my staging.yml file when deploying to staging environment using GitHub Actions. I first created a new environment variable manually using the Railway UI: COMMIT_HASH. Here is part of the staging.yml file:
- name: Capture Commit Hash
run: echo "COMMIT_HASH=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
- name: Log Commit Hash (verify it's set)
run: echo "env.COMMIT_HASH:${{ env.COMMIT_HASH }}"
I can see the expected git commit hash in the Action log when it runs. But when completed, I look at the COMMIT_HASH environment variable and it is blank. I'm guessing I need to set that variable here in the .yml file? I was hoping there's a railway cli command but I can only see a way to show the variables. Do I need to use the public API, or am I overthinking this?
21 Replies
a year ago
Hm, are the action logs the build logs? You should be able to set a variable when creating the service: https://docs.railway.com/reference/cli-api#add using the -v flag
Status changed to Awaiting User Response Railway • about 1 year ago
a year ago
Yes, the GitHub action produces the build log. I see from the doc that railway variables has a --set option that lets me set an environment variable. I think this is what I need. So, I added this to my staging.yml file:
- name: Set COMMIT_HASH Environment Variable
run: |
set -e
railway variables --set "COMMIT_HASH=${{ env.COMMIT_HASH }}"
but I got an error:
Run set -e
2 set -e
3 railway variables --set "COMMIT_HASH=5e2d7e8"
4 shell: /usr/bin/bash -e {0}
5 env:
6 COMMIT_HASH: 5e2d7e8
7Unauthorized. Please login with railway login
8Error: Process completed with exit code 1.
Am I on the right path? I'm building out my staging.yml, which github actions runs whenever I commit to main branch. I want to set an environment variable COMMIT_HASH to a vaule obtained earlier in the .yml file, which does seem to work. I ran railway CLI from a bash command line and I was able to log in interactively and list variables with railway variables. But, when I tried using the --set option, it said "unexpected argument '--set'. But the documentation does list a --set argument:
So, how would you suggest I accomplish this? Thanks.
Attachments
Status changed to Awaiting Railway Response Railway • about 1 year ago
a year ago
One more thought. The service is already created. The staging.yml file is my CI/CD process. I'm including here for clarity:
name: Deploy to Staging
on:
push:
branches:
- main # Trigger on pushes to the main branch
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install Railway CLI
run: npm install -g @railway/cli
- name: Install Dependencies
run: npm install
- name: Capture Commit Hash
run: echo "COMMIT_HASH=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
- name: Log Commit Hash (verify it's set)
run: echo "env.COMMIT_HASH:${{ env.COMMIT_HASH }}"
- name: Run Unit Tests
run: npm test
env:
DATABASE_URL: ${{ secrets.DATABASE_URL_STAGING }}
- name: Deploy to Railway Staging
run: |
set -e
railway up --environment staging --service g3tools-v2-api
env:
RAILWAY_TOKEN: ${{ secrets.RAILWAY_API_STAGING_TOKEN }}
- name: Set COMMIT_HASH Environment Variable
run: |
set -e
railway variables --set "COMMIT_HASH=${{ env.COMMIT_HASH }}"
- name: Run Database Migrations
run: |
set -e
npm install -g prisma
# Run Prisma migrations
DATABASE_URL="${{ secrets.DATABASE_URL_STAGING }}" npx prisma migrate deploy
- name: Run Non-Table SQL Scripts
run: |
set -e
for sql_file in ./db/non-table-sql/*.sql; do
echo "Running $sql_file"
mysql -h ${{ secrets.DB_HOST }} -P ${{ secrets.DB_PORT }} -u ${{ secrets.DB_USER }} -p${{ secrets.DB_PASSWORD_PRODUCTION }} g3tools_v2_test < "$sql_file"
done
- name: Run API Tests with Newman
run: |
set -e
npm install -g newman
newman run "./postman/API Tests.postman_collection.json" -e "./postman/test.postman_environment.json" --delay-request 100 --bail
env:
DATABASE_URL: ${{ secrets.DATABASE_URL_STAGING }}
AUTH_TOKEN: ${{ secrets.AUTH_TOKEN }}
- name: "Log Environment Variables (debug) at end"
run: printenv
a year ago
Hello,
The --set flag is relatively new to the CLI API, are you on the latest version?
As for the unauth error, what kind of token are you using?
Status changed to Awaiting User Response Railway • about 1 year ago
a year ago
I have 3.14.0 CLI on my PC. But in my staging.yml, I have a step that installs the CLI:
- name: Install Railway CLI
run: npm install -g @railway/cli
That runs in GitHub so I'm guessing it will install the latest version. On my local machine, I see I was running 3.14.0. I just re-installed and now running 3.20.1. So, I'm assuming it will now have the --set argument. I just need to log into railway.
I just logged into railway on my local machine. Did railway login, chose "no" to open website, followed the link, verified, closed the website, went back into bash terminal and it said I was logged in. I ran the railway variables --set "COMMIT_HASH=xxxxxxxx" and it worked. I did railway variables and saw the new environment variable set.
So, this works from my local machine. I need to log into railway in the staging.yml. Do I used the secrets.RAILWAY_API_STAGING_TOKEN that I'm using to railway up command (see above)? I don't remember exactly how I got the token (obviously from railway site). Thanks.
Status changed to Awaiting Railway Response Railway • about 1 year ago
a year ago
Not sure I can answer what kind of token I'm using. It is a GUID, if that helps. The main problem I need help with now is how can I do this through the staging.yml and the CI/CD process, unattended. I see in the CLI login doc that --browserless still requires you go to a webpage and verify. I just need to set an environment variable in an existing deployment as part of CI/CD. If there's a better way, I'd appreciate your suggestions. Thanks again.
a year ago
Please see this docs section for how you can authenticate with the CLI in CI/CD environments -
Status changed to Awaiting User Response Railway • about 1 year ago
brody
Please see this docs section for how you can authenticate with the CLI in CI/CD environments -https://docs.railway.com/guides/cli#tokens
a year ago
Isn't that what I'm doing here in the .yml file:
- name: Deploy to Railway Staging
run: |
set -e
railway up --environment staging --service g3tools-v2-api
env:
RAILWAY_TOKEN: ${{ secrets.RAILWAY_API_STAGING_TOKEN }}Status changed to Awaiting Railway Response Railway • about 1 year ago
Status changed to Awaiting User Response Railway • about 1 year ago
a year ago
Yes. After running railway up, I attempt to change the environment variable with:
- name: Set COMMIT_HASH Environment Variable
run: |
set -e
railway variables --set "COMMIT_HASH=${{ env.COMMIT_HASH }}"
and I receive the error:
Run set -e
set -e
railway variables --set "COMMIT_HASH=1f64ea0"
shell: /usr/bin/bash -e {0}
env:
COMMIT_HASH: 1f64ea0
Unauthorized. Please login with `railway login`
Error: Process completed with exit code 1.Status changed to Awaiting Railway Response Railway • about 1 year ago
Status changed to Awaiting User Response Railway • about 1 year ago
a year ago
Not sure I follow. Do I create another token for the account? Where do I do that? Then how would I use it in the yml file, like:
- name: Set COMMIT_HASH Environment Variable
run: |
set -e
railway variables --set "COMMIT_HASH=${{ env.COMMIT_HASH }}"
env:
RAILWAY_TOKEN: ${{ secrets.RAILWAY_API_STAGING_TOKEN }}
I'm guessing here. Can you please provide an example? Thanks.
Status changed to Awaiting Railway Response Railway • about 1 year ago
a year ago
The docs here talk about an account token -
https://docs.railway.com/guides/cli#tokens
There is also more information on them here -
https://docs.railway.com/reference/public-api#personal-token
Status changed to Awaiting User Response Railway • about 1 year ago
a year ago
I tried that and got:
Run set -e
2 set -e
3 railway variables --set "COMMIT_HASH=1f25e14"
4 shell: /usr/bin/bash -e {0}
5 env:
6 COMMIT_HASH: 1f25e14
7 RAILWAY_TOKEN: ***
8No service linked
9Run railway service to link a service
10Error: Process completed with exit code 1.
Am I getting warmer? Can I run railway service in the .yml file unattended? I know I can run it interactively and choose an option. Something like:
railway link "g3tools-v2-api"
Status changed to Awaiting Railway Response Railway • about 1 year ago
a year ago
Did you try to specify the environment and service via flags? you shouldn't need to use link.
Status changed to Awaiting User Response Railway • about 1 year ago
a year ago
trying now. The key seems to be I need to pass the environment and service as part of each railway command, along with the token?
Status changed to Awaiting Railway Response Railway • about 1 year ago
Status changed to Awaiting User Response Railway • about 1 year ago
a year ago
This seems to have worked:
- name: Set COMMIT_HASH Environment Variable
run: |
set -e
railway variables --environment staging --service g3tools-v2-api --set "COMMIT_HASH=${{ env.COMMIT_HASH }}"
env:
RAILWAY_TOKEN: ${{ secrets.RAILWAY_API_STAGING_TOKEN }}Thanks.
Status changed to Awaiting Railway Response Railway • about 1 year ago
Status changed to Awaiting User Response Railway • about 1 year ago
a year ago
I didn't try the project token, just the api token above.
Status changed to Awaiting Railway Response Railway • about 1 year ago
Status changed to Awaiting User Response Railway • about 1 year ago
Status changed to Solved brody • about 1 year ago