Skip to content

Deployment

gcgbarbosa edited this page Apr 14, 2021 · 3 revisions

We currently keep two instances of the TNRSweb running. One for development and one for one for production. This page will provide information about how each instance is configured.

Development

The development instance is updated automatically everytime a commit is pushed to the main branch of the TNRSweb repository. Two scripts are involved in this operation. The first script is written in JavaScript and receives a notification from Github when the pull request is merged. Thereafter it triggers a second shell script that clone the repository, exports the static files to a specific folder deploys it using Apache.

Webhook script (Step 1)

Github has a built-in webhook functionality that is used by the first script. Every repository can have multiple webhooks. If you have permission, you can find the webhook configuration for TNRSweb here. We currently have an instruction that sends an HTTP request to the development server with JSON encoded information about the push.

We use a javascript library to facilitate the process called octokit/webhooks:

// needs webhooks from octokit
// npm install @octokit/webhooks
const { Webhooks } = require("@octokit/webhooks");
// exec is used to run shell scripts
const { exec } = require("child_process");

// declare webhooks with the secret passphrase from github
const webhooks = new Webhooks({
  secret: "<you can find this in the config>",
});

// run the deploy script in shell
const runDeployScript = () => exec("sh deploy-tnrs-web.sh", (error, stdout, stderr) => {
  // print the result
  console.log(stdout);
  // print to stderr if there is an error
  console.log(stderr);
  if (error !== null) {
    console.log(`exec error: ${error}`);
  }
});

webhooks.onAny(({ id, name, payload }) => {
  console.log(name, "event received, id", id);
  // check the name of the branch that received a push
  // in this case we are looking we are checking the `main` branch
  if (payload["ref"] == "refs/heads/main") {
    runDeployScript()
  }
});

require("http").createServer(webhooks.middleware).listen(3000);
// now we can receive webhook events at port 3000

Whenever the script receives an HTTP request from github that contains the secret key it runs the shell file deploy-tnrs-web.sh.

Shell script (Step 2)

The shell script is still under development. Right now it clones the repository, and call the npm binary with the correct parameters to export the static files to the out. Afterwards the shell script moves the content of out to the apache folder.

#!/usr/bin/env bash

# FIXME: parameterize these
APACHE_FOLDER=""
# REPOSITORY_FOLDER=""

# clone repo
echo "## cloning the repository ##"
rm -rf TNRSweb
git clone https://github.com/EnquistLab/TNRSweb

# making sure the repo is up to date
cd TNRSweb
git checkout main
git pull

# run compiling script
echo "## installing npm packages ##"
npm install
echo "## compiling js files ##"
npm run export

# delete the content of the /var/www/tnrs
rm -rf $APACHE_FOLDER*

# move the content to the right place
echo "## moving compiled files to /var/www ##"
mv out/* $APACHE_FOLDER

echo "## deployed successfully ##"

Production

The production deployment is done manually. The shell script provided above sumarizes the steps pretty well. All the steps done using the script now typed to the stdin terminal.

Clone this wiki locally