简体   繁体   English

如何在React项目中自动创建Sentry版本并将源地图上传到Sentry?

[英]How to automatically create a Sentry release and upload source-maps to Sentry in a react project?

我有一个create-react-app项目,并且我想在部署过程中生成Sentry版本,并将源地图也上传到Sentry。

This script will create a Sentry release for version specified in the package.json file, and upload the source maps to Sentry. 该脚本将为package.json文件中指定的版本创建一个Sentry版本,并将源映射上传到Sentry。

It will work for any JS project, not just React. 它可以用于任何JS项目,而不仅仅是React。

create a file in your project root and name it deploy.sh : 在项目根目录中创建一个文件,并将其命名为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
...

replace: 更换:

  1. :sentry_organization_slug and :sentry_project_slug with the correct values from sentry (from the URL of any page inside your sentry account website) :sentry_organization_slug:sentry_project_slug ,其中包含来自岗哨的正确值(来自岗哨帐户网站内任何页面的网址)
  2. SENTRY_TOKEN with your token from Sentry SENTRY_TOKEN与来自Sentry的令牌
  3. THE_URL_OF_THE_MAIN_JS_FILE with the URL where your react build file is publicly accessible. THE_URL_OF_THE_MAIN_JS_FILE以及您的THE_URL_OF_THE_MAIN_JS_FILE构建文件可公开访问的URL。

run. 跑。

Make sure you don't forget to update the package.json version on every release 确保您不要忘记在每个发行版上更新package.json版本

I had the same problem recently and despite that there is no official solution for Create React App from Sentry their tooling is great and it's quite easy to automate the process of creating releases by yourself. 最近我遇到了同样的问题,尽管没有针对Sentry的Create React App的官方解决方案,但是它们的工具很棒,而且可以很容易地自动完成创建发布的过程。 You would need to generate release name, build the app and use this name to initialize Sentry library, create Sentry Release and upload sourcemaps. 您将需要生成发行版名称,构建应用程序并使用该名称来初始化Sentry库,创建Sentry发行版并上传源地图。

I wrote the article which explains in details how to do it: https://medium.com/@vshab/create-react-app-and-sentry-cde1f15cbaa 我写了这篇文章,详细说明了如何执行此操作: https : //medium.com/@vshab/create-react-app-and-sentry-cde1f15cbaa

Or you can go straight forward and look at example of configured project: https://github.com/vshab/create-react-app-and-sentry-example 或者,您可以直接查看配置项目的示例: https : //github.com/vshab/create-react-app-and-sentry-example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM