From fc1dfef7d06367de17fdc9af0ae8a62f6f750c2b Mon Sep 17 00:00:00 2001 From: Kacper Kula Date: Thu, 30 Jan 2025 10:52:37 +0000 Subject: [PATCH] chore: fixing publish step --- .github/workflows/publish.yml | 70 ----------------- .github/workflows/tag-and-publish.yml | 106 ++++++++++++++++++++++++++ .github/workflows/tag.yml | 42 ---------- 3 files changed, 106 insertions(+), 112 deletions(-) delete mode 100644 .github/workflows/publish.yml create mode 100644 .github/workflows/tag-and-publish.yml delete mode 100644 .github/workflows/tag.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml deleted file mode 100644 index c3a2a72..0000000 --- a/.github/workflows/publish.yml +++ /dev/null @@ -1,70 +0,0 @@ -name: Release Obsidian plugin - -on: - push: - tags: - - "*" - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - - name: Use Node.js - uses: actions/setup-node@v3 - with: - node-version: "18.x" - - name: Install packages - run: npm install - - name: Build plugin - run: | - npm run build - - name: Extract release notes - id: release-notes - run: | - tag="${GITHUB_REF#refs/tags/}" - # Remove 'v' prefix if present for matching - version="${tag#v}" - - awk -v version="$version" ' - BEGIN { found=0; content="" } - /^# [0-9]+\.[0-9]+\.[0-9]+/ { - if (found) { exit } - if ($2 ~ "^"version"($| \\()") { found=1; next } - } - found { content = content $0 "\n" } - END { printf "%s", content } - ' CHANGELOG.md > release_notes.txt - - echo "NOTES<> $GITHUB_OUTPUT - cat release_notes.txt >> $GITHUB_OUTPUT - echo "EOF" >> $GITHUB_OUTPUT - - name: Create release - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - tag="${GITHUB_REF#refs/tags/}" - - gh release create "$tag" \ - --title="$tag" \ - --notes="${{ steps.release-notes.outputs.NOTES }}" \ - main.js manifest.json styles.css - - echo "RELEASE_URL=$(gh release view $tag --json url -q .url)" >> $GITHUB_OUTPUT - - name: Send Discord notification - if: success() && github.event_name != 'pull_request' - env: - DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} - run: | - tag="${GITHUB_REF#refs/tags/}" - - MESSAGE="šŸŽ‰ New release $tag is now available!\n\n" - MESSAGE+="šŸ“ Changes:\n" - MESSAGE+="${{ steps.release-notes.outputs.NOTES }}\n" - MESSAGE+="\nšŸ”— Release: ${{ steps.create-release.outputs.RELEASE_URL }}" - - curl -H "Content-Type: application/json" \ - -d "{\"content\": \"$MESSAGE\"}" \ - $DISCORD_WEBHOOK \ No newline at end of file diff --git a/.github/workflows/tag-and-publish.yml b/.github/workflows/tag-and-publish.yml new file mode 100644 index 0000000..ea9f104 --- /dev/null +++ b/.github/workflows/tag-and-publish.yml @@ -0,0 +1,106 @@ +name: Check Version and Release + +on: + push: + branches: + - main + paths: + - 'package.json' + +jobs: + check-and-release: + runs-on: ubuntu-latest + permissions: + contents: write + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Use Node.js + uses: actions/setup-node@v3 + with: + node-version: "18.x" + + - name: Get package version + id: package-version + run: | + VERSION=$(node -p "require('./package.json').version") + echo "VERSION=$VERSION" >> $GITHUB_OUTPUT + + - name: Check if tag exists + id: check-tag + run: | + if git rev-parse "${{ steps.package-version.outputs.VERSION }}" >/dev/null 2>&1; then + echo "EXISTS=true" >> $GITHUB_OUTPUT + else + echo "EXISTS=false" >> $GITHUB_OUTPUT + fi + + - name: Install packages + if: steps.check-tag.outputs.EXISTS == 'false' + run: npm install + + - name: Build plugin + if: steps.check-tag.outputs.EXISTS == 'false' + run: | + npm run build + + - name: Extract release notes + if: steps.check-tag.outputs.EXISTS == 'false' + id: release-notes + run: | + version="${{ steps.package-version.outputs.VERSION }}" + + # Use awk to extract the section for the current version + awk -v version="$version" ' + BEGIN { found=0; content="" } + /^# [0-9]+\.[0-9]+\.[0-9]+/ { + if (found) { exit } + if ($2 ~ "^"version"($| \\()") { found=1; next } + } + found { content = content $0 "\n" } + END { printf "%s", content } + ' CHANGELOG.md > release_notes.txt + + # Escape multiline output for GitHub Actions + echo "NOTES<> $GITHUB_OUTPUT + cat release_notes.txt >> $GITHUB_OUTPUT + echo "EOF" >> $GITHUB_OUTPUT + + - name: Create tag and release + if: steps.check-tag.outputs.EXISTS == 'false' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + # Create and push tag + git config user.name "GitHub Actions" + git config user.email "actions@github.com" + git tag -a "${{ steps.package-version.outputs.VERSION }}" -m "Release version ${{ steps.package-version.outputs.VERSION }}" + git push origin "${{ steps.package-version.outputs.VERSION }}" + + # Create release + gh release create "${{ steps.package-version.outputs.VERSION }}" \ + --title="${{ steps.package-version.outputs.VERSION }}" \ + --notes="${{ steps.release-notes.outputs.NOTES }}" \ + main.js manifest.json styles.css + + - name: Send Discord notification + if: steps.check-tag.outputs.EXISTS == 'false' && success() + env: + DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} + run: | + # Get release URL using gh cli + RELEASE_URL=$(gh release view "${{ steps.package-version.outputs.VERSION }}" --json url -q .url) + + # Prepare the message content + MESSAGE="šŸŽ‰ New release ${{ steps.package-version.outputs.VERSION }} is now available!\n\n" + MESSAGE+="šŸ“ Changes:\n" + MESSAGE+="${{ steps.release-notes.outputs.NOTES }}\n" + MESSAGE+="\nšŸ”— Release: $RELEASE_URL" + + # Send to Discord using curl + curl -H "Content-Type: application/json" \ + -d "{\"content\": \"$MESSAGE\"}" \ + $DISCORD_WEBHOOK \ No newline at end of file diff --git a/.github/workflows/tag.yml b/.github/workflows/tag.yml deleted file mode 100644 index 38eee99..0000000 --- a/.github/workflows/tag.yml +++ /dev/null @@ -1,42 +0,0 @@ -name: Auto Tag on Version Change - -on: - push: - branches: - - main - paths: - - 'package.json' - -jobs: - check-and-tag: - runs-on: ubuntu-latest - permissions: - contents: write - - steps: - - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Get package version - id: package-version - run: | - VERSION=$(node -p "require('./package.json').version") - echo "VERSION=$VERSION" >> $GITHUB_OUTPUT - - - name: Check if tag exists - id: check-tag - run: | - if git rev-parse "${{ steps.package-version.outputs.VERSION }}" >/dev/null 2>&1; then - echo "EXISTS=true" >> $GITHUB_OUTPUT - else - echo "EXISTS=false" >> $GITHUB_OUTPUT - fi - - - name: Create and push tag - if: steps.check-tag.outputs.EXISTS == 'false' - run: | - git config user.name "GitHub Actions" - git config user.email "actions@github.com" - git tag -a "${{ steps.package-version.outputs.VERSION }}" -m "Release version ${{ steps.package-version.outputs.VERSION }}" - git push origin "${{ steps.package-version.outputs.VERSION }}" \ No newline at end of file