From 20f740a7540e6f074bf1abf612a4712d83fede31 Mon Sep 17 00:00:00 2001 From: Andrew Butler <1628649+AButler@users.noreply.github.com> Date: Tue, 3 Feb 2026 14:34:21 +0000 Subject: [PATCH] Move create tag to external file --- .github/workflows/release.yaml | 38 ++-------------------------------- build/create-tag.js | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 36 deletions(-) create mode 100644 build/create-tag.js diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 579b5d4..e693117 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -84,42 +84,8 @@ jobs: with: script: | const tag = process.env.TAG; - - // Check if tag already exists - let existingTag; - try { - existingTag = await github.rest.git.getRef({ - owner: context.repo.owner, - repo: context.repo.repo, - ref: `tags/${tag}` - }); - } catch (error) { - if (error.status === 404) { - // Tag doesn't exist, we can create it - existingTag = null; - } else { - throw error; - } - } - - if (existingTag) { - const existingSha = existingTag.data.object.sha; - if (existingSha === context.sha) { - console.log(`Tag ${tag} already exists with the same SHA (${context.sha}). Skipping tag creation.`); - return; - } else { - throw new Error(`Tag ${tag} already exists with a different SHA. Existing: ${existingSha}, Current: ${context.sha}`); - } - } - - // Create the tag - await github.rest.git.createRef({ - owner: context.repo.owner, - repo: context.repo.repo, - ref: `refs/tags/${tag}`, - sha: context.sha - }); - console.log(`Tag ${tag} created successfully at ${context.sha}`); + const createTag = require('./build/create-tag.js'); + await createTag({tag, github, context}); - name: Create GitHub release uses: ncipollo/release-action@v1 diff --git a/build/create-tag.js b/build/create-tag.js new file mode 100644 index 0000000..28c955b --- /dev/null +++ b/build/create-tag.js @@ -0,0 +1,38 @@ +module.exports = async ({tag, github, context}) => { + // Check if tag already exists + let existingTag = null; + + try { + existingTag = await github.rest.git.getRef({ + owner: context.repo.owner, + repo: context.repo.repo, + ref: `tags/${tag}` + }); + } catch (error) { + if (error.status !== 404) { + throw error; + } + + // Tag doesn't exist, we can create it + existingTag = null; + } + + if (existingTag) { + const existingSha = existingTag.data.object.sha; + if (existingSha === context.sha) { + console.log(`Tag ${tag} already exists with the same SHA (${context.sha}). Skipping tag creation.`); + return; + } + + throw new Error(`Tag ${tag} already exists with a different SHA. Existing: ${existingSha}, Current: ${context.sha}`); + } + + // Create the tag + await github.rest.git.createRef({ + owner: context.repo.owner, + repo: context.repo.repo, + ref: `refs/tags/${tag}`, + sha: context.sha + }); + console.log(`Tag ${tag} created successfully at ${context.sha}`); +};