Articles in this section

Changing Static Files Per Branch

Goal

We’re deploying a static site that will serve a different file depending on whether it’s prod or dev.

Assumptions

You will need:

  • An Upsun project
  • Git access

Problems

  1. We have a static site that consumes an outside API from the browser.
  2. This API has different keys for prod and dev mode.
  3. How can a static site serve different files per environment?

Steps

1. Add your key files to the repository

$ touch keys/dev.json
$ touch keys/prod.json

2. Add a writable path

Supposing your site is served from the web directory, we’ll add a writable path underneath it to host our symlink.
Add the mount in your .platform.app.yaml file:

mounts:
    'web/keys':
        source: local
        source_path: keys

3. Symlink the changed files at deploy time

Now that we have our key files in the repository and a writable path available, we can use our deploy hook to create a symlink to the appropriate file:

hooks:
    deploy: |
        set -e
        if [ "$PLATFORM_BRANCH" = "master" ] ; then
            ln -sfv ~/keys/prod.json web/keys/key.json
        else
            ln -sfv ~/keys/dev.json web/keys/key.json
        fi

Conclusion

Now we can serve different content to the browser per-environment without changing the static nature of our site.

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.