releaser: add option 'use-gh-cli'

This commit is contained in:
umarcor
2021-12-19 06:33:31 +01:00
parent 4177a535f1
commit 52491e6bcc
5 changed files with 44 additions and 12 deletions

View File

@@ -115,6 +115,7 @@ jobs:
uses: ./releaser/composite uses: ./releaser/composite
with: with:
token: ${{ secrets.GITHUB_TOKEN }} token: ${{ secrets.GITHUB_TOKEN }}
use-gh-cli: true
files: | files: |
artifact-*.txt artifact-*.txt
README.md README.md

View File

@@ -149,9 +149,16 @@ Set option `rm` to `true` for systematically removing previous artifacts (e.g. o
Whether to create releases from any tag or to treat some as snapshots. By default, all the tags with non-empty `prerelease` field (see [semver.org: Is there a suggested regular expression (RegEx) to check a SemVer string?](https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string)) are considered snapshots; neither a release is created nor assets are uploaded. Whether to create releases from any tag or to treat some as snapshots. By default, all the tags with non-empty `prerelease` field (see [semver.org: Is there a suggested regular expression (RegEx) to check a SemVer string?](https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string)) are considered snapshots; neither a release is created nor assets are uploaded.
### use-gh-cli
In order to work around the reliability issues explained in section *Troubleshooting* above, option *use-gh-cli* allows
using GitHub's official command line tool ([cli/cli](https://github.com/cli/cli)) for uploading/updating assets.
IMPORTANT: Using this option requires the repository to be cloned (preferredly through [actions/checkout](https://github.com/actions/checkout)).
## Advanced/complex use cases ## Advanced/complex use cases
**Releaser** is essentially a very fine wrapper to use the GitHub Actions context data along with the classes **Releaser** is essentially a very thin wrapper to use the GitHub Actions context data along with the classes
and methods of PyGithub. and methods of PyGithub.
Similarly to [actions/github-script](https://github.com/actions/github-script), users with advanced/complex requirements might find it desirable to write their own Python script, instead of using **Releaser**. Similarly to [actions/github-script](https://github.com/actions/github-script), users with advanced/complex requirements might find it desirable to write their own Python script, instead of using **Releaser**.

View File

@@ -40,6 +40,10 @@ inputs:
description: 'Whether to create releases from any tag or to treat some as snapshots' description: 'Whether to create releases from any tag or to treat some as snapshots'
required: false required: false
default: true default: true
use-gh-cli:
description: 'Whether to use the GitHub CLI for uploading artifacts (requires actions/checkout)'
required: false
default: false
runs: runs:
using: 'docker' using: 'docker'
image: 'Dockerfile' image: 'Dockerfile'

View File

@@ -40,6 +40,10 @@ inputs:
description: 'Whether to create releases from any tag or to treat some as snapshots' description: 'Whether to create releases from any tag or to treat some as snapshots'
required: false required: false
default: true default: true
use-gh-cli:
description: 'Whether to use the GitHub CLI for uploading artifacts (requires actions/checkout)'
required: false
default: false
runs: runs:
using: 'composite' using: 'composite'
steps: steps:
@@ -55,3 +59,4 @@ runs:
INPUT_TAG: ${{ inputs.tag }} INPUT_TAG: ${{ inputs.tag }}
INPUT_RM: ${{ inputs.rm }} INPUT_RM: ${{ inputs.rm }}
INPUT_SNAPSHOTS: ${{ inputs.snapshots }} INPUT_SNAPSHOTS: ${{ inputs.snapshots }}
INPUT_USE-GH-CLI: ${{ inputs.use-gh-cli }}

View File

@@ -27,6 +27,7 @@ from os import environ, getenv
from glob import glob from glob import glob
from pathlib import Path from pathlib import Path
from github import Github, GithubException from github import Github, GithubException
from subprocess import check_call
def GetListOfArtifacts(argv): def GetListOfArtifacts(argv):
@@ -104,6 +105,7 @@ def GetReleaseHandler(gh):
# is semver compilant prerelease tag, thus a snapshot (we skip it) # is semver compilant prerelease tag, thus a snapshot (we skip it)
print("! Skipping snapshot prerelease") print("! Skipping snapshot prerelease")
sys_exit() sys_exit()
return (tag, env_tag, True) return (tag, env_tag, True)
def GetRepositoryHandler(repo): def GetRepositoryHandler(repo):
@@ -152,6 +154,12 @@ def UploadArtifacts(gh_release, artifacts):
assets = gh_release.get_assets() assets = gh_release.get_assets()
def delete_all_assets(assets):
print("· RM set. All previous assets are being cleared...")
for asset in assets:
print(f" - {asset.name}")
asset.delete_asset()
def delete_asset_by_name(name): def delete_asset_by_name(name):
for asset in assets: for asset in assets:
if asset.name == name: if asset.name == name:
@@ -188,15 +196,22 @@ def UploadArtifacts(gh_release, artifacts):
return return
print(" - keep") print(" - keep")
UseGitHubCLI = getenv("INPUT_USE-GH-CLI", "false").lower() == 'true'
if getenv("INPUT_RM", "false") == "true": if getenv("INPUT_RM", "false") == "true":
print("· RM set. All previous assets are being cleared...") delete_all_assets(assets)
for asset in assets:
print(f" - {asset.name}")
asset.delete_asset()
else: else:
if not UseGitHubCLI:
for asset in assets: for asset in assets:
replace_asset(artifacts, asset) replace_asset(artifacts, asset)
if UseGitHubCLI:
env = environ.copy()
env["GITHUB_TOKEN"] = environ["INPUT_TOKEN"]
cmd = ["gh", "release", "upload", "--clobber", tag] + artifacts
print(f" > {' '.join(cmd)}")
check_call(cmd, env=env)
else:
for artifact in artifacts: for artifact in artifacts:
print(f" > {artifact!s}:\n - uploading...") print(f" > {artifact!s}:\n - uploading...")
gh_release.upload_asset(artifact) gh_release.upload_asset(artifact)