[[GitHub Actions]]を使ってリリースを自動化する。[[Obsidian PluginをCommunity Pluginとして公開]]に則ったもの。
## ローカルでの事前準備
タグ付けまでの作業はLocalで行う。[[GitHub Actions]]でまとめて実行する方法もある。
- `manifest.json`のバージョン更新
- `package.json`のバージョン更新
- バージョンでタグ付け
- ビルドの確認
- push
以下のような`Makefile`を用意し、`make release version=x.y.z`みたいに実行。
```makefile
MAKEFLAGS += --warn-undefined-variables
SHELL := /bin/bash
ARGS :=
.SHELLFLAGS := -eu -o pipefail -c
.DEFAULT_GOAL := help
.PHONY: $(shell egrep -oh ^[a-zA-Z0-9][a-zA-Z0-9_-]+: $(MAKEFILE_LIST) | sed 's/://')
help: ## Print this help
@echo 'Usage: make [target]'
@echo ''
@echo 'Targets:'
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z0-9][a-zA-Z0-9_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $1, $2}' $(MAKEFILE_LIST)
guard-%:
@ if [ "${${*}}" = "" ]; then \
echo "[ERROR] Required: $* !!"; \
echo "[ERROR] Please set --> $*"; \
exit 1; \
fi
#------
release: guard-version ## [Required: $version. ex. 0.5.1]
@echo '1. Update versions'
@sed -i -r 's/\"version\": \".+\"/\"version\": \"$(version)\"/g' manifest.json
@git add manifest.json
@git commit -m "Update manifest"
@npm version $(version)
@echo '2. Build'
@npm run build
@echo '3. push'
@git push --tags
@git push
```
[[🔌Obsidianプラグイン]]のバージョンは`v`を含めてはいけないため、[[npmのversion prefixを削除]]して`v`を付けないようにする。
## GitHub Actionsでリリース作業
タグがpushされたときに自動で実行される。つまり、先の作業が成功したら実行される。
- 成果物をビルド
- リリースページの作成
- 成果物の一部をリリースページに配置
- Slackで通知
==⚠️2021-07-19現在では[[create-release]]と[[upload-release-asset]]はメンテナンスされていないため以下は非推奨. 代わりに[[action-gh-release]]を使用すること⚠️==
`.github/workflows/release.yaml`は以下の通り。[[create-release]]でリリースページを作成し、 [[upload-release-asset]]で成果物を配置する。
## YAMLファイル
```yaml:.github/workflows/release.yaml
name: "Release"
on:
push:
tags:
- "v*"
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/
[email protected]
- uses: actions/
[email protected]
with:
node-version: 14.x
- run: npm install
- run: npm run build
- name: Create Release
id: create_release
uses: actions/
[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: false
prerelease: false
- name: Upload main.js
id: upload-main
uses: actions/
[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./main.js
asset_name: main.js
asset_content_type: text/javascript
- name: Upload styles.css
id: upload-styles
uses: actions/
[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./styles.css
asset_name: styles.css
asset_content_type: text/css
- name: Upload manifest.json
id: upload-manifest
uses: actions/
[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./manifest.json
asset_name: manifest.json
asset_content_type: application/json
- name: "Slack notification (not success)"
uses: homoluctus/
[email protected]
if: "! success()"
with:
type: ${{ job.status }}
username: GitHub Actions (Failure)
job_name: ":obsidian: Release ${{ github.ref }}"
mention: channel
mention_if: always
icon_emoji: "github"
url: ${{ secrets.SLACK_WEBHOOK }}
notify:
needs: release
runs-on: ubuntu-latest
steps:
- name: "Slack Notification (success)"
uses: homoluctus/
[email protected]
if: always()
with:
type: ${{ job.status }}
username: GitHub Actions (Success)
job_name: ":obsidian: Release ${{ github.ref }}"
icon_emoji: ":github:"
url: ${{ secrets.SLACK_WEBHOOK }}
```