Skip to content

Managing Rails Env File

Christina Cortland edited this page Aug 7, 2024 · 10 revisions

This documentation assumes that you are following the steps from the top container-discovery project directory.

  1. Required fields for the Rails env file
  2. Managing Rails env file
  3. How the rails env file resolution works

Required fields

To run this container application, following fields must be set in the rails env file.

COOKIE_STORE=/tmp/cookies/holding_cookies.dat
SECRET_TOKEN=SECRET_TOKEN
MYSQL_HOST=MYSQL_HOST
MYSQL_DATABASE=MYSQL_DATABASE
MYSQL_PASSWORD=MYSQL_PASSWORD
MYSQL_USERNAME=MYSQL_USERNAME
SOLR_URL=SOLR_URL

The MySQL credentials can be found in the LastPass shared folder Shared-Discovery and Access-Library Systems -> container-discovery POC admin.
If you don't have access to the shared folder, please contact the library systems team.
Because the container application uses remote MySQL, MYSQL_SOCKET field must not be set in your rails env file.
It is recommended to keep all rails env file in rails_env directory as all files except test_env.example will be ignored by git.

Managing rails env file

To update environment variables in production, you will need to deploy an updated env file to s3 using the existing deploy_rails_env.sh script. Running the script requires AWS CLI.

  1. Download the existing env file from s3://container-discovery/container_env_latest

  2. Update the env file locally

  3. Deploy the updated env file to s3:

    ./deploy_rails_env.sh -r NEW_RAILS_ENV_FILE

  4. Redeploy the container image via Jenkins

How the rails env file resolution works

The container runs differently in the cloud than how it runs locally.
The container image needs to be registered in ECR and ECS will select the container image with "latest" tag to run.
The Jenkins job for this project will create new container image and push to ECR when all the tests run successfully.

As a part of the Docker image build, each container image is assigned current git commit hash value and uses this value to identify which rails env file it will use.
When the container image runs in ECS, it fetches the rails env file from S3 bucket matching the commit hash value - s3://container-discovery/container_env_GIT_COMMIT_HASH_VALUE. That means to run a container image in ECS, the matching rails env file must be present in the S3 bucket. The script keeps a special rails env file in the S3 bucket - s3://container-discovery/container_env_latest.

./deploy_rails_env.sh -r NEW_RAILS_ENV_FILE

The script will copy the rails file as s3://container-discovery/container_env_GIT_COMMIT_HASH_VALUE and another copy as s3://container-discovery/container_env_latest.

Behind the scene, the Jenkins job also manages the rails env files.
The Jenkins job downloads s3://container-discovery/container_env_latest for running the tests.
After it successfully tests and builds the container image, it will run the deploy script with the downloaded latest env file.
That is why the devs must deploy the rails env before pushing the changes if there are any updates to the env file.

./deploy_rails_env.sh

If you invoke the script without any arguments, it will copy the s3://container-discovery/container_env_latest to s3://container-discovery/container_env_GIT_COMMIT_HASH_VALUE.

./deploy_rails_env.sh -b PREVIOUS_COMMIT_HASH

Calling the script with -b PREVIOUS_COMMIT_HASH flag will update the s3://container-discovery/container_env_latest from s3://container-discovery/container_env_PREVIOUS_COMMIT_HASH.
Sometimes, rollback of container deployment is needed.
In that case, one will need to update ECR to use the correct previous version of the container image for ECS to use.
Becase each container image is assigned a github commit hash value, it will be able to fetch the correct rails hash file when the previous container image is run.
The problem is when you make updates later.
If you call the script without any flags (since no env didn't change) next, it will create a new rails env in S3 based on the rails env that matches the github commit of problematic version of container image that we rolled back.
Calling with -b flag is used to alleviate this problem.