1 year ago

#387762

test-img

Tino

Parse ngrok URL into environment variable is always null

I'm using a bash script to start an ngrok tunnel. I would then like to pass the returned tunnel URL into an environment variable so that I can access it in my code. So my script seems to create a valid tunnel, but I'm stuck with bash parsing it into an environment variable. When I manually execute curl http://localhost:4040/api/tunnels | jq ".tunnels[0].public_url" I get the URL. But I fail to pass that. API_URLis always null.

ping -c1 -W1 -q 127.0.0.1 &>/dev/null
status=$( echo $? )
if [[ $status == 0 ]] ; then
        #Connection success!
        echo "Backend is running"
        echo "==> Creating ⚡️ ngrok connection and parsing into API_URL env variable"
        pkill -f ngrok
        ../ngrok/ngrok http 3030 > /dev/null &
        while ! nc -z localhost 4040; do
        sleep 1/8 # wait Ngrok to be available
        done
        NGROK_REMOTE_URL="$(curl http://localhost:4040/api/tunnels | jq ".tunnels[0].public_url")"
        if test -z "${NGROK_REMOTE_URL}"
        then
        echo "❌ ERROR: ngrok doesn't seem to return a valid URL (${NGROK_REMOTE_URL})."
        exit 1
        fi

        # Trim double quotes from variable
        NGROK_REMOTE_URL=$(echo ${NGROK_REMOTE_URL} | tr -d '"')
        # If http protocol is returned, replace by https
        NGROK_REMOTE_URL=${NGROK_REMOTE_URL/http:\/\//https:\/\/}

        # parse NGROK_REMOTE_URL into API_URL
        export API_URL=$NGROK_REMOTE_URL #<== this doens't work
        echo "==> API_URL is now set to ${API_URL}"       
        echo "==> Launching Expo (React Native)"
        echo "Make sure you tunnel your localhost to nkgrok or similar."
        yarn start
else
     #Connection failure
        echo "Backend is not running."
        exit 1
fi

bash

ngrok

0 Answers

Your Answer

Accepted video resources