createRelease and GITHUB_REF

Hi, I’d like to create automatically pre-release from tag. I use YAML below to do it.
My tag is v_test and it is on the release list like tag releases/tag/v_test , not release yet.
After createRelease there is still only tag, but no release. Function createRelease returns code 201, so release was created, but it is not available on release list. On the release list, there is only tag.
Surprisingly, release is available with tag name refs/tags/v_test and it is on /releases/tag/refs/tags/v_test .
I think, problem is with tag_name or createRelease .

name: RELEASE

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    runs-on: self-hosted
    steps:
      - name: CHECKOUT
        uses: actions/checkout@v2
      - name: PRERELEASE
        uses: actions/github-script@v2
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            const { repo: { owner, repo }, sha } = context;
            const release = await github.repos.createRelease({
                owner, repo,
                tag_name: process.env.GITHUB_REF,
                prerelease: true
            });

I solved it, by modification tag_name with YAML below to cut last part of tag name:

name: RELEASE

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    runs-on: self-hosted
    steps:
      - name: CHECKOUT
        uses: actions/checkout@v2
      - name: PRERELEASE
        uses: actions/github-script@v2
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            const { repo: { owner, repo }, sha } = context;
            const tag = process.env.GITHUB_REF.split('/').slice(-1)[0];
            const release = await github.repos.createRelease({
                owner, repo,
                tag_name: tag ,
                prerelease: true
            });

@qbaj,

When using the Releases API “Create a release” to create a new release, the value of the parameter “tag_name” should be only the name of tag, instead of the full name that contains the prefix “refs/tags/”.

The expression “github.ref” and the default environment variable “GITHUB_REF” will return the full name of the tag or branch.

To create a new release in the workflow, GitHub has provided an official action (actions/create-release). If you pass the full name of a tag to the input “tag_name” of this action, the action will automatically omit the prefix and then pass the tag name into the API. You also can pass the tag name that has been omitted the prefix to the input “tag_name”.

If you want to execute the API in yourself JavaScript code, as you have done, you can use the function split() to omit the prefix from the full name of the tag (in fact, the official action also is doing like as this).

To omit the prefix from the full name of the tag in the bash scripts, you can do like as this:

GITHUB_REF=refs/tags/v1
tag_name=${GITHUB_REF##refs/tags/}
echo "tag_name = $tag_name"

Output:

tag_name = v1

Thanks for reply.

When I use full tag name refs/tags/v_test, createRelease returns code 201. So release was created, but it is not available on releases list.

Should this release (created by me) show on that list too?
If not, why createRelease returns code 201 and release is available on /releases/tag/refs/tags/v_test?

@qbaj,

When creating a new release with a tag name, if the tag with the specified name is not existing, generally GitHub will automatically create a new tag with the specified name bead on the latest commit on the default branch or the specified target_commitish, then create the new release with this new tag.

In your case, according to my tests, I think that:

  • When passing the full tag name to the API, the full tag name will be considered as a new tag name. And the API use this new tag name to create a new release.

  • However, due to the new tag name contains the prefix “refs/tags/”, maybe GitHub determines the new tag is illegal or unreasonable, so it will hide the new tag and new release. And we can only access the hidden release via the URL “https://github.com/<owner>/<repo>/releases/tag/refs/tags/<tag-name>”.