代码之家  ›  专栏  ›  技术社区  ›  Yossi Shasho

如何在react项目中自动创建Sentry发布并将源地图上载到Sentry?

  •  1
  • Yossi Shasho  · 技术社区  · 7 年前

    我有一个createreact应用程序项目,我希望部署过程生成一个Sentry版本,并将源地图上传到Sentry。

    1 回复  |  直到 7 年前
        1
  •  6
  •   Yossi Shasho    7 年前

    此脚本将为中指定的版本创建Sentry版本 package.json

    它适用于任何JS项目,而不仅仅是React。

    deploy.sh :

    SENTRY_TOKEN="YOUR_TOKEN"
    PACKAGE_VERSION=`cat package.json \
      | grep version \
      | head -1 \
      | awk -F: '{ print $2 }' \
      | sed 's/[",]//g' \
      | tr -d '[[:space:]]'`
    
    printf "\nBuilding version $PACKAGE_VERSION...\n\n"
    
    #2) Build for dev and cd to build directory
    npm run build # or whatever your build command is
    cd build/static/js # or whatever your build folder is
    
    #3) create Sentry release
    SOURCE_MAP=`find . -maxdepth 1 -mindepth 1 -name '*.map' | awk '{ gsub("./", "") ; print $0 }'`
    printf "\nCreating a Sentry release for version $PACKAGE_VERSION...\n"
    
    curl https://sentry.io/api/0/projects/:sentry_organization_slug/:sentry_project_slug/releases/ \
      -X POST \
      -H "Authorization: Bearer ${SENTRY_TOKEN}" \
      -H 'Content-Type: application/json' \
      -d "{\"version\": \"${PACKAGE_VERSION}\"}" \
    
    #4) Upload a file for the given release
    printf "\n\nUploading sourcemap file to Sentry: ${SOURCE_MAP}...\n"
    curl "https://sentry.io/api/0/projects/:sentry_organization_slug/:sentry_project_slug/releases/$PACKAGE_VERSION/files/" \
      -X POST \
      -H "Authorization: Bearer ${SENTRY_TOKEN}" \
      -F file=@${SOURCE_MAP} \
      -F name="https://THE_URL_OF_THE_MAIN_JS_FILE/$SOURCE_MAP"
    
    #5) IMPORTANT: Delete the sourcemaps before deploying
    rm $SOURCE_MAP
    
    #6) upload to your cloud provider
    ...
    

    替换:

    1. :sentry_organization_slug :sentry_project_slug 使用sentry提供的正确值(来自sentry帐户网站内任何页面的URL)
    2. SENTRY_TOKEN 用哨兵的令牌
    3. THE_URL_OF_THE_MAIN_JS_FILE 使用可公开访问react生成文件的URL。

        2
  •  2
  •   vshab    6 年前

    我最近也遇到了同样的问题,尽管Sentry没有针对CreateReact应用程序的官方解决方案,但他们的工具非常棒,而且自己创建发布的过程非常容易自动化。您需要生成发布名称、构建应用程序并使用此名称初始化Sentry库、创建Sentry发布和上载sourcemaps。

    https://medium.com/@vshab/create-react-app-and-sentry-cde1f15cbaa

    或者,您可以直接查看已配置项目的示例: https://github.com/vshab/create-react-app-and-sentry-example