sentry-demos / ios

iOS Empower Plant
1 stars 4 forks source link

Get SENTRY_ORG/PROJECT from .env; SENTRY_AUTH_TOKEN from .zshrc/.sentryclirc #20

Closed realkosty closed 1 year ago

realkosty commented 1 year ago

End goal is to run build in GH Actions from command line. In order to do that we need to un-hardcode these from the project definition files. While we're doing it figured could add some code to automagically get SENTRY_AUTH_TOKEN to make local setup even easier.

Same change but in more readable formatting:

BEFORE

# Type a script or drag a script file from your workspace to insert its path.
if which sentry-cli >/dev/null; then
export SENTRY_ORG=testorg-az
export SENTRY_PROJECT=ios
export SENTRY_AUTH_TOKEN=REPLACE_WITH_YOUR_AUTH_TOKEN
ERROR=$(sentry-cli upload-dif --include-sources "$DWARF_DSYM_FOLDER_PATH" 2>&1 >/dev/null)
if [ ! $? -eq 0 ]; then
echo "warning: sentry-cli - $ERROR"
fi
else
echo "warning: sentry-cli not installed, download from https://github.com/getsentry/sentry-cli/releases"
fi

AFTER

if which sentry-cli >/dev/null; then
    if [ -f .env ] && grep -q "^SENTRY_ORG=" .env && grep -q "^SENTRY_PROJECT=" .env; then
        export $(grep -v '^#' .env | sed '/^\s*$/d' | xargs)
    else
        echo "[ERROR] .env does not exist or does not have SENTRY_ORG and SENTRY_PROJECT defined"
        exit 1
    fi
    if [ ! -n "$SENTRY_AUTH_TOKEN" ]; then
        if [ -f ~/.sentryclirc ]; then
            export SENTRY_AUTH_TOKEN=$(grep -oE "token=([^\n\r]*)$" ~/.sentryclirc | cut -d'=' -f2)
        else 
            echo ".sentryclirc does not exist."
        fi
        if [ -f ~/.zshrc ] && grep -q "export SENTRY_AUTH_TOKEN" ~/.zshrc; then
            grep -m 1 "export SENTRY_AUTH_TOKEN" ~/.zshrc > /tmp/ios.sentry-build.tmp && source /tmp/ios.sentry-build.tmp && rm /tmp/ios.sentry-build.tmp
        else
            echo ".zshrc does not exist or does not have SENTRY_AUTH_TOKEN in it."
        fi
    fi
    if [ ! -n "$SENTRY_AUTH_TOKEN" ]; then
        echo "[ERROR] must provide SENTRY_AUTH_TOKEN either through command line, .zshrc or .sentryclirc"
        exit 1
    fi

    ERROR=$(sentry-cli upload-dif --include-sources "$DWARF_DSYM_FOLDER_PATH" 2>&1 >/dev/null)
    if [ ! $? -eq 0 ]; then
        echo "warning: sentry-cli - $ERROR"
    fi
else
    echo "[ERROR] sentry-cli not installed, download from https://github.com/getsentry/sentry-cli/releases"
    exit 1
fi

Testing

  1. build in XCode with both .zshrc and .sentryclirc present [SUCCESS]
  2. build in XCode with only .sentryclirc present [SUCCESS]
  3. build in XCode with neither .zshrc or .sentryclirc present [FAIL with useful error message]

Not tested yet but should work:

  1. SENTRY_AUTH_TOKEN=... xcodebuild ...