diff --git a/.github/workflows/TestReleaser.yml b/.github/workflows/TestReleaser.yml index 1e3fe11..26db9e1 100644 --- a/.github/workflows/TestReleaser.yml +++ b/.github/workflows/TestReleaser.yml @@ -115,6 +115,7 @@ jobs: uses: ./releaser/composite with: token: ${{ secrets.GITHUB_TOKEN }} + use-gh-cli: true files: | artifact-*.txt README.md diff --git a/releaser/README.md b/releaser/README.md index 703e65f..16d3711 100644 --- a/releaser/README.md +++ b/releaser/README.md @@ -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. +### 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 -**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. 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**. diff --git a/releaser/action.yml b/releaser/action.yml index dc3e9c0..897e506 100644 --- a/releaser/action.yml +++ b/releaser/action.yml @@ -40,6 +40,10 @@ inputs: description: 'Whether to create releases from any tag or to treat some as snapshots' required: false default: true + use-gh-cli: + description: 'Whether to use the GitHub CLI for uploading artifacts (requires actions/checkout)' + required: false + default: false runs: using: 'docker' image: 'Dockerfile' diff --git a/releaser/composite/action.yml b/releaser/composite/action.yml index b1a0599..f018513 100644 --- a/releaser/composite/action.yml +++ b/releaser/composite/action.yml @@ -40,6 +40,10 @@ inputs: description: 'Whether to create releases from any tag or to treat some as snapshots' required: false default: true + use-gh-cli: + description: 'Whether to use the GitHub CLI for uploading artifacts (requires actions/checkout)' + required: false + default: false runs: using: 'composite' steps: @@ -55,3 +59,4 @@ runs: INPUT_TAG: ${{ inputs.tag }} INPUT_RM: ${{ inputs.rm }} INPUT_SNAPSHOTS: ${{ inputs.snapshots }} + INPUT_USE-GH-CLI: ${{ inputs.use-gh-cli }} diff --git a/releaser/releaser.py b/releaser/releaser.py index 03767ae..7269aa5 100755 --- a/releaser/releaser.py +++ b/releaser/releaser.py @@ -27,6 +27,7 @@ from os import environ, getenv from glob import glob from pathlib import Path from github import Github, GithubException +from subprocess import check_call def GetListOfArtifacts(argv): @@ -104,6 +105,7 @@ def GetReleaseHandler(gh): # is semver compilant prerelease tag, thus a snapshot (we skip it) print("! Skipping snapshot prerelease") sys_exit() + return (tag, env_tag, True) def GetRepositoryHandler(repo): @@ -152,6 +154,12 @@ def UploadArtifacts(gh_release, artifacts): 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): for asset in assets: if asset.name == name: @@ -188,18 +196,25 @@ def UploadArtifacts(gh_release, artifacts): return print(" - keep") - if getenv("INPUT_RM", "false") == "true": - print("· RM set. All previous assets are being cleared...") - for asset in assets: - print(f" - {asset.name}") - asset.delete_asset() - else: - for asset in assets: - replace_asset(artifacts, asset) + UseGitHubCLI = getenv("INPUT_USE-GH-CLI", "false").lower() == 'true' - for artifact in artifacts: - print(f" > {artifact!s}:\n - uploading...") - gh_release.upload_asset(artifact) + if getenv("INPUT_RM", "false") == "true": + delete_all_assets(assets) + else: + if not UseGitHubCLI: + for asset in assets: + 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: + print(f" > {artifact!s}:\n - uploading...") + gh_release.upload_asset(artifact) def UpdateReference(gh_release, tag, sha, is_prerelease, is_draft):