reactjs - 如何在 React 项目中自动创建 Sentry 版本并将源映射上传到 Sentry?
问题描述
我有一个 create-react-app 项目,我希望部署过程生成 Sentry 版本并将源映射也上传到 Sentry。
解决方案
该脚本将为文件中指定的版本创建 Sentry 版本package.json
,并将源映射上传到 Sentry。
它适用于任何 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
...
代替:
:sentry_organization_slug
并:sentry_project_slug
使用 sentry 的正确值(来自您的 sentry 帐户网站内任何页面的 URL)SENTRY_TOKEN
使用 Sentry 的令牌THE_URL_OF_THE_MAIN_JS_FILE
使用您的反应构建文件可公开访问的 URL。
跑。
确保不要忘记在每个版本上更新 package.json 版本
推荐阅读
- windows - cmake-gui 尝试打开但 UI 未显示
- webpack - 如何包含具有多个入口点的正确 webpack 包?
- r - 我在基本安装包和库调用方面遇到问题
- python - 根据另一列的值替换 Pandas DataFrame 中的值
- vue.js - 是否可以在 vuejs 公共文件夹上上传文件?
- android - 在 RecyclerView 中使用 ExoPlayer - 官方方式
- python - 如果一个词出现在前面,如何排除正则表达式选择?
- pdfjs - pdf.js 不采用 blob url
- ios - 如何从 SwiftUI 迁移到故事板?
- python - 如何在交叉验证中获得 Keras scikit-learn 包装器的训练和验证损失?