Catching 404 response when calling github.rest.repos.getReleaseByTag() #468
-
Hi, I apologize in advance if this is a beginner's question but I am not a node expert so I am struggling a bit to do something that is probably pretty simple. I currently have the following step in my workflow: - name: Check if release already exists
id: check-release
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
script: |
const { VERSION } = process.env
github.rest.repos.getReleaseByTag({
owner: context.repo.owner,
repo: context.repo.repo,
tag: `v${VERSION}`,
}) This step succeeds when the tag exists and fails ( Regards, Thomas |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
👋 Hey @thomasleplus, I think something like this would work for you: try {
const { VERSION } = process.env
github.rest.repos.getReleaseByTag({
owner: context.repo.owner,
repo: context.repo.repo,
tag: `v${VERSION}`,
})
core.info(`Release v${VERSION} found`)
} catch (error) {
if (error.status === 404) {
core.info(`Release v${VERSION} not found`)
} else {
throw error
}
} That's based on the error handling example in https://github.com/octokit/request-error.js |
Beta Was this translation helpful? Give feedback.
-
Hi @joshmgross, Thanks, you've pointed me in the right direction. With the code that you've posted, the step was still failing with an HttpError but then I noticed an extra message: - name: Check if release already exists
if: env.VERSION != ''
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
id: check-release
with:
script: |
const { VERSION } = process.env
return github.rest.repos.getReleaseByTag({
owner: context.repo.owner,
repo: context.repo.repo,
tag: `v${VERSION}`,
}).then(function(result) {
core.info(`Release ${result.data.name} found`)
return result.data.name
}).catch(function(error) {
if (error.status === 404) {
core.info(`Release v${VERSION} not found`)
return
} else {
throw error
}
})
result-encoding: string Again I am not a node expert so maybe I am missing something but the workflow seems to work now so thanks again, Thomas |
Beta Was this translation helpful? Give feedback.
Hi @joshmgross,
Thanks, you've pointed me in the right direction. With the code that you've posted, the step was still failing with an HttpError but then I noticed an extra message:
Promise { <pending> }
. I believe the issue is that getReleaseByTag() returns a Promise and so I modified your code like this and it seems to work now: