Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 30 additions & 39 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
public/_gen/
public/
functions/lib/
*.tar.gz

# Created by https://www.gitignore.io/api/node,firebase,webstorm+all

Expand Down
6 changes: 5 additions & 1 deletion functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <PROJECT_ID>`
}

function targetUrlForPath(path) {
let target = BASE_URL;
Expand Down
7 changes: 7 additions & 0 deletions scripts/add_baseurl_firebase.sh
Original file line number Diff line number Diff line change
@@ -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 "<PROJECT_ID>"
firebase functions:config:set network.base_url="https://pokeapi.co" --project "<PROJECT_ID>"
38 changes: 38 additions & 0 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -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}"