Merged
Conversation
✅ Deploy Preview for redwoodjs-docs ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
This was referenced May 11, 2022
Contributor
|
Cant wait for this!!! |
Contributor
Author
|
Apparently I did write the doc updates! Damn I'm good. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Contains all the changes from the following 7 PRs:
git cloneand branch names #5282yarnandpm2exec calls during Baremetal deploy #5427Closes #5082
Closes #5083
Closes #5084
Closes #5300
Closes #5347
Closes #5298
Closes #5299
Closes #5422
Closes #5297
Release Notes
This release features some major updates to the Baremetal deploy option! Let's break them down along with any changes you'll need to make to your server and
deploy.tomlfile.New Code Update Strategy
We've got a good news/bad news situation. The good news: you can now deploy a branch other than
mainand code updates are much more reliable usinggit clone! The bad news is that you're going to have to do some more config and server setup to get this latest version working. But once you do, everything will be awesome!PM2
Baremetal now requires that PM2 be installed globally, rather than a development dependency of your app. This has to do with the new directory structure we're going to create and where
pm2is executed from (short version: it's run outside of your actual codebase, soyarn pm2won't be available).You'll want to remove your existing pm2 services. This will cause downtime, so be mindful if running in a production environment. Run the following commands on the server:
Now install PM2 globally: https://pm2.io/docs/runtime/guide/installation/
PM2 will still be running in memory as the previous
yarn pm2version, so you can run this command to update the process:If you previously setup PM2 to run at startup, you'll need to update to the new global install instead:
Be sure to remove
pm2in your app'spackage.jsonfile.Directory Structure
Next we'll need to update the directory structure inside your deploy path.
Current directory structure:
New directory structure:
The
currentsymlink is updated at the end of a deploy to point to the latest release (assuming a successful clone and build). This means all build packages inweb/distandapi/distremain self-contained in their parent timestamp directory. This makes a rollback trivial: just point thecurrentsymlink to the previous directory and restart your web/api processes!yarn rw deploy baremetal --rollbackis coming soon! A shared.envfile exists in the root of your app path and each copy of the codebase will contain a symlink back out to this file.The easiest way to update to this structure is to simply remove everything inside of your currently deploy directory (like
/var/www/myapp) and letyarn rw deploy baremetal --first-runset everything up for you. You'll want to keep your.envfile, however:Config Updates
You'll need to add a new option to your
ecosystem.config.jsfile for any processes that run within the context of your app:module.exports = { apps: [ { name: 'serve', + cwd: 'current', script: 'node_modules/.bin/rw', args: 'serve', instances: 'max', exec_mode: 'cluster', wait_ready: true, listen_timeout: 10000, } }You'll also need to add the
repoconfig setting and optionallybranchto yourdeploy.tomlfile (also note the new environment name prefixingservers, more on that in a moment):The
branchwill default tomainif not set.Make sure everything is saved, committed, and pushed up to your repo because we're ready for the first deploy.
First Deploy
Run the deploy command for the first time to setup the directory structure, check out your code, and build and start monitoring your processes:
And that's it! If everything started up correctly then you're good to go for future deploys. If not check out https://redwoodjs.com/docs/deployment/baremetal#troubleshooting
If you want to perform a one-time deploy of a branch other than the one listed in
deploy.tomlyou can do so via a flag at deploy time:Multiple Environment Support
Baremetal deploy now supports deploying to multiple environments! If you've got production, and staging, and whatever else, this update is for you. If you're setting up a new deploy from scratch with
yarn rw setup deploy baremetalyou're good to go. If you have an existingdeploy.tomlfile you may want to make an update:Instead of just
[[servers]]prefixing your server config, make it[[servers.environment]]whereenvironmentis the actual name of your environment. For example:You can still have multiple servers in an environment, just repeat the
[[servers.production]]heading multiple times, one for each block of server config:This new syntax is optional: keeping your default
[[servers]]name will act as production (and leaving off the stage name inyarn rw deploy baremetalwill default to use these server settings).Maintenance Page
You can now enable/disable a maintenance page with the Baremetal deploy! Simply create
web/src/maintenance.htmlcontaining your maintenance message. (For now this page must just be plain HTML, but if the need is there it should be possible to add the page to the webpack/babel pipeline and make it a full React app. If this is something you'd like to work on, get in touch!)When you're ready to enable your maintenance page on the site:
And to take it back down:
This command respects the environment argument so you can put it up on just your
stagingenvironment, for example:That's it! This feature works by turning
web/dist/index.htmlinto a symlink pointing toweb/src/maintenance.html. As long as your server is configured to check for the existence of200.htmlfor the web side and serve that for any URL request that isn't the API, users should see the maintenance page the time time your site loads. Note that if a user already has your site loaded, they will most likely be able to keep clicking through your pages since everything is already cached in the browser. We're thinking about adding a "ping" feature that, on each page request, would check with the server to see if the maintenance page should be displayed, and if so interrupt their browsing session to show it. If this is something you'd like to work on, let us know by opening an issue or PR!Old Deploy Cleanup
Baremetal will now cleanup old deploy codebase directories at the end of a deployment! It defaults to keep the last 5 deployments, but this can be configured in
deploy.tomlwith thekeepReleasesoption:[[servers.production]] host = "server.com" username = "ubuntu" agentForward = true sides = ["api", "web"] path = "/var/www/myapp" processNames = ["api"] repo = "git@github.com:myorg/myrepo.git" branch = "main" + keepReleases = 5Why keep around old deploys? In case something goes horribly wrong and you need to rollback!
This config setting is optional and will default to keeping the last 5 deploys if not set.
Rollback
If you deploy and find something has gone horribly wrong, you can now rollback your deploy to the previous release!
You can even rollback multiple deploys, up to the total number you still have denoted with the
keepReleasesoption:Note that this will not rollback your database—if you had a release that changed the database, that updated database will still be in effect, but with the previous version of the web and api sides. Trying to undo database migrations is a very difficult proposition and isn't even possible in many cases.
Make sure to thoroughly test releases that change the database before doing it for real!
There's no Rollforward: you probably rolled back because the newer deploy is faulty! Performing a new deploy would be the equivalent action to rolling the codebase forward to a newer release.
Override Default
yarnandpm2commandsYou can now override the default
yarnandpm2exec commands with your own custom command. Why would you want to do this? Tools like doppler provide their own CLI command to inject secrets into your shell, and you then chain on the command you would normally run, like so:To set this, add the following to your
deploy.tomlfile:These two settings are optional and will default to the existing
yarnandpm2commands if not set.Lifecycle Events
When in the course of a deploy, if it becomes necessary to run additional commands not provided for in the stock list of Baremetal steps, you can turn to lifecycle events. These events allow you to run custom commands before or after the existing commands. Here are the current steps in a deploy:
update- the latest code is cloned from the serversymlinkEnv- a symlink is created in the newly cloned codebase pointing to the.envfile in the main app directoryinstall- latest dependencies are installedmigrate- database and db migrations are runbuild- the api and/or web sides are builtsymlinkCurrent- a symlink is created from the main app directory to the latest deploy directoryrestart- PM2 restarts any services listedcleanup- old deploy directories are removedThere are three ways you can define your lifecycle events: globally (runs for all environments and all servers), environment-specific (runs for all servers in a given environment) or server-specific (runs only for a single server).
Global Lifecycle Events
Add a top level
[before]or[after]key to yourdeploy.toml, listing which step you want to hook into and what command to run:You can hook into multiple steps, and execute multiple custom commands:
Environment Lifecycle Events
Server Lifecycle Events
Important Considerations
All commands are run inside the newly deployed code directory (like
/var/www/myapp/20220520120000) exceptupdateandcleanup! These are run in thepathdefined indeploy.tomlsince they are independent of any single release.The
buildandrestartsteps may run multiple times based on how many sides you're building or services you're restarting. Any defined before/after lifecycle events will run as many times as the original step is run, so plan accordingly: make your command idempotent so that it can be run multiple times without side effects.