diff --git a/.github/workflows/release-and-publish.yml b/.github/workflows/release-and-publish.yml new file mode 100644 index 0000000..8e5900e --- /dev/null +++ b/.github/workflows/release-and-publish.yml @@ -0,0 +1,64 @@ +name: Release and Publish + +on: + push: + tags: + - 'v*' + +jobs: + create-release: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Create Release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ github.ref }} + release_name: Release ${{ github.ref }} + draft: true + prerelease: false + + publish-npm: + needs: create-release + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: '16.x' + registry-url: 'https://registry.npmjs.org' + - run: npm ci + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + + update-release: + needs: publish-npm + runs-on: ubuntu-latest + steps: + - name: Update Release + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const { owner, repo } = context.repo; + const tagName = context.ref.replace('refs/tags/', ''); + + const releases = await github.rest.repos.listReleases({ owner, repo }); + const draftRelease = releases.data.find(release => release.draft && release.tag_name === tagName); + + if (draftRelease) { + await github.rest.repos.updateRelease({ + owner, + repo, + release_id: draftRelease.id, + draft: false, + name: `Release ${tagName}`, + body: 'This release has been automatically published to npm.' + }); + } \ No newline at end of file