@toptal/davinci-ci 7.3.2 → 7.4.0

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Change Log
2
2
 
3
+ ## 7.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#2368](https://github.com/toptal/davinci/pull/2368) [`676ef4fd`](https://github.com/toptal/davinci/commit/676ef4fdc68cee6766903fb811274c470bf3a5ee) Thanks [@sashuk](https://github.com/sashuk)!
8
+ - add ability to override environment variables by reading environment files
9
+
3
10
  ## 7.3.2
4
11
 
5
12
  ### Patch Changes
package/README.md CHANGED
@@ -85,6 +85,8 @@ The image is present on path `src/configs/docker/Dockerfile.gha-deploy` and have
85
85
  | `NGINX_CONFIG` | Ngninx config, davinci has an example file present at `src/configs/docker/nginx-vhost.conf` | Yes |
86
86
  | `HTML_CDN_CACHE_TAG` | `Cache-Tag` header for HTML documents, used for specific cache purging | No |
87
87
 
88
+ The default entrypoint `src/configs/docker/env-runtime.entrypoint.sh` reads environment file (`.env.$DAVINCI_ENV` has to be located in the root of the dist folder) and populates the environment variables with values from the file if the overriden environment variable is not set. Variables that are provided to `docker run` command (for example, `docker run -e DAVINCI_envvar1=1`) have higher priority that variables from the envrionment file.
89
+
88
90
  ### IDE Tooling
89
91
 
90
92
  This package can be used directly in your IDE through these extensions
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toptal/davinci-ci",
3
- "version": "7.3.2",
3
+ "version": "7.4.0",
4
4
  "keywords": [
5
5
  "ci",
6
6
  "docker",
@@ -15,6 +15,10 @@ RUN [ -z "$ENV_RUNTIME_ENTRYPOINT" ] && echo "ENV_RUNTIME_ENTRYPOINT is required
15
15
  COPY $ENV_RUNTIME_ENTRYPOINT /usr/local/bin/env-runtime.entrypoint.sh
16
16
  RUN chmod +x /usr/local/bin/env-runtime.entrypoint.sh
17
17
 
18
+ # Copy script that reads .env file and replaces DAVINCI_* environment variables if they are empty
19
+ COPY ./davinci/packages/ci/src/configs/docker/import-davinci-environment-file.sh /usr/local/bin/import-davinci-environment-file.sh
20
+ RUN chmod +x /usr/local/bin/import-davinci-environment-file.sh
21
+
18
22
  # version is used in build process, so the value won't be available here otherwise
19
23
  ARG VERSION
20
24
  RUN [ -z "$VERSION" ] && echo "VERSION is required" && exit 1 || true
@@ -13,7 +13,11 @@
13
13
 
14
14
  # keep in sync with index.html
15
15
  VARS_PREFIX="DAVINCI_"
16
- INDEX_FILE="/usr/share/nginx/html/index.html"
16
+ DIST_PATH="/usr/share/nginx/html"
17
+ INDEX_FILE="$DIST_PATH/index.html"
18
+
19
+ # import environment variables from .env.DAVINCI_ENV file depending
20
+ source import-davinci-environment-file.sh
17
21
 
18
22
  VARS="$(env | grep $VARS_PREFIX | awk -F = '{print $1}')"
19
23
 
@@ -0,0 +1,31 @@
1
+ #!/bin/sh -e
2
+
3
+ # Reads environment variables from .env.$DAVINCI_ENV file and overrides every empty
4
+ # variable in the existing environment
5
+ # See import-davinci-environment-file.test.sh for testing
6
+
7
+ if [ -z "$DAVINCI_ENV" ]; then
8
+ echo "Error: DAVINCI_ENV is empty"
9
+ exit 1
10
+ fi
11
+
12
+ if [ -z "$DIST_PATH" ]; then
13
+ echo "Error: DIST_PATH is empty"
14
+ exit 1
15
+ fi
16
+
17
+ ENV_FILE="$DIST_PATH/.env.$DAVINCI_ENV"
18
+ if [ -f "$ENV_FILE" ]; then
19
+ echo "Info: $ENV_FILE exists, importing variables..."
20
+
21
+ # Store current environment variables
22
+ OLD_ENV="$(export -p)"
23
+
24
+ # Load env file variables
25
+ export $(grep -v '^#' $ENV_FILE | xargs)
26
+
27
+ # Override env variables with previous values
28
+ eval "$OLD_ENV"
29
+ else
30
+ echo "Info: $ENV_FILE does not exist"
31
+ fi
@@ -0,0 +1,56 @@
1
+ #!/bin/bash
2
+
3
+ # How to run test:
4
+ # - navigate to this folder in bash
5
+ # - run script "./import-davinci-environment-variables.test.sh", there should be no lines with "Assertion failed"
6
+
7
+ # Set up test environment
8
+ export TMP_PATH="./tmp"
9
+ mkdir -p "$TMP_PATH"
10
+ export DIST_PATH="$TMP_PATH"
11
+
12
+ # Set up console colors
13
+ RED='\033[0;31m'
14
+ NC='\033[0m'
15
+
16
+ ############
17
+ # Test case:
18
+ # - EMPTY_VARIALBE_ABC value is replaced with value from .env.staging if the value in .env.staging is not empty
19
+ # - EMPTY_VARIALBE_DEF value stays empty as the value in .env.staging is empty
20
+ # - NON_EMPTY_VARIABLE value is not replaced with value from .env.staging
21
+ ############
22
+
23
+ export DAVINCI_ENV="staging"
24
+ export NON_EMPTY_VARIABLE="123"
25
+ printf "%s\n" "EMPTY_VARIALBE_ABC=from-env-file" "EMPTY_VARIALBE_DEF=" "NON_EMPTY_VARIABLE=from-env-file" "RANDOM_VARIABLE=456" > "$DIST_PATH/.env.$DAVINCI_ENV"
26
+
27
+ set -a
28
+ source import-davinci-environment-file.sh
29
+
30
+ if [ "$EMPTY_VARIALBE_ABC" != "from-env-file" ]; then
31
+ echo -e "${RED}Assertion failed:${NC} EMPTY_VARIALBE_ABC is not equal to expected_value (received \"$EMPTY_VARIALBE_ABC\")"
32
+ exit 1
33
+ fi
34
+
35
+ if [ "$EMPTY_VARIALBE_DEF" != "" ]; then
36
+ echo -e "${RED}Assertion failed:${NC} EMPTY_VARIALBE_DEF is not equal to expected_value (received \"$EMPTY_VARIALBE_DEF\")"
37
+ exit 1
38
+ fi
39
+
40
+ if [ "$NON_EMPTY_VARIABLE" != "123" ]; then
41
+ echo -e "${RED}Assertion failed:${NC} NON_EMPTY_VARIABLE is not equal to expected_value"
42
+ exit 1
43
+ fi
44
+
45
+ # Clean up test environment
46
+ rm -rf "$TMP_PATH"
47
+
48
+ ###########
49
+ # Test case: environment file does not exist
50
+ ###########
51
+ command_output=$(source import-davinci-environment-file.sh)
52
+
53
+ if [ "$command_output" != "Info: $DIST_PATH/.env.$DAVINCI_ENV does not exist" ]; then
54
+ echo -e "${RED}Assertion failed:${NC} command_output is not equal to expected_value (received \"$command_output\")"
55
+ exit 1
56
+ fi
@@ -18,4 +18,9 @@ server {
18
18
  add_header Cache-Tag "davinci,spa,index-doc,${HTML_CDN_CACHE_TAG}";
19
19
  try_files $uri /index.html;
20
20
  }
21
+
22
+ # forbid environment files download
23
+ location ~ ^/\.env\. {
24
+ deny all;
25
+ }
21
26
  }