From af932508d5ad0fabd093a16bd8c06baf0f26a230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=20Pezz=C3=A8?= Date: Fri, 1 May 2020 22:31:11 +0200 Subject: [PATCH 01/11] play: deploy staging --- .circleci/config.yml | 21 ++++----------------- .gitignore | 3 ++- functions/src/index.ts | 2 +- scripts/deploy.sh | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 19 deletions(-) create mode 100644 scripts/deploy.sh diff --git a/.circleci/config.yml b/.circleci/config.yml index 56fc23a..6dd4386 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -23,20 +23,7 @@ jobs: - image: circleci/node:10.11.0 steps: - checkout - - run: mkdir -p public - - # Get stored artifacts from api-data and unpack into the 'public' directory - - run: wget $(curl -s 'https://circleci.com/api/v1.1/project/github/PokeAPI/api-data/latest/artifacts?branch=master' | jq -r .[0].url) - - run: tar xzf _gen.tar.gz -C public - - # Get stored artifacts from pokeapi.co and unpack into the current directory - - run: wget $(curl -s 'https://circleci.com/api/v1.1/project/github/PokeAPI/pokeapi.co/latest/artifacts?branch=master' | jq -r .[0].url) - - run: tar xzf static_website.tar.gz -C public - - # Deploy to Firebase - - run: yarn --ignore-engines --cwd functions install - - run: functions/node_modules/.bin/firebase deploy --token=$FIREBASE_DEPLOY_TOKEN --project=$FIREBASE_PROJECT_ID - + - run: sh scripts/deploy.sh workflows: version: 2 @@ -49,6 +36,6 @@ workflows: - deploy: requires: - test - filters: - branches: - only: master + # filters: # TODO: uncomment + # branches: + # only: master diff --git a/.gitignore b/.gitignore index cbd5788..f493f27 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ -public/_gen/ +public/ functions/lib/ +*.tar.gz # Created by https://www.gitignore.io/api/node,firebase,webstorm+all diff --git a/functions/src/index.ts b/functions/src/index.ts index 88cb61f..b28fec8 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -6,7 +6,7 @@ import * as functions from "firebase-functions"; import * as status from "http-status-codes" -const BASE_URL = "https://pokeapi.co"; // TODO: support also https://pokeapi-215911.firebaseapp.com conditionally +const BASE_URL = "https://pokeapi-test-b6137.firebaseapp.com/"; // TODO: support also https://pokeapi-215911.firebaseapp.com conditionally function targetUrlForPath(path) { let target = BASE_URL; diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100644 index 0000000..1bff33c --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,32 @@ +#!/bin/sh +# Executed when the `master` or `staging` branches of PokeAPI/api-data and PokeAPI/pokeapi.co are pushed to +# Runs in CircleCI +# Deploys both pokeapi.co and api-data to Firebase in the respective project +# $FIREBASE_DEPLOY_TOKEN, $FIREBASE_PROJECT_ID, $FIREBASE_DEPLOY_TOKEN_STAGING, $FIREBASE_PROJECT_ID_STAGING are present in CircleCI +# $deploy_location is an environment variable set when the job is triggered by one of the two repositories getting pushed + +deploy_location='staging' + +if [ "${deploy_location:=master}" = 'master' ]; then # https://stackoverflow.com/a/2013589/3482533 + TOKEN=${FIREBASE_DEPLOY_TOKEN} + PROJECT=${FIREBASE_PROJECT_ID} +elif [ "${deploy_location}" = 'staging' ]; then + TOKEN=${FIREBASE_DEPLOY_TOKEN_STAGING} + PROJECT=${FIREBASE_PROJECT_ID_STAGING} +fi + +mkdir -p public + +deploy_location='master' + +# Get stored artifacts from api-data and unpack into the 'public' directory +wget -O '_gen.tar.gz' --no-check-certificate "$(curl -s https://circleci.com/api/v1.1/project/github/PokeAPI/api-data/latest/artifacts?branch=${deploy_location} | jq -r .[0].url)" +tar xzf _gen.tar.gz -C public + +# Get stored artifacts from pokeapi.co and unpack into the current directory +wget -O 'static_website.tar.gz' --no-check-certificate "$(curl -s https://circleci.com/api/v1.1/project/github/PokeAPI/pokeapi.co/latest/artifacts?branch=${deploy_location} | jq -r .[0].url)" +tar xzf static_website.tar.gz -C public + +# Deploy to Firebase +yarn --ignore-engines --cwd functions install +functions/node_modules/.bin/firebase deploy --token="${TOKEN}" --project="${PROJECT}" \ No newline at end of file From 58e940daedf0a45ce450fe7b22810901e0c415b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=20Pezz=C3=A8?= Date: Fri, 1 May 2020 23:38:02 +0200 Subject: [PATCH 02/11] feat: enable -x/use correct branches --- .circleci/config.yml | 2 +- scripts/deploy.sh | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 6dd4386..bb86fb2 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -23,7 +23,7 @@ jobs: - image: circleci/node:10.11.0 steps: - checkout - - run: sh scripts/deploy.sh + - run: sh -x scripts/deploy.sh workflows: version: 2 diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 1bff33c..dcb9f3a 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -5,9 +5,7 @@ # $FIREBASE_DEPLOY_TOKEN, $FIREBASE_PROJECT_ID, $FIREBASE_DEPLOY_TOKEN_STAGING, $FIREBASE_PROJECT_ID_STAGING are present in CircleCI # $deploy_location is an environment variable set when the job is triggered by one of the two repositories getting pushed -deploy_location='staging' - -if [ "${deploy_location:=master}" = 'master' ]; then # https://stackoverflow.com/a/2013589/3482533 +if [ "${deploy_location:=staging}" = 'master' ]; then # https://stackoverflow.com/a/2013589/3482533 TOKEN=${FIREBASE_DEPLOY_TOKEN} PROJECT=${FIREBASE_PROJECT_ID} elif [ "${deploy_location}" = 'staging' ]; then @@ -17,8 +15,6 @@ fi mkdir -p public -deploy_location='master' - # Get stored artifacts from api-data and unpack into the 'public' directory wget -O '_gen.tar.gz' --no-check-certificate "$(curl -s https://circleci.com/api/v1.1/project/github/PokeAPI/api-data/latest/artifacts?branch=${deploy_location} | jq -r .[0].url)" tar xzf _gen.tar.gz -C public From 260dfdd5ab1cb67bd453320030772d682090cf47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=20Pezz=C3=A8?= Date: Fri, 1 May 2020 23:47:29 +0200 Subject: [PATCH 03/11] refactor: remove trailing slash --- functions/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/src/index.ts b/functions/src/index.ts index b28fec8..7e36135 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -6,7 +6,7 @@ import * as functions from "firebase-functions"; import * as status from "http-status-codes" -const BASE_URL = "https://pokeapi-test-b6137.firebaseapp.com/"; // TODO: support also https://pokeapi-215911.firebaseapp.com conditionally +const BASE_URL = "https://pokeapi-test-b6137.firebaseapp.com"; // TODO: support also https://pokeapi-215911.firebaseapp.com conditionally function targetUrlForPath(path) { let target = BASE_URL; From e06cbd67da735106b060d4e351a831eb81b533c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=20Pezz=C3=A8?= Date: Sat, 2 May 2020 15:18:56 +0200 Subject: [PATCH 04/11] feat: add BASE_URL to config --- functions/src/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/functions/src/index.ts b/functions/src/index.ts index 7e36135..8cf2b51 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -5,8 +5,12 @@ import * as express from "express"; import * as functions from "firebase-functions"; import * as status from "http-status-codes" +const config = functions.config(); +let BASE_URL = "https://pokeapi.co"; -const BASE_URL = "https://pokeapi-test-b6137.firebaseapp.com"; // TODO: support also https://pokeapi-215911.firebaseapp.com conditionally +if (config.network && config.network.base_url) { + BASE_URL = config.network.base_url; // To retrieve the config run: `firebase functions:config:get --project ` +} function targetUrlForPath(path) { let target = BASE_URL; From 66f5a2600fba259b86dc53af14ba1d4006989d74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=20Pezz=C3=A8?= Date: Sat, 2 May 2020 15:35:51 +0200 Subject: [PATCH 05/11] docs: add firebase config script --- scripts/add_baseurl_firebase.sh | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 scripts/add_baseurl_firebase.sh diff --git a/scripts/add_baseurl_firebase.sh b/scripts/add_baseurl_firebase.sh new file mode 100644 index 0000000..757e1ef --- /dev/null +++ b/scripts/add_baseurl_firebase.sh @@ -0,0 +1,7 @@ +#!/bin/dontexecute +# This script is not intended to be executed +# It is intended to document how to update the configuration of Firebase + +# Adds BASE_URL to FireBase config +firebase functions:config:set network.base_url="https://pokeapi-test-b6137.firebaseapp.com" --project "" +firebase functions:config:set network.base_url="https://pokeapi.co" --project "" From 4bd7b60a67b4178312095da1f32707d5afc97018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=20Pezz=C3=A8?= Date: Sat, 2 May 2020 15:48:37 +0200 Subject: [PATCH 06/11] feat: upgrade config to 2.1/document deploy step --- .circleci/config.yml | 56 ++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index bb86fb2..143db65 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,41 +1,45 @@ -version: 2 +version: 2.1 + +executors: + node10: + docker: + - image: circleci/node:10.11.0 jobs: build: - docker: - - image: circleci/node:10.11.0 + executor: node10 steps: - - checkout - - run: yarn --ignore-engines --cwd functions install - - run: yarn --ignore-engines --cwd functions run lint - - run: yarn --ignore-engines --cwd functions run build + - checkout + - run: yarn --ignore-engines --cwd functions install + - run: yarn --ignore-engines --cwd functions run lint + - run: yarn --ignore-engines --cwd functions run build test: - docker: - - image: circleci/node:10.11.0 + executor: node10 steps: - - checkout - - run: yarn --ignore-engines --cwd functions - # TODO: write some tests and run them here + - checkout + - run: yarn --ignore-engines --cwd functions + # TODO: write some tests and run them here deploy: - docker: - - image: circleci/node:10.11.0 + executor: node10 steps: - - checkout - - run: sh -x scripts/deploy.sh + - checkout + - run: + name: Deploy website and api-data to the correct environment (production/staging) + command: sh -x scripts/deploy.sh workflows: version: 2 commit: jobs: - - build - - test: - requires: - - build - - deploy: - requires: - - test - # filters: # TODO: uncomment - # branches: - # only: master + - build + - test: + requires: + - build + - deploy: + requires: + - test + # filters: # TODO: uncomment + # branches: + # only: master From b85a0cb1741fef68b045f697df056ce91206d92f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=20Pezz=C3=A8?= Date: Sat, 2 May 2020 15:57:44 +0200 Subject: [PATCH 07/11] refactor: inform user og what is being deployed --- scripts/deploy.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/deploy.sh b/scripts/deploy.sh index dcb9f3a..606fc1f 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -6,9 +6,11 @@ # $deploy_location is an environment variable set when the job is triggered by one of the two repositories getting pushed if [ "${deploy_location:=staging}" = 'master' ]; then # https://stackoverflow.com/a/2013589/3482533 + echo 'Deploying master branches of PokeAPI/api-data and PokeAPI/pokeapi.co to https://pokeapi.co' TOKEN=${FIREBASE_DEPLOY_TOKEN} PROJECT=${FIREBASE_PROJECT_ID} elif [ "${deploy_location}" = 'staging' ]; then + echo 'Deploying staging branches of PokeAPI/api-data and PokeAPI/pokeapi.co to the staging location' TOKEN=${FIREBASE_DEPLOY_TOKEN_STAGING} PROJECT=${FIREBASE_PROJECT_ID_STAGING} fi From 892f9fd66b6e0fad876aa1af1f2dc73e87f7394d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=20Pezz=C3=A8?= Date: Sat, 2 May 2020 19:49:35 +0200 Subject: [PATCH 08/11] feat: handle .tar.gz non-existance --- scripts/deploy.sh | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 606fc1f..34b0208 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -19,12 +19,20 @@ mkdir -p public # Get stored artifacts from api-data and unpack into the 'public' directory wget -O '_gen.tar.gz' --no-check-certificate "$(curl -s https://circleci.com/api/v1.1/project/github/PokeAPI/api-data/latest/artifacts?branch=${deploy_location} | jq -r .[0].url)" +if [ $? -ne 0 ]; then + echo "Couldn't find the latest api-data .tar.gz for the branch ${deploy_location}" + exit 1 +fi tar xzf _gen.tar.gz -C public # Get stored artifacts from pokeapi.co and unpack into the current directory wget -O 'static_website.tar.gz' --no-check-certificate "$(curl -s https://circleci.com/api/v1.1/project/github/PokeAPI/pokeapi.co/latest/artifacts?branch=${deploy_location} | jq -r .[0].url)" +if [ $? -ne 0 ]; then + echo "Couldn't find the latest pokeapi.co website .tar.gz for the branch ${deploy_location}" + exit 1 +fi tar xzf static_website.tar.gz -C public # Deploy to Firebase yarn --ignore-engines --cwd functions install -functions/node_modules/.bin/firebase deploy --token="${TOKEN}" --project="${PROJECT}" \ No newline at end of file +functions/node_modules/.bin/firebase deploy --token="${TOKEN}" --project="${PROJECT}" From e0cd124937e09af8dc61a4be2f0c1a597ad3214f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=20Pezz=C3=A8?= Date: Sat, 2 May 2020 19:50:23 +0200 Subject: [PATCH 09/11] refactor: check certificates --- scripts/deploy.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 34b0208..1765beb 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -18,7 +18,7 @@ fi mkdir -p public # Get stored artifacts from api-data and unpack into the 'public' directory -wget -O '_gen.tar.gz' --no-check-certificate "$(curl -s https://circleci.com/api/v1.1/project/github/PokeAPI/api-data/latest/artifacts?branch=${deploy_location} | jq -r .[0].url)" +wget -O '_gen.tar.gz' "$(curl -s https://circleci.com/api/v1.1/project/github/PokeAPI/api-data/latest/artifacts?branch=${deploy_location} | jq -r .[0].url)" if [ $? -ne 0 ]; then echo "Couldn't find the latest api-data .tar.gz for the branch ${deploy_location}" exit 1 @@ -26,7 +26,7 @@ fi tar xzf _gen.tar.gz -C public # Get stored artifacts from pokeapi.co and unpack into the current directory -wget -O 'static_website.tar.gz' --no-check-certificate "$(curl -s https://circleci.com/api/v1.1/project/github/PokeAPI/pokeapi.co/latest/artifacts?branch=${deploy_location} | jq -r .[0].url)" +wget -O 'static_website.tar.gz' "$(curl -s https://circleci.com/api/v1.1/project/github/PokeAPI/pokeapi.co/latest/artifacts?branch=${deploy_location} | jq -r .[0].url)" if [ $? -ne 0 ]; then echo "Couldn't find the latest pokeapi.co website .tar.gz for the branch ${deploy_location}" exit 1 From 4b961c45944b14d65a52e40a9aef03672a7d871f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=20Pezz=C3=A8?= Date: Sat, 2 May 2020 23:21:24 +0200 Subject: [PATCH 10/11] feat: run deploy only on master --- .circleci/config.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 143db65..e49e70b 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -40,6 +40,6 @@ workflows: - deploy: requires: - test - # filters: # TODO: uncomment - # branches: - # only: master + filters: + branches: + only: master From b519dfbb622045c3bf58f73eb721d5cabd0bedcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=20Pezz=C3=A8?= Date: Sat, 2 May 2020 23:25:31 +0200 Subject: [PATCH 11/11] feat: defaults to master --- scripts/deploy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 1765beb..582906f 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -5,7 +5,7 @@ # $FIREBASE_DEPLOY_TOKEN, $FIREBASE_PROJECT_ID, $FIREBASE_DEPLOY_TOKEN_STAGING, $FIREBASE_PROJECT_ID_STAGING are present in CircleCI # $deploy_location is an environment variable set when the job is triggered by one of the two repositories getting pushed -if [ "${deploy_location:=staging}" = 'master' ]; then # https://stackoverflow.com/a/2013589/3482533 +if [ "${deploy_location:=master}" = 'master' ]; then # https://stackoverflow.com/a/2013589/3482533 echo 'Deploying master branches of PokeAPI/api-data and PokeAPI/pokeapi.co to https://pokeapi.co' TOKEN=${FIREBASE_DEPLOY_TOKEN} PROJECT=${FIREBASE_PROJECT_ID}