Skip to content

Commit 8fa2043

Browse files
authored
Merge pull request #10 from PokeAPI/test-staging
2 parents 9ca29bc + b519dfb commit 8fa2043

File tree

5 files changed

+82
-41
lines changed

5 files changed

+82
-41
lines changed

.circleci/config.yml

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,45 @@
1-
version: 2
1+
version: 2.1
2+
3+
executors:
4+
node10:
5+
docker:
6+
- image: circleci/node:10.11.0
27

38
jobs:
49
build:
5-
docker:
6-
- image: circleci/node:10.11.0
10+
executor: node10
711
steps:
8-
- checkout
9-
- run: yarn --ignore-engines --cwd functions install
10-
- run: yarn --ignore-engines --cwd functions run lint
11-
- run: yarn --ignore-engines --cwd functions run build
12+
- checkout
13+
- run: yarn --ignore-engines --cwd functions install
14+
- run: yarn --ignore-engines --cwd functions run lint
15+
- run: yarn --ignore-engines --cwd functions run build
1216

1317
test:
14-
docker:
15-
- image: circleci/node:10.11.0
18+
executor: node10
1619
steps:
17-
- checkout
18-
- run: yarn --ignore-engines --cwd functions
19-
# TODO: write some tests and run them here
20+
- checkout
21+
- run: yarn --ignore-engines --cwd functions
22+
# TODO: write some tests and run them here
2023

2124
deploy:
22-
docker:
23-
- image: circleci/node:10.11.0
25+
executor: node10
2426
steps:
25-
- checkout
26-
- run: mkdir -p public
27-
28-
# Get stored artifacts from api-data and unpack into the 'public' directory
29-
- run: wget $(curl -s 'https://circleci.com/api/v1.1/project/github/PokeAPI/api-data/latest/artifacts?branch=master' | jq -r .[0].url)
30-
- run: tar xzf _gen.tar.gz -C public
31-
32-
# Get stored artifacts from pokeapi.co and unpack into the current directory
33-
- run: wget $(curl -s 'https://circleci.com/api/v1.1/project/github/PokeAPI/pokeapi.co/latest/artifacts?branch=master' | jq -r .[0].url)
34-
- run: tar xzf static_website.tar.gz -C public
35-
36-
# Deploy to Firebase
37-
- run: yarn --ignore-engines --cwd functions install
38-
- run: functions/node_modules/.bin/firebase deploy --token=$FIREBASE_DEPLOY_TOKEN --project=$FIREBASE_PROJECT_ID
39-
27+
- checkout
28+
- run:
29+
name: Deploy website and api-data to the correct environment (production/staging)
30+
command: sh -x scripts/deploy.sh
4031

4132
workflows:
4233
version: 2
4334
commit:
4435
jobs:
45-
- build
46-
- test:
47-
requires:
48-
- build
49-
- deploy:
50-
requires:
51-
- test
52-
filters:
53-
branches:
54-
only: master
36+
- build
37+
- test:
38+
requires:
39+
- build
40+
- deploy:
41+
requires:
42+
- test
43+
filters:
44+
branches:
45+
only: master

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
public/_gen/
1+
public/
22
functions/lib/
3+
*.tar.gz
34

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

functions/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import * as express from "express";
55
import * as functions from "firebase-functions";
66
import * as status from "http-status-codes"
77

8+
const config = functions.config();
9+
let BASE_URL = "https://pokeapi.co";
810

9-
const BASE_URL = "https://pokeapi.co"; // TODO: support also https://pokeapi-215911.firebaseapp.com conditionally
11+
if (config.network && config.network.base_url) {
12+
BASE_URL = config.network.base_url; // To retrieve the config run: `firebase functions:config:get --project <PROJECT_ID>`
13+
}
1014

1115
function targetUrlForPath(path) {
1216
let target = BASE_URL;

scripts/add_baseurl_firebase.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/dontexecute
2+
# This script is not intended to be executed
3+
# It is intended to document how to update the configuration of Firebase
4+
5+
# Adds BASE_URL to FireBase config
6+
firebase functions:config:set network.base_url="https://pokeapi-test-b6137.firebaseapp.com" --project "<PROJECT_ID>"
7+
firebase functions:config:set network.base_url="https://pokeapi.co" --project "<PROJECT_ID>"

scripts/deploy.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/sh
2+
# Executed when the `master` or `staging` branches of PokeAPI/api-data and PokeAPI/pokeapi.co are pushed to
3+
# Runs in CircleCI
4+
# Deploys both pokeapi.co and api-data to Firebase in the respective project
5+
# $FIREBASE_DEPLOY_TOKEN, $FIREBASE_PROJECT_ID, $FIREBASE_DEPLOY_TOKEN_STAGING, $FIREBASE_PROJECT_ID_STAGING are present in CircleCI
6+
# $deploy_location is an environment variable set when the job is triggered by one of the two repositories getting pushed
7+
8+
if [ "${deploy_location:=master}" = 'master' ]; then # https://stackoverflow.com/a/2013589/3482533
9+
echo 'Deploying master branches of PokeAPI/api-data and PokeAPI/pokeapi.co to https://pokeapi.co'
10+
TOKEN=${FIREBASE_DEPLOY_TOKEN}
11+
PROJECT=${FIREBASE_PROJECT_ID}
12+
elif [ "${deploy_location}" = 'staging' ]; then
13+
echo 'Deploying staging branches of PokeAPI/api-data and PokeAPI/pokeapi.co to the staging location'
14+
TOKEN=${FIREBASE_DEPLOY_TOKEN_STAGING}
15+
PROJECT=${FIREBASE_PROJECT_ID_STAGING}
16+
fi
17+
18+
mkdir -p public
19+
20+
# Get stored artifacts from api-data and unpack into the 'public' directory
21+
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)"
22+
if [ $? -ne 0 ]; then
23+
echo "Couldn't find the latest api-data .tar.gz for the branch ${deploy_location}"
24+
exit 1
25+
fi
26+
tar xzf _gen.tar.gz -C public
27+
28+
# Get stored artifacts from pokeapi.co and unpack into the current directory
29+
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)"
30+
if [ $? -ne 0 ]; then
31+
echo "Couldn't find the latest pokeapi.co website .tar.gz for the branch ${deploy_location}"
32+
exit 1
33+
fi
34+
tar xzf static_website.tar.gz -C public
35+
36+
# Deploy to Firebase
37+
yarn --ignore-engines --cwd functions install
38+
functions/node_modules/.bin/firebase deploy --token="${TOKEN}" --project="${PROJECT}"

0 commit comments

Comments
 (0)