jamjan05_AI-Vault-for-Obsidian/.github/workflows/release.yml

111 lines
3.6 KiB
YAML

name: Build and Release
on:
release:
types:
- published
permissions:
attestations: write
contents: write
id-token: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.release.tag_name }}
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Validate tag is semver
env:
VERSION: ${{ github.event.release.tag_name }}
run: |
# Obsidian requires plain semver (x.y.z, optionally -prerelease). Fail fast
# on bad tags like "1.0.2.1" before building or creating a release.
if [[ ! "${VERSION}" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
echo "::error::Tag '${VERSION}' is not valid semver (expected x.y.z or x.y.z-prerelease)."
exit 1
fi
echo "Tag '${VERSION}' is valid semver."
- name: Ensure stable releases are created from main
if: ${{ !github.event.release.prerelease }}
run: |
git fetch origin main
if [ "$(git rev-parse HEAD)" != "$(git rev-parse origin/main)" ]; then
echo "::error::Stable releases must be created from the current main branch. Use pre-release for beta/test branches."
exit 1
fi
- uses: actions/setup-node@v4
with:
node-version: "20"
- name: Install dependencies
run: npm ci
- name: Update version files from tag
env:
VERSION: ${{ github.event.release.tag_name }}
run: |
node <<'NODE'
const fs = require("node:fs");
const version = process.env.VERSION;
const readJson = (p) => JSON.parse(fs.readFileSync(p, "utf8"));
const writeJson = (p, d) => fs.writeFileSync(p, JSON.stringify(d, null, 2) + "\n");
const manifest = readJson("manifest.json");
manifest.version = version;
writeJson("manifest.json", manifest);
const pkg = readJson("package.json");
pkg.version = version;
writeJson("package.json", pkg);
if (fs.existsSync("package-lock.json")) {
const lock = readJson("package-lock.json");
lock.version = version;
if (lock.packages?.[""]) lock.packages[""].version = version;
writeJson("package-lock.json", lock);
}
if (fs.existsSync("versions.json")) {
const versions = readJson("versions.json");
versions[version] = manifest.minAppVersion;
writeJson("versions.json", versions);
}
NODE
- name: Build
run: npm run build
- name: Attest release assets
uses: actions/attest-build-provenance@v2
with:
subject-path: |
main.js
styles.css
- name: Commit updated version files to main
if: ${{ !github.event.release.prerelease }}
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git fetch origin main
git checkout main
git add manifest.json package.json versions.json
git add package-lock.json 2>/dev/null || true
git diff --cached --quiet || git commit -m "chore: update version to ${{ github.event.release.tag_name }}"
git push origin main
- name: Upload release assets
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAG: ${{ github.event.release.tag_name }}
run: |
gh release upload "$TAG" main.js styles.css manifest.json --clobber