From 66a005b4b2ad6e100adca13129501c185919ea01 Mon Sep 17 00:00:00 2001 From: Mangiang Date: Mon, 15 Nov 2021 21:22:51 -0500 Subject: [PATCH 01/10] tests: Use Nginx instead of yarn start --- .drone-docker.yml | 38 ++++++++++---------------------------- .drone-kubernetes.yml | 38 ++++++++++---------------------------- 2 files changed, 20 insertions(+), 56 deletions(-) diff --git a/.drone-docker.yml b/.drone-docker.yml index 728c7a1..5907b62 100644 --- a/.drone-docker.yml +++ b/.drone-docker.yml @@ -31,26 +31,27 @@ steps: - sed -i 's/{{ DEPLOY_SSH_KEY_PASSPHRASE }}/'"$DEPLOY_SSH_KEY_PASSPHRASE"'/g' ./.ansible-items/inventory - sed -i 's/{{ DEPLOY_USER }}/'"$DEPLOY_USER"'/g' ./.ansible-items/inventory - - name: install frontend dependencies + - name: install dependencies and build frontend image: node:lts-alpine3.14 commands: - node -v - npm -v - yarn --version - yarn --cwd ./items/frontend install + - yarn --cwd ./items/frontend build --pure-lockfile + - rm -rf ./items/api/public/static/* + - cp ./items/frontend/build/* ./items/api/public + - rm ./items/api/public/index.html depends_on: - replace hosts and user variables - name: frontend - image: node:lts-alpine3.14 + image: nginx:1.21.4-alpine commands: - - node -v - - npm -v - - yarn --version - - yarn --cwd ./items/frontend start + - cp ./items/frontend/build/* /usr/share/nginx/html detach: true depends_on: - - install frontend dependencies + - install dependencies and build frontend - name: test frontend image: cypress/browsers:node16.5.0-chrome94-ff93 @@ -60,25 +61,7 @@ steps: - yarn --version - yarn --cwd ./items/frontend cy:run depends_on: - - install frontend dependencies - - - name: build frontend - image: node:lts-alpine3.14 - commands: - - node -v - - npm -v - - yarn --version - - yarn --cwd ./items/frontend build --pure-lockfile - - rm -rf ./items/api/public/static/* - - mv ./items/frontend/build/* ./items/api/public - - rm ./items/api/public/index.html - depends_on: - - test frontend - when: - branch: - - main - - master - - development + - install dependencies and build frontend - name: check ansible syntax image: plugins/ansible:3 @@ -111,8 +94,7 @@ steps: SPT_ITEMS_PATH: from_secret: deploy_path depends_on: - - check ansible syntax - - build frontend + - test frontend when: branch: - master diff --git a/.drone-kubernetes.yml b/.drone-kubernetes.yml index a9239a1..7b62c8c 100644 --- a/.drone-kubernetes.yml +++ b/.drone-kubernetes.yml @@ -31,26 +31,27 @@ steps: - sed -i 's/{{ DEPLOY_SSH_KEY_PASSPHRASE }}/'"$DEPLOY_SSH_KEY_PASSPHRASE"'/g' ./.ansible-items/inventory - sed -i 's/{{ DEPLOY_USER }}/'"$DEPLOY_USER"'/g' ./.ansible-items/inventory - - name: install frontend dependencies + - name: install dependencies and build frontend image: node:lts-alpine3.14 commands: - node -v - npm -v - yarn --version - yarn --cwd ./items/frontend install + - yarn --cwd ./items/frontend build --pure-lockfile + - rm -rf ./items/api/public/static/* + - cp ./items/frontend/build/* ./items/api/public + - rm ./items/api/public/index.html depends_on: - replace hosts and user variables - name: frontend - image: node:lts-alpine3.14 + image: nginx:1.21.4-alpine commands: - - node -v - - npm -v - - yarn --version - - yarn --cwd ./items/frontend start + - cp ./items/frontend/build/* /usr/share/nginx/html detach: true depends_on: - - install frontend dependencies + - install dependencies and build frontend - name: test frontend image: cypress/browsers:node16.5.0-chrome94-ff93 @@ -60,25 +61,7 @@ steps: - yarn --version - yarn --cwd ./items/frontend cy:run depends_on: - - install frontend dependencies - - - name: build frontend - image: node:lts-alpine3.14 - commands: - - node -v - - npm -v - - yarn --version - - yarn --cwd ./items/frontend build --pure-lockfile - - rm -rf ./items/api/public/static/* - - mv ./items/frontend/build/* ./items/api/public - - rm ./items/api/public/index.html - depends_on: - - test frontend - when: - branch: - - main - - master - - development + - install dependencies and build frontend - name: check ansible syntax image: plugins/ansible:3 @@ -111,8 +94,7 @@ steps: SPT_ITEMS_PATH: from_secret: deploy_path depends_on: - - check ansible syntax - - build frontend + - test frontend when: branch: - master From 0bfc57000ddbaf1340053021d7e451594e527448 Mon Sep 17 00:00:00 2001 From: Mangiang Date: Mon, 15 Nov 2021 21:26:58 -0500 Subject: [PATCH 02/10] tests: fix copy directory --- .drone-kubernetes.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone-kubernetes.yml b/.drone-kubernetes.yml index 7b62c8c..aa8a5f1 100644 --- a/.drone-kubernetes.yml +++ b/.drone-kubernetes.yml @@ -40,7 +40,7 @@ steps: - yarn --cwd ./items/frontend install - yarn --cwd ./items/frontend build --pure-lockfile - rm -rf ./items/api/public/static/* - - cp ./items/frontend/build/* ./items/api/public + - cp -r ./items/frontend/build/* ./items/api/public - rm ./items/api/public/index.html depends_on: - replace hosts and user variables From 972ddd70cb44bb5869f30ab8d4437382dd35f27c Mon Sep 17 00:00:00 2001 From: Mangiang Date: Mon, 15 Nov 2021 21:40:45 -0500 Subject: [PATCH 03/10] tests: fix copy directory --- .drone-kubernetes.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone-kubernetes.yml b/.drone-kubernetes.yml index aa8a5f1..1b78b75 100644 --- a/.drone-kubernetes.yml +++ b/.drone-kubernetes.yml @@ -48,7 +48,7 @@ steps: - name: frontend image: nginx:1.21.4-alpine commands: - - cp ./items/frontend/build/* /usr/share/nginx/html + - cp -r ./items/frontend/build/* /usr/share/nginx/html detach: true depends_on: - install dependencies and build frontend From e4d8cb7663133bc63b3452c1873b3573692548f1 Mon Sep 17 00:00:00 2001 From: Mangiang Date: Mon, 15 Nov 2021 21:47:29 -0500 Subject: [PATCH 04/10] tests: fix nginx setup --- .drone-kubernetes.yml | 3 ++- items/frontend/package.json | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.drone-kubernetes.yml b/.drone-kubernetes.yml index 1b78b75..d86eb09 100644 --- a/.drone-kubernetes.yml +++ b/.drone-kubernetes.yml @@ -49,6 +49,7 @@ steps: image: nginx:1.21.4-alpine commands: - cp -r ./items/frontend/build/* /usr/share/nginx/html + - nginx -g daemon off; detach: true depends_on: - install dependencies and build frontend @@ -59,7 +60,7 @@ steps: - node -v - npm -v - yarn --version - - yarn --cwd ./items/frontend cy:run + - yarn --cwd ./items/frontend cy:ci depends_on: - install dependencies and build frontend diff --git a/items/frontend/package.json b/items/frontend/package.json index 460cf14..8c3f923 100644 --- a/items/frontend/package.json +++ b/items/frontend/package.json @@ -43,7 +43,7 @@ "cy:verify": "cypress verify", "cy:run:ci": "cross-env CYPRESS_BASE_URL=http://frontend:3000 cypress run --browser chrome", "cy:open": "yarn run cy:start:wait -- \"cypress open\"", - "cy:run": "yarn run wait-on http-get://frontend:3000 && yarn run cy:install && yarn run cy:verify && yarn run cy:run:ci", + "cy:ci": "yarn run wait-on http-get://frontend && yarn run cy:install && yarn run cy:verify && yarn run cy:run:ci", "eject": "react-scripts eject" }, "eslintConfig": { From bf1db24b342af81b43d0c772c0c8072504eea19d Mon Sep 17 00:00:00 2001 From: Mangiang Date: Mon, 15 Nov 2021 21:55:38 -0500 Subject: [PATCH 05/10] tests: fix nginx setup --- .drone-kubernetes.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone-kubernetes.yml b/.drone-kubernetes.yml index d86eb09..6858373 100644 --- a/.drone-kubernetes.yml +++ b/.drone-kubernetes.yml @@ -49,7 +49,7 @@ steps: image: nginx:1.21.4-alpine commands: - cp -r ./items/frontend/build/* /usr/share/nginx/html - - nginx -g daemon off; + - nginx -g "daemon off;" detach: true depends_on: - install dependencies and build frontend From 0c637ef92e565cd37ce67b84cb8c0b07ce3305e8 Mon Sep 17 00:00:00 2001 From: Mangiang Date: Mon, 15 Nov 2021 22:00:35 -0500 Subject: [PATCH 06/10] tests: fix nginx setup --- items/frontend/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/items/frontend/package.json b/items/frontend/package.json index 8c3f923..ce25159 100644 --- a/items/frontend/package.json +++ b/items/frontend/package.json @@ -41,7 +41,7 @@ "cy:start:wait": "start-server-and-test cy:start:app http://localhost:3000", "cy:install": "cypress install", "cy:verify": "cypress verify", - "cy:run:ci": "cross-env CYPRESS_BASE_URL=http://frontend:3000 cypress run --browser chrome", + "cy:run:ci": "cross-env CYPRESS_BASE_URL=http://frontend cypress run --browser chrome", "cy:open": "yarn run cy:start:wait -- \"cypress open\"", "cy:ci": "yarn run wait-on http-get://frontend && yarn run cy:install && yarn run cy:verify && yarn run cy:run:ci", "eject": "react-scripts eject" From 2a61366bcf3474d3209abc39e7ad60e29b63227e Mon Sep 17 00:00:00 2001 From: Mangiang Date: Mon, 15 Nov 2021 22:09:11 -0500 Subject: [PATCH 07/10] tests: fix nginx setup --- items/frontend/src/cypress/integration/url-check.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/items/frontend/src/cypress/integration/url-check.spec.tsx b/items/frontend/src/cypress/integration/url-check.spec.tsx index 802a7dd..22910fc 100644 --- a/items/frontend/src/cypress/integration/url-check.spec.tsx +++ b/items/frontend/src/cypress/integration/url-check.spec.tsx @@ -39,7 +39,7 @@ describe('Url check', () => { describe('Check page not found', () => { it('Invalid url should redirect to page not found', () => { - cy.visit('/ABC') + cy.visit('/ABC', {failOnStatusCode: false}).its('status').should('equal', 404) cy.get('#not-found-message').contains("This page does not exist !"); }) }) From a8a34dbd70b67b7fde58ffb1c1b727245d7494e6 Mon Sep 17 00:00:00 2001 From: Mangiang Date: Mon, 15 Nov 2021 22:32:26 -0500 Subject: [PATCH 08/10] tests: fix nginx setup --- .drone-kubernetes.yml | 1 + items/frontend/src/cypress/fixtures/default.conf | 15 +++++++++++++++ .../src/cypress/integration/url-check.spec.tsx | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 items/frontend/src/cypress/fixtures/default.conf diff --git a/.drone-kubernetes.yml b/.drone-kubernetes.yml index 6858373..758bc55 100644 --- a/.drone-kubernetes.yml +++ b/.drone-kubernetes.yml @@ -49,6 +49,7 @@ steps: image: nginx:1.21.4-alpine commands: - cp -r ./items/frontend/build/* /usr/share/nginx/html + - cp ./items/frontend/src/cypress/fixtures/default.conf /etc/nginx/conf.d/default.conf - nginx -g "daemon off;" detach: true depends_on: diff --git a/items/frontend/src/cypress/fixtures/default.conf b/items/frontend/src/cypress/fixtures/default.conf new file mode 100644 index 0000000..40b9650 --- /dev/null +++ b/items/frontend/src/cypress/fixtures/default.conf @@ -0,0 +1,15 @@ +server { + listen 80; + server_name localhost; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html; + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } +} diff --git a/items/frontend/src/cypress/integration/url-check.spec.tsx b/items/frontend/src/cypress/integration/url-check.spec.tsx index 22910fc..802a7dd 100644 --- a/items/frontend/src/cypress/integration/url-check.spec.tsx +++ b/items/frontend/src/cypress/integration/url-check.spec.tsx @@ -39,7 +39,7 @@ describe('Url check', () => { describe('Check page not found', () => { it('Invalid url should redirect to page not found', () => { - cy.visit('/ABC', {failOnStatusCode: false}).its('status').should('equal', 404) + cy.visit('/ABC') cy.get('#not-found-message').contains("This page does not exist !"); }) }) From 2d7b814700a57c422e211519f4fdbdeab1af39a7 Mon Sep 17 00:00:00 2001 From: Mangiang Date: Mon, 15 Nov 2021 22:41:10 -0500 Subject: [PATCH 09/10] tests: fix nginx testing setup --- .drone-docker.yml | 8 +++++--- .drone-kubernetes.yml | 2 +- .../src/cypress/{fixtures => nginx_config}/default.conf | 0 3 files changed, 6 insertions(+), 4 deletions(-) rename items/frontend/src/cypress/{fixtures => nginx_config}/default.conf (100%) diff --git a/.drone-docker.yml b/.drone-docker.yml index 5907b62..130dc28 100644 --- a/.drone-docker.yml +++ b/.drone-docker.yml @@ -40,7 +40,7 @@ steps: - yarn --cwd ./items/frontend install - yarn --cwd ./items/frontend build --pure-lockfile - rm -rf ./items/api/public/static/* - - cp ./items/frontend/build/* ./items/api/public + - cp -r ./items/frontend/build/* ./items/api/public - rm ./items/api/public/index.html depends_on: - replace hosts and user variables @@ -48,7 +48,9 @@ steps: - name: frontend image: nginx:1.21.4-alpine commands: - - cp ./items/frontend/build/* /usr/share/nginx/html + - cp -r ./items/frontend/build/* /usr/share/nginx/html + - cp ./items/frontend/src/cypress/nginx_config/default.conf /etc/nginx/conf.d/default.conf + - nginx -g "daemon off;" detach: true depends_on: - install dependencies and build frontend @@ -59,7 +61,7 @@ steps: - node -v - npm -v - yarn --version - - yarn --cwd ./items/frontend cy:run + - yarn --cwd ./items/frontend cy:ci depends_on: - install dependencies and build frontend diff --git a/.drone-kubernetes.yml b/.drone-kubernetes.yml index 758bc55..f0c8d9d 100644 --- a/.drone-kubernetes.yml +++ b/.drone-kubernetes.yml @@ -49,7 +49,7 @@ steps: image: nginx:1.21.4-alpine commands: - cp -r ./items/frontend/build/* /usr/share/nginx/html - - cp ./items/frontend/src/cypress/fixtures/default.conf /etc/nginx/conf.d/default.conf + - cp ./items/frontend/src/cypress/nginx_config/default.conf /etc/nginx/conf.d/default.conf - nginx -g "daemon off;" detach: true depends_on: diff --git a/items/frontend/src/cypress/fixtures/default.conf b/items/frontend/src/cypress/nginx_config/default.conf similarity index 100% rename from items/frontend/src/cypress/fixtures/default.conf rename to items/frontend/src/cypress/nginx_config/default.conf From cb016f4b0e27ce3c881246bcf8c854984617b590 Mon Sep 17 00:00:00 2001 From: Mangiang Date: Mon, 15 Nov 2021 22:57:50 -0500 Subject: [PATCH 10/10] docs: Update documentation --- items/docs/Walkthrough.md | 58 ++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/items/docs/Walkthrough.md b/items/docs/Walkthrough.md index 4ec88bc..b3e9094 100644 --- a/items/docs/Walkthrough.md +++ b/items/docs/Walkthrough.md @@ -7,10 +7,9 @@ * [Triggers](#triggers) * [Steps](#steps) * [Replace hosts and user variables](#replace-hosts-and-user-variables) - * [Install frontend dependencies](#install-frontend-dependencies) + * [Install frontend dependencies and build it](#install-frontend-dependencies-and-build-it) * [Run the frontend](#run-the-frontend) * [Test frontend](#test-frontend) - * [Build frontend](#build-frontend) * [Check ansible syntax](#check-ansible-syntax) * [Apply ansible playbook](#apply-ansible-playbook) * [Playbook definition](#playbook-definition) @@ -88,32 +87,42 @@ The following environment variables are injected using Drone secrets: Using `sed` makes temporary changes in the container/pod instead of commiting secrets in the repo in plain text. \ The changes are never pushed and are discarded when the container/pod is terminated. -### Install frontend dependencies +### Install frontend dependencies and build it ```yml -- name: install frontend dependencies +- name: install dependencies and build frontend image: node:lts-alpine3.14 commands: - node -v - npm -v - yarn --version - yarn --cwd ./items/frontend install + - yarn --cwd ./items/frontend build --pure-lockfile + - rm -rf ./items/api/public/static/* + - cp -r ./items/frontend/build/* ./items/api/public + - rm ./items/api/public/index.html depends_on: - replace hosts and user variables ``` +Executed on every push. \ +Since the PHP backend serves the ReactJS frontend, the former is built and moved in the latter. +Notes: +* `yarn --cwd ` executes the command in the specified file +* `rm -rf ./api/public/static/*` deletes the static files to make sure no old JavaScript files remain +* `rm ./api/public/invdex.html` ReactJS is bundled with a `index.html`. It is discarded to use [](https://dev.sp-tarkov.com/Rev/spt-items-api/raw/branch/master/resources/views/app.blade.php) instead. ### Run the frontend ```yaml - name: frontend - image: node:lts-alpine3.14 + image: nginx:1.21.4-alpine commands: - - node -v - - npm -v - - yarn --version - - yarn --cwd ./items/frontend start + - cp -r ./items/frontend/build/* /usr/share/nginx/html + - cp ./items/frontend/src/cypress/nginx_config/default.conf /etc/nginx/conf.d/default.conf + - nginx -g "daemon off;" detach: true depends_on: - - install frontend dependencies + - install dependencies and build frontend ``` +Copies the frontend and the nginx conf in the container to be able to test it. The frontend is run and `detach` is specified so the End-to-End tests (using cypress) can run on it ### Test frontend @@ -124,32 +133,12 @@ The frontend is run and `detach` is specified so the End-to-End tests (using cyp - node -v - npm -v - yarn --version - - yarn --cwd ./items/frontend cy:run + - yarn --cwd ./items/frontend cy:ci depends_on: - - install frontend dependencies + - install dependencies and build frontend ``` Run frontend tests using Cypress -### Build frontend -```yml -- name: build frontend - image: node:lts-alpine3.14 - commands: - - node -v - - npm -v - - yarn --version - - yarn --cwd ./items/frontend build --pure-lockfile - - rm -rf ./items/api/public/static/* - - mv ./items/frontend/build/* ./items/api/public - - rm ./items/api/public/index.html -``` -Executed on every push. \ -Since the PHP backend serves the ReactJS frontend, the former is built and moved in the latter. -Notes: - * `yarn --cwd ` executes the command in the specified file - * `rm -rf ./api/public/static/*` deletes the static files to make sure no old JavaScript files remain - * `rm ./api/public/index.html` ReactJS is bundled with a `index.html`. It is discarded to use [](https://dev.sp-tarkov.com/Rev/spt-items-api/raw/branch/master/resources/views/app.blade.php) instead. - ### Check ansible syntax ```yml - name: check ansible syntax @@ -159,6 +148,9 @@ Notes: inventory: ./.ansible-items/inventory galaxy: ./.ansible-items/requirements.yml syntax_check: true + when: + branch: + - development ``` Executed on every push. \ Check the Ansible syntax in [playbook.yml](../../.ansible-items/playbook.yml), [inventory](../../.ansible-items/inventory) and [requirements.yml](../../.ansible-items/requirements.yml). The check is executed on every push since we want to detect any error before validating the build using the promotion. @@ -184,6 +176,8 @@ Check the Ansible syntax in [playbook.yml](../../.ansible-items/playbook.yml), [ from_secret: deploy_user_group SPT_ITEMS_PATH: from_secret: deploy_path + depends_on: + - test frontend when: branch: - master