Skip to content

Instantly share code, notes, and snippets.

@srea
Last active March 23, 2020 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srea/5c84156f4bde4dd70b533e82d469f8e4 to your computer and use it in GitHub Desktop.
Save srea/5c84156f4bde4dd70b533e82d469f8e4 to your computer and use it in GitHub Desktop.
Replace ngrok subdomain of bitrise.yml

setup

$ brew install jq

example

Launch ngrok before exec command

$ curl https://gist.githubusercontent.com/srea/5c84156f4bde4dd70b533e82d469f8e4/raw/cf62d2503c2082ff01f4172fe058ddd2942c01ee/update_ngrok_subdomain.sh -o update_ngrok_subdomain.sh
$ chmod +x update_ngrok_subdomain.sh
$ ./ngrok http 8080
$ ./update_ngrok_subdomain.sh BITRISE_PERSONAL_ACCESS_TOKEN APP_SLUG

ref

https://www.freeformatter.com/json-escape.html

#!/bin/bash
set -e
# See
# https://blog.bitrise.io/download-or-upload-the-bitrise-yml-of-your-app-with-bitrise-api
# https://api-docs.bitrise.io
TOKEN=${1}
BITRISE_REPOSITORY_SLUG=${2}
NGROK_SUBDOMAIN=$(curl -s -H 'Content-Type: application/json' http://localhost:4040/api/tunnels | jq -r ".tunnels[].public_url" | grep -E -o -m 1 '([a-z0-9]{8})')
TMP_CURRENT_BITRISE_YML='tmp_current_bitrise.yml'
TMP_REPLACE_BITRISE_YML='tmp_replaced_bitrise.yml'
TMP_UPDATE_LOG='tmp_update.log'
echo "Downloading latest bitrise.yml"
curl -s -H "Authorization: token ${TOKEN}" "https://api.bitrise.io/v0.1/apps/${BITRISE_REPOSITORY_SLUG}/bitrise.yml" -o $TMP_CURRENT_BITRISE_YML
echo "Find ngrok subdomain... "
CURRENT_NGROK_SUBDOMAIN=$(cat < $TMP_CURRENT_BITRISE_YML | grep -E -o -m 1 -h 'https:\/\/(.*)\.ngrok\.io' | grep -E -o '[a-z0-9]{8}')
echo "Replaceing ngrok subdomain"
sed -e "s/${CURRENT_NGROK_SUBDOMAIN}/${NGROK_SUBDOMAIN}/g" $TMP_CURRENT_BITRISE_YML | tee $TMP_REPLACE_BITRISE_YML
echo "Uploading replaced bitrise.yml"
# escape for json body
REPLACED_YML_STRING=$(jq -aRsM . <<< cat ${TMP_REPLACE_BITRISE_YML})
curl -s -X POST -H 'Content-Type: application/json' -H "Authorization: token ${TOKEN}" "https://api.bitrise.io/v0.1/apps/${BITRISE_REPOSITORY_SLUG}/bitrise.yml" -d "{\"app_config_datastore_yaml\":${REPLACED_YML_STRING}}" -o $TMP_UPDATE_LOG
echo "Checking diff"
diff $TMP_CURRENT_BITRISE_YML $TMP_REPLACE_BITRISE_YML
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment