postiz-app icon indicating copy to clipboard operation
postiz-app copied to clipboard

feature/railway deployment

Open chad-syntax opened this issue 9 months ago • 16 comments

What kind of change does this PR introduce?

Adds railway deployment support

Why was this change needed?

I've seen a couple of issues, one from today actually https://github.com/gitroomhq/postiz-app/issues/632

EDIT: and also https://github.com/gitroomhq/postiz-app/issues/340

Some people are asking for it and I wanted it for myself so I did it. I figure I should open this up with some details on what problems I ran into and what I fixed.

Other information:

I did some moderate testing, but I haven't had the time to connect up all the different providers and test each one, I did hook up my discord and the posting and scheduling seemed to work with media I uploaded. I'm just not that familiar with the product to test fully.

I have created a railway template that will start working once this PR is merged in since it is configured to pull from this repo's main branch: https://railway.com/template/xWx2v4?referralCode=chad

Further reading on what I had to fix for the curious:

  • the Dockerfile had two volumes, uploads, and config, but railway does not support docker volumes, and instead requires you to use a railway volume. Also as a bonus, you can only have one volume per service.
    • uploads is used when STORAGE_PROVIDER="local"
    • config is used to transfer environment variables from build time to run time.
    • Solution: thankfully railway provides env vars during build time and run time, so we can delete the config volume completely and adjust the entry point to not warn if in a railway instance. Then we can just have the one /uploads volume provided by railway. However this also means editing the Dockerfile to remove the volumes, which is not ideal. I have created a separate Dockerfile.railway to solve this. If there is a better solution where we only need one Dockerfile, please lmk.
  • Connecting to redis
    • by default bullmq cannot find the redis service even though the url is good. This is because railway only supports ipv6 and that somehow messes up bullmq’s ability to connect.
    • Solution: Adding ?family=0 to the connection string fixes this according to the railway docs.
  • Wrong railway config from the start
    • I was a lot more confused than I should have been because of looking at other PRs and the existing railway config in the repo. Configuring the start and build commands with the railway.toml would sidestep Docker completely.
    • Solution: prune the railway config to only specify that we are using Docker. With Docker, Railway doesn’t need build and start commands configured, it will automatically build the docker image to build, and run the entrypoint to start.
  • Port configuration
    • Another small pain point was in some configurations the PORT env var was provided, which actually informs railway which port to connect to for the service.
    • Solution: remove the PORT env var, Railway automatically scans ports and suggests the correct port of 5000 to point the service to. This should be set in the template.

Checklist:

Put a "X" in the boxes below to indicate you have followed the checklist;

  • [x] I have read the CONTRIBUTING guide.
  • [x] I checked that there were not similar issues or PRs already open for this.
  • [x] This PR fixes just ONE issue (do not include multiple issues or types of change in the same PR) For example, don't try and fix a UI issue and include new dependencies in the same PR.

Summary by CodeRabbit

  • Chores
    • Simplified system initialization by removing outdated setup configuration.
  • New Features
    • Enhanced server port configuration to now prioritize a dedicated environment variable, offering more precise control over deployment settings.

chad-syntax avatar Feb 28 '25 14:02 chad-syntax

@chad-syntax is attempting to deploy a commit to the Listinai Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Feb 28 '25 14:02 vercel[bot]

[!IMPORTANT]

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The pull request removes the [phases.setup] configuration from railway.toml and updates the port selection logic in apps/backend/src/main.ts. The port determination now checks for the BACK_END_PORT environment variable before using PORT or defaulting to 3000.

Changes

File(s) Change Summary
railway.toml Removed the [phases.setup] section, including its nixPkgs and aptPkgs lists.
apps/backend/src/main.ts Modified port selection logic to check BACK_END_PORT first, then PORT, and fallback to 3000 if neither is set.

Sequence Diagram(s)

sequenceDiagram
    participant App as Backend Application
    participant Env as Environment
    
    App->>Env: Check BACK_END_PORT?
    alt BACK_END_PORT exists
        App->>App: Use BACK_END_PORT value
    else
        App->>Env: Check PORT?
        alt PORT exists
            App->>App: Use PORT value
        else
            App->>App: Use default 3000
        end
    end

Poem

I'm a little rabbit, hopping with cheer,
Changes in code now make things clear.
The setup has vanished, light as air,
And BACK_END_PORT leads with flair.
Leaping through logic with a joyful bound,
CodeRabbit Inc. keeps our roots sound!


[!NOTE]

🎁 Summarized by CodeRabbit Free

Your organization has reached its limit of developer seats under the Pro Plan. For new users, CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please add seats to your subscription by visiting https://app.coderabbit.ai/login.If you believe this is a mistake and have available seats, please assign one to the pull request author through the subscription management page using the link above.

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Feb 28 '25 14:02 coderabbitai[bot]

Hey @chad-syntax , Unfortunately after an internal discussion with Nevo David, we won't be merging this as long as he didn't arrive at home yet. He is currently traveling and won't be home at circa 8th March, as it could potentially break our production environment for the SaaS Version. This doesn't mean that your PR won't be merged, but we will wait till Nevo gets home and approves this PR.

egelhaus avatar Feb 28 '25 21:02 egelhaus

Hey @chad-syntax , Unfortunately after an internal discussion with Nevo David, we won't be merging this as long as he didn't arrive at home yet. He is currently traveling and won't be home at circa 8th March, as it could potentially break our production environment for the SaaS Version. This doesn't mean that your PR won't be merged, but we will wait till Nevo gets home and approves this PR.

Yeah no worries, I just wanted to give back after getting it running myself. There's no rush. I'm gathering up some screenshots for you in the meantime.

chad-syntax avatar Feb 28 '25 21:02 chad-syntax

Please provide test Screenshots or any proof that this will work with normal OSS Use.

Screenshot 2025-02-28 at 4 38 23 PM Screenshot 2025-02-28 at 4 38 16 PM Screenshot 2025-02-28 at 4 35 50 PM Screenshot 2025-02-28 at 4 33 37 PM Screenshot 2025-02-28 at 4 32 50 PM Screenshot 2025-02-28 at 4 30 33 PM Screenshot 2025-02-28 at 4 27 16 PM Screenshot 2025-02-28 at 4 26 59 PM

attached the following screenshots:

  • railway dashboard showing the postiz infrastructure (which in my own config I have custom dns setup to postiz.chadsyntax.com)
  • postiz auth page
  • postiz calendar page
  • creating and configuring a post
  • posting immediately which did post to my two channels that I have set up (discord and reddit)
  • posting with a schedule which did post at the right time to my two channels
  • screenshots of reddit and discord showing the postiz job concluded and indeed made the post

chad-syntax avatar Feb 28 '25 21:02 chad-syntax

Hey, thanks for providing screenshots of it working. But we will still have to wait on Nevo, as editing the template file etc. could potentially break our production environment. I or Nevo will inform you as soon as he arrives back at home and tests it out.

Thanks for the PR.

egelhaus avatar Feb 28 '25 22:02 egelhaus

Will wait for the ghcr.io/gitroomhq/postiz-app:latest image with the backend port mapping changes and then merge this code if needed. Just the suggestion

So the back-end port mapping is a bit of an issue... railway will use the PORT env var if it is present. So it makes sense to set that to 5000. And it works if we do that, railway automatically maps the service to port 5000 and we get the sign in page, but we can't actually sign in because api request to the back-end api fail.

[Nest] 186  - 03/01/2025, 10:03:33 PM   ERROR [NestApplication] Error: listen EADDRINUSE: address already in use :::5000 +5ms

[Nest] 186  - 03/01/2025, 10:03:33 PM   ERROR [Error: listen EADDRINUSE: address already in use :::5000] Backend failed to start on port 5000

This is because in the back-end code we have:

const port = process.env.PORT || 3000;

in apps/backend/src/main.ts and we need the back-end to be running on a different port (3000 preferably).

So either we can NOT set the PORT env var in the railway template, and direct folks in the documentation to edit the service and map the port manually...

OR

We update the main.ts file to look for a different env var. Such as:

const port = process.env.BACK_END_PORT || process.env.PORT || 3000;

and we can set BACK_END_PORT="3000" and PORT="5000" in the railway template.

chad-syntax avatar Mar 01 '25 22:03 chad-syntax

hi @chad-syntax , we have already merged PR #633 by defaulting the port in the config and we don't need these changes but it's up to @egelhaus @nevo-david to decide .. Now I'm using this railway deployment for my org to manage social media posts.. Nothing wrong with it.

Oh I see, I'll have to test to double check, but if it's the case we can set PORT=5000 in the railway env vars, and the back-end still runs on 3000 then yes we don't need this PR.

chad-syntax avatar Mar 02 '25 14:03 chad-syntax

Very exciting updates! Is it possible for users to test out the Railway deploy (I see two at the moment), or should we wait until Nevo the maintainer/dev is back?

candidexmedia avatar Mar 08 '25 18:03 candidexmedia

Very exciting updates! Is it possible for users to test out the Railway deploy (I see two at the moment), or should we wait until Nevo the maintainer/dev is back?

Yes, Im using it now for my org using this template.. https://railway.com/new/template/Rd76MM @candidexmedia

tamilarasi-tech avatar Mar 08 '25 18:03 tamilarasi-tech

Hey @chad-syntax and @tamilarasi-tech , would it be possible if we let the maintainer @nevo-david create the templates? For the one we will put documentation. And the Template File should be in the Repo.

egelhaus avatar Mar 09 '25 12:03 egelhaus

Hey @chad-syntax and @tamilarasi-tech , would it be possible if we let the maintainer @nevo-david create the templates? For the one we will put documentation. And the Template File should be in the Repo.

Absolutely, I don't think there is a railway template file convention though, or at least I couldn't find anything like that.

chad-syntax avatar Mar 09 '25 15:03 chad-syntax

Hey @chad-syntax and @tamilarasi-tech , would it be possible if we let the maintainer @nevo-david create the templates? For the one we will put documentation. And the Template File should be in the Repo.

Yeah fine with that.

tamilarasi-tech avatar Mar 10 '25 10:03 tamilarasi-tech

Your PR's base branch was set to main, PRs should be set to target dev. The base branch of this PR has been automatically changed to development, please check that there are no merge conflicts

github-actions[bot] avatar Mar 10 '25 12:03 github-actions[bot]

Hey @chad-syntax and @tamilarasi-tech , would it be possible if we let the maintainer @nevo-david create the templates? For the one we will put documentation. And the Template File should be in the Repo.

Railway offers kickbacks for open source developers, so having an official Postiz template is a good idea:

https://blog.railway.com/p/1M-open-source-kickbacks

https://railway.com/open-source-kickback

I will wait to deploy the official one so that I can support the project.

candidexmedia avatar Mar 10 '25 20:03 candidexmedia

We have managewd to use this template as suggested by @tamilarasi-tech

Thanks for it

But running into an issue, not sure if it is isolated due to Railway or if I have missed something. When setting up Instagram we have done everything correctly as per the docs but I keep getting the following error in our Railway logs.

This is what happens on the frontend during the redirect. I am not sure what the issue is, any help would be appreciated image


[Nest] 297  - 03/25/2025, 12:11:55 PM    WARN OpenAI API key not set, chat functionality will not work

TypeError: fetch failed

    at node:internal/deps/undici/undici:13178:13

    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

    at async /app/dist/apps/frontend/.next/server/app/(site)/integrations/social/[provider]/continue/page.js:3:28067

    at async it (/app/dist/apps/frontend/.next/server/app/(site)/integrations/social/[provider]/continue/page.js:3:26753) {

  digest: '1500677290',
  [cause]: Error: unknown scheme

      at makeNetworkError (node:internal/deps/undici/undici:8961:35)

      at schemeFetch (node:internal/deps/undici/undici:10363:34)

      at node:internal/deps/undici/undici:10205:26

      at mainFetch (node:internal/deps/undici/undici:10224:11)

      at fetching (node:internal/deps/undici/undici:10172:7)

      at fetch (node:internal/deps/undici/undici:10041:20)

      at fetch (node:internal/deps/undici/undici:13176:10)

      at fetch (node:internal/bootstrap/web/exposed-window-or-worker:72:12)

      at i (/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:180858)

      at /app/dist/apps/frontend/.next/server/chunks/4522.js:2:2127

}

Very exciting updates! Is it possible for users to test out the Railway deploy (I see two at the moment), or should we wait until Nevo the maintainer/dev is back?

Yes, Im using it now for my org using this template.. https://railway.com/new/template/Rd76MM @candidexmedia

nirmalkarmani avatar Mar 25 '25 12:03 nirmalkarmani

I tried to use the Docker image but ended also with the error:

address already in use :::5000 

It's really weird I'm facing this as @chad-syntax in March... because nowhere in the Docker documentation (compose, Coolify, raw Docker...) it's specified I have to set in addition PORT=3000.

Once this is set, the backend starts properly 👍

sneko avatar Sep 29 '25 22:09 sneko