Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable staging deployment #42

Merged
merged 9 commits into from
May 13, 2020
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
28 changes: 17 additions & 11 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ executors:

commands:
ditto-transform:
description: Run Ditto in order to transform the base URL to https://pokeapi.co instead of localhost
description: Run Ditto in order to transform the BASE_URL instead of http://localhost
steps:
- run: pip install --user -r requirements.txt
- run:
command: |
~/.local/bin/ditto transform \
--base-url='https://pokeapi.co' \
--src-dir=data \
--dest-dir=_gen
- run:
name: Install requirements.txt
command: pip install --user -r requirements.txt
- run:
name: Transform api-data's JSON files to have the correct BASE_URL instead of http://localhost
command: bash -x scripts/transform.sh

jobs:
test:
Expand All @@ -28,10 +27,15 @@ jobs:
steps:
- checkout
- ditto-transform
- run: tar czf _gen.tar.gz _gen/*
- run:
name: Pack the transformed data in a .tar.gz
command: tar czf _gen.tar.gz _gen/*
- store_artifacts:
path: _gen.tar.gz
- run: "curl -X POST --header \"Content-Type: application/json\" https://circleci.com/api/v1.1/project/github/PokeAPI/deploy/build?circle-token=$CIRCLECI_API_TOKEN"
# Trigger a new build of the deploy job of the deploy project
- run:
name: Trigger a deploy of the deploy project passing the desired environment/location to deploy to
command: 'curl --header "Content-Type: application/json" --data "{\"build_parameters\": {\"deploy_location\": \"$CIRCLE_BRANCH\", \"CIRCLE_JOB\": \"deploy\"}}" --request POST "https://circleci.com/api/v1.1/project/github/PokeAPI/deploy/tree/master?circle-token=$CIRCLECI_API_TOKEN"'

workflows:
version: 2
Expand All @@ -43,4 +47,6 @@ workflows:
- test
filters:
branches:
only: master
only:
- master
- staging
9 changes: 9 additions & 0 deletions scripts/transform.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
# Runs in CircleCI
# Replaces all the http://localhost links of api-data to match the corresponding deployment location.

if [ "${CIRCLE_BRANCH}" = 'master' ]; then # https://stackoverflow.com/a/2013589/3482533
~/.local/bin/ditto transform --base-url='https://pokeapi.co' --src-dir=data --dest-dir=_gen
elif [ "${CIRCLE_BRANCH}" = 'staging' ]; then
~/.local/bin/ditto transform --base-url='https://pokeapi-test-b6137.firebaseapp.com' --src-dir=data --dest-dir=_gen
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming Bash 4 is available on the host, I'd organize this with something like this (untested):

also rename the file to .bash instead of .sh if you do this to make it obvious it uses bash syntax

#!/usr/bin/env bash

declare -A BRANCH_TO_URL=(
    ["master"]="https://pokeapi.co"
    ["staging"]="https://pokeapi-test-b6137.firebaseapp.com"
)

BASE_URL="${BRANCH_TO_URL[$CIRCLE_BRANCH]}"
~/.local/bin/ditto transform --base-url="$BASE_URL" --src-dir=data --dest-dir=_gen

fi