Skip to content

Instantly share code, notes, and snippets.

@vincenzo
Created March 22, 2018 16:03
Show Gist options
  • Save vincenzo/e5c132d491cd601ea88f436e0f87751f to your computer and use it in GitHub Desktop.
Save vincenzo/e5c132d491cd601ea88f436e0f87751f to your computer and use it in GitHub Desktop.
Node.js, Platform.sh, GitHub, CircleCI
version: 2
jobs:
test:
docker:
- image: circleci/node:8.9.3
environment:
PLATFORM_VARIABLES: "set this to what you need, if you need"
steps:
- checkout
- run:
name: Install NodeJS app
command: npm install
- run:
name: Test NodeJS app
command: npm test
workflows:
version: 2
testwork:
jobs:
- test
version: 2
jobs:
test:
docker:
- image: circleci/node:8.9.3
environment:
PLATFORM_VARIABLES: "set this to what you need, if you need"
steps:
# Dependecies.
- run:
name: Install JQ
# jq is required by the integration-url.sh script.
command: sudo apt-get install jq
# Checkout and Test.
- checkout
- run:
name: Install NodeJS app
command: npm install
- run:
name: Test NodeJS app
# We set this env variable to be available for the tests to pick up.
command: INTEGRATION_ENV_URL=$(.circleci/integration-url.sh) npm test
workflows:
version: 2
testwork:
jobs:
- test
#!/usr/bin/env bash
# Authorization URL.
PLATFORMSH_AUTH_URL="https://accounts.platform.sh/oauth2/token"
# IMPORTANT: replacing printf with echo will noy yield the same (correct) result
PLATFORMSH_CLIENT_ID=$(printf "platform-cli:" | base64)
# PLATFORM_API_TOKEN variable is set in the Environment Variables section of the project's settings on CircleCI
PLATFORMSH_ACCESS_TOKEN=$(curl -s "${PLATFORMSH_AUTH_URL}" -H "Authorization: Basic ${PLATFORMSH_CLIENT_ID}" -H 'Content-Type: application/json' -X POST -d "{\"grant_type\": \"api_token\", \"api_token\": \"$PLATFORMSH_API_TOKEN\"}" | jq -r ".access_token")
# The env's machine name on platform.sh is pr-<github-pr-number>.
# CI_PULL_REQUEST is set by CircleCI during the environment set up for the container.
PLATFORMSH_PR_ENV="pr-$(echo ${CI_PULL_REQUEST} | grep / | cut -d/ -f7-)"
# Platform.sh region.
PLATFORMSH_REGION="eu"
# The project ID on platform.sh
PLATFORMSH_PROJECT="trxpe5o7zxsif"
# getEnvironment() API endpoint.
PLATFORMSH_API_URL="https://${PLATFORMSH_REGION}.platform.sh/api/projects/${PLATFORMSH_PROJECT}/environments/${PLATFORMSH_PR_ENV}"
# The platform.sh environment may not yet be ready when CircleCI runs this script.
# So, we enter a polling loop with a cycle of ${SLEEP_TIME} seconds.
# Since this script is only run when CircleCI is triggered by a pull request on our repo,
# and since the same pull request also triggers the creation of an environment,
# it is safe to assume that under normal conditions this loop will eventually terminate.
# If for any reason this should not be the case, CircleCI will eventually kill the process.
SLEEP_TIME="60" # in seconds
ENV_CMD="curl -s ${PLATFORMSH_API_URL} -H 'Authorization: Bearer '${PLATFORMSH_ACCESS_TOKEN}"
ENV_INFO=$(eval ${ENV_CMD})
ENV_STATUS=$(echo ${ENV_INFO} | jq -r '.status')
while [ "${ENV_STATUS}" != "active" ]; do
sleep ${SLEEP_TIME}
ENV_INFO=$(eval ${ENV_CMD})
ENV_STATUS=$(echo ${ENV_INFO} | jq -r '.status')
done
# Fetching pf:routes as we host in multi-app. Check what the API returns for a single-app deployment, I have the feeling it's something like public_url.
echo $(echo $ENV_INFO | jq -r '.["_links"]["pf:routes"]' | grep -Eo 'https:\/\/.*\/graphql')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment