Merge pull request #44 from InfSein/dev #138
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and Deploy | |
# 监听 master 分支上的 push 事件 | |
on: | |
push: | |
branches: | |
- master | |
jobs: | |
build-and-deploy: | |
# 构建环境使用 ubuntu | |
runs-on: ubuntu-latest | |
steps: | |
# 官方action, 将代码拉取到虚拟机 | |
- name: Checkout | |
uses: actions/checkout@v3 | |
with: | |
persist-credentials: false | |
fetch-depth: 0 # 克隆完整的历史记录,包括标签 | |
# 安装node.js | |
- name: Use Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: "20.8.1" | |
# 下载依赖、打包项目 | |
- name: Install and Build | |
run: | | |
yarn install | |
yarn build | |
# 部署 | |
- name: Deploy | |
uses: JamesIves/[email protected] | |
with: | |
# 项目配置的打包目录名称 | |
folder: dist | |
# 部署后提交到的分支 | |
branch: static-pages | |
# 打包构建输出为zip文件 | |
- name: Zip Static Pages | |
run: | | |
zip -r static-pages.zip dist | |
# 获取 package.json 中的版本号 | |
- name: Get Version from package.json | |
id: get_version | |
run: echo "VERSION=$(jq -r .version < package.json)" >> $GITHUB_ENV | |
# 获取上一个标签 | |
- name: Get Previous Tag | |
id: prev_tag | |
run: | | |
PREV_TAG=$(git describe --tags --abbrev=0 --first-parent master^ 2>/dev/null || echo "initial") | |
echo "PREV_TAG=$PREV_TAG" >> $GITHUB_ENV | |
# 生成从上一个标签到当前的更改日志 | |
- name: Generate Changelog | |
id: generate_changelog | |
run: | | |
CHANGELOG=$(git log ${{ env.PREV_TAG }}..HEAD --grep='^feat:\|^fix:' --pretty=format:"* %s" | sed 's/^feat://;s/^fix://') | |
echo "CHANGELOG=$CHANGELOG" >> $GITHUB_ENV | |
# 检查是否存在同版本的 Release | |
- name: Check for Existing Release | |
id: check_release | |
run: | | |
if gh release view "v${{ env.VERSION }}" > /dev/null 2>&1; then | |
echo "exists=true" >> $GITHUB_ENV | |
else | |
echo "exists=false" >> $GITHUB_ENV | |
fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
# 创建或更新 Release,描述中包含变更日志和提交历史 | |
- name: Create or Update Release | |
run: | | |
RELEASE_NOTES=$(echo -e "## 更新日志\n\n${{ env.CHANGELOG }}\n\n\n\n**查看所有更改**: https://github.com/InfSein/hqhelper-dawntrail/compare/${{ env.PREV_TAG }}...v${{ env.VERSION }}") | |
if [ "${{ env.exists }}" = "true" ]; then | |
gh release upload "v${{ env.VERSION }}" static-pages.zip --clobber | |
gh release edit "v${{ env.VERSION }}" --notes "$RELEASE_NOTES" | |
else | |
gh release create "v${{ env.VERSION }}" static-pages.zip --title "Release v${{ env.VERSION }}" --notes "$RELEASE_NOTES" | |
fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |