๐ Lab 18 - Run-Commands and deploying the frontend
May 31, 2023 ยท View on GitHub
โฐ Estimated time: 15-20 minutes
๐ Learning outcomes:
- Understand how to create custom targets via the "run-commands" workspace executor
- Explore real-world usages of "run-commands" by creating a frontend "deploy" executor
- Learn how to expose custom ENV variables to Nx
๐๏ธโโ๏ธ Steps :
-
Make sure you are on the
mainbranch
-
We'll use a CLI tool called Surge to statically deploy the front-end:
npm i -S surge
-
Get the surge token (you'll need to create an account with an email and password):
npx surge tokenโ๏ธ Copy the token you get
-
Let's use the Surge CLI to deploy our project:
# make sure the project is built first - and we have something in dist nx build store # use surge to deploy whatever assets are in dist/apps/store npx surge dist/apps/store https://<chose-some-unique-url-123>.surge.sh --token <your-surge-token>โ ๏ธ Make sure you chose a unique value for your domain above, otherwise it will fail as you won't have permission to deploy to an existing one.
โ ๏ธ You should see surge deploying to your URL - if you click you'll see just the header though, because it doesn't have a server yet to get the games from (the Network Dev Tools tab will have failing requests).
-
Let's now abstract away the above command into an Nx target. Generate a new "deploy" target using the
@nx/workspace:run-commandsgenerator:- under the
storeproject - the "command" will be the same as the one you typed in the previous step
๐ณ Hint
Consult the run-commands generator docs here
- under the
-
Use Git to inspect the changes in
project.jsonand try to deploy the store using Nx!
-
We're now storing the surge token in
project.json. We don't want to check-in this file and risk exposing this secret token. Also, we might want to deploy to different domains depending on the environment. Let's move these out:๐ Create a new file
apps/store/.local.env๐ And let's add your secrets to it
SURGE_TOKEN=<your-surge-token> SURGE_DOMAIN=https://<some-unique-url-123>.surge.shโ Finally, update your "deploy" command, so that it loads the values from the ENV, using the
${ENV_VAR}syntax.๐ณ Hint
surge dist/apps/store ${SURGE_DOMAIN} --token ${SURGE_TOKEN}
-
Now invoke the deploy target again, and check if it all still works.
โ ๏ธ Note for Windows users: the command might fail, as we're trying to access env variables the Linux-way. To make it pass, you can generate a separate
windows-deployexecutor (make sure you keep the existingdeploytarget intact - it will be used by GitHub Actions):nx generate run-commands windows-deploy --project=store --command="surge dist/apps/store %SURGE_DOMAIN% --token %SURGE_TOKEN%" nx windows-deploy store
-
BONUS - Notice how you can keep running the deploy command and it will always fully run it, it will not pull from the cache. We'll talk about this after the lab.
-
We currently have to remember to always run the build before deploying an app. Can you fix this, so that it always makes sure the app is built before we deploy?
โ We did not load those environment variables into the deploy process anywhere.
We just added a .local.env file. How does that work?
Nx automatically picks up any .env or .local.env files in your workspace,
and loads them into processes invoked for that specific app.
๐If you get stuck, check out the solution
โก๏ธ For the next lab, head over to our chapter list to chose your path โก๏ธ