diff --git a/.circleci/config.yml b/.circleci/config.yml index 56fc23a..e49e70b 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,54 +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: 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 - + - 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: - branches: - only: master + - build + - test: + requires: + - build + - deploy: + requires: + - test + filters: + 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..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.co"; // 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; 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 "" diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100644 index 0000000..582906f --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,38 @@ +#!/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 + +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} +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 + +mkdir -p public + +# Get stored artifacts from api-data and unpack into the 'public' directory +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 +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' "$(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}"