How can I access the DB_URL from GithubActions to FlyIO?

I am using the cron of GithubActions in the RAILS application to process the data storage in the DB.

env:
      RAILS_ENV: production
      DATABASE_URL: ${{ secrets.DATABASE_URL }}
      SECRET_KEY_BASE: ${{ secrets.SECRET_KEY_BASE }}

DATABASE_URL has already been set in Github’s secrets.
To retrieve the values, the following steps were performed:

❯ fly ssh console
Connecting to xxx... complete
root@yyy:/rails# echo $DATABASE_URL
postgres://myapp:string@myappn-db.flycast:5432/myapp?sslmode=disable

But the following error occurred when executing GithubActions cron.

rake aborted!
ActiveRecord::NoDatabaseError: We could not find your database: bookrin. Available database configurations can be found in config/database.yml. (ActiveRecord::NoDatabaseError)

To resolve this error:

- Did you not create the database, or did you delete it? To create the database, run:

    bin/rails db:create

- Has the database name changed? Verify that config/database.yml contains the correct database name.
/home/runner/work/BookRIn/BookRIn/app/models/reading_circles_client.rb:37:in `block in update_clubs'
/home/runner/work/BookRIn/BookRIn/app/models/reading_circles_client.rb:36:in `each'
/home/runner/work/BookRIn/BookRIn/app/models/reading_circles_client.rb:36:in `update_clubs'
/home/runner/work/BookRIn/BookRIn/app/models/reading_circles_client.rb:19:in `save'
/home/runner/work/BookRIn/BookRIn/lib/tasks/fetch.rake:8:in `block (2 levels) in <top (required)>'

Caused by:
PG::ConnectionBad: could not translate host name "bookrin-db.flycast" to address: Name or service not known (PG::ConnectionBad)
/home/runner/work/BookRIn/BookRIn/app/models/reading_circles_client.rb:37:in `block in update_clubs'
/home/runner/work/BookRIn/BookRIn/app/models/reading_circles_client.rb:36:in `each'
/home/runner/work/BookRIn/BookRIn/app/models/reading_circles_client.rb:36:in `update_clubs'
/home/runner/work/BookRIn/BookRIn/app/models/reading_circles_client.rb:19:in `save'
/home/runner/work/BookRIn/BookRIn/lib/tasks/fetch.rake:8:in `block (2 levels) in <top (required)>'
Tasks: TOP => fetch:reading_clubs
(See full trace by running task with --trace)
Error: Process completed with exit code 1.

I can’t seem to connect to the DB in the form myapp-db.flycast, is there a wrong way to get the DB_URL?

flycast addresses work only in the Fly.io network (or from machines added to Wireguard). An external Github Actions worker won’t be able to connect to your Fly-hosted database via a .flycast address.

Ways to solve this:

  1. Add a public IP address to your Postgres cluster. (Don’t do this! huge security risk)
  2. Generate a Wireguard configuration file and get the Github Actions worker to connect to Wireguard. (Also security risk, you’re allowing an external and possibly untrusted machine into your Fly private network).
  3. Somehow get the process you’re trying to execute, to run inside a Fly machine instead of the Github Actions worker. (this is probably the best option but it might imply some extra work to run the process on a Fly machine and then get the data out / where you need it).
  • Daniel
1 Like

@roadmr
Thank you for your response.

Currently, I am trying to use a rake task to fetch data from an API and cron update the database as follows:

name: Fetch Latest Reading Clubs

on:
  push:
    branches: [ "**" ]
  workflow_dispatch:
  schedule:
    - cron: "0 21 * * *"

jobs:
  fetch-reading-clubs:
    runs-on: ubuntu-latest

    env:
      RAILS_ENV: production
      DATABASE_URL: ${{ secrets.DATABASE_URL }}
      SECRET_KEY_BASE: ${{ secrets.SECRET_KEY_BASE }}
      FBC_LOGIN_NAME: ${{ secrets.FBC_LOGIN_NAME }}
      FBC_PASSWORD: ${{ secrets.FBC_PASSWORD }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.3.0'

      - name: Install dependencies
        run: bundle install

      - name: Run rake fetch task
        run: rake fetch:reading_clubs

To implement it using method 3, I am considering the following steps:

  • Log in to Fly and connect to the console from GitHub Actions
  • Set the necessary environment variables (such as XXX_LOGIN_NAME and XXX_PASSWORD)
    • The DATABASE_URL should already be set as an environment variable on Fly, so I plan to use that.
  • Execute the rake task from the console

I would appreciate it if you could let me know if there are any mistakes in this approach.

@roadmr
It might be better to use Fly’s features for cron instead of going through complex steps with GitHub Actions. I’ll give it a try.

solved.

name: Fetch Latest Reading Clubs

on:
  workflow_dispatch:
  schedule:
    - cron: "0 18 * * *"

jobs:
  fetch-reading-clubs:
    runs-on: ubuntu-latest

    env:
      RAILS_ENV: production
      FLY_SSH_TOKEN: ${{ secrets.FLY_SSH_TOKEN }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.3.0'

      - name: Install dependencies
        run: bundle install

      - name: Install Fly CLI
        uses: superfly/flyctl-actions/setup-flyctl@master

      - name: Run rake fetch task
        run: flyctl ssh console -t  $FLY_SSH_TOKEN --command "rake fetch:reading_clubs"

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.