Skip to content

Commit

Permalink
feat: upload assets to the release associated with given tagName (#895)
Browse files Browse the repository at this point in the history
Co-authored-by: FabianLars <fabianlars@fabianlars.de>
  • Loading branch information
Chiichen and FabianLars authored Dec 10, 2024
1 parent 707c1af commit fb76925
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .changes/feat-upload-with-tag-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
action: patch
---

Upload assets to the release associated with given `tagName`
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ These inputs allow you to modify the GitHub release.

| Name | Required | Description | Type | Default |
| ------------------ | :------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------ | ------------------------- |
| `releaseId` | false | The id of the release to upload artifacts as release assets | number | |
| `tagName` | false | The tag name of the release to create or the tag of the release belonging to `releaseId` | string | |
| `releaseName` | false | The name of the release to create | string | |
| `releaseId` | false | The id of the release to upload artifacts as release assets. If set, `tagName` and `releaseName` will not be considered to find a release. | number | |
| `tagName` | false | The tag name of the release to upload/create or the tag of the release belonging to `releaseId` | string | |
| `releaseName` | false | The name of the release to create. Required if there's no existing release for `tagName` | string | |
| `releaseBody` | false | The body of the release to create | string | |
| `releaseDraft` | false | Whether the release to create is a draft or not | bool | false |
| `releaseDraft` | false | Whether the release to find or create is a draft or not | bool | false |
| `prerelease` | false | Whether the release to create is a prerelease or not | bool | false |
| `releaseCommitish` | false | Any branch or commit SHA the Git tag is created from, unused if the Git tag already exists | string | SHA of current commit |
| `releaseCommitish` | false | Any branch or commit SHA the Git tag is created from, unused if the Git tag already exists. | string | SHA of current commit |
| `owner` | false | The account owner of the repository the release will be uploaded to. Requires `GITHUB_TOKEN` in env and a `releaseCommitish` target if it doesn't match the current repo. | string | owner of the current repo |
| `repo` | false | The name of the repository the release will be uploaded to. Requires `GITHUB_TOKEN` in env and a `releaseCommitish` target if it doesn't match the current repo. | string | name of the current repo |

Expand All @@ -149,7 +149,9 @@ These inputs allow you to modify the GitHub release.
- When your Tauri app is not in the root of the repo, use the `projectPath` input.
- Usually it will work without it, but the action will install and use a global `@tauri-apps/cli` installation instead of your project's CLI which can cause issues if you also configured `tauriScript` or if you have multiple `tauri.conf.json` files in your repo.
- Additionally, relative paths provided via the `--config` flag will be resolved relative to the `projectPath` to match Tauri's behavior.
- If `releaseId` is set, the action will use this release to upload assets to. If `tagName` is set the action will try to find an existing release for that tag. If there's none, the action requires `releaseName` to create a new release for the specified `tagName`.
- If you create the release yourself and provide a `releaseId` but do not set `tagName`, the download url for updater bundles in `latest.json` will point to `releases/latest/download/<bundle>` which can cause issues if your repo contains releases that do not include updater bundles.
- If you provide a `tagName` to an existing release, `releaseDraft` must be set to `true` if the existing release is a draft.
- If you only want to build the app without having the action upload any assets, for example if you want to only use [`actions/upload-artifact`](https://github.com/actions/upload-artifact), simply omit `tagName`, `releaseName` and `releaseId`.

## Partners
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ inputs:
releaseId:
description: 'The id of the release to upload artifacts as release assets'
tagName:
description: 'The tag name of the release to create'
description: 'The tag name of the release to create or upload to'
releaseName:
description: 'The name of the release to create'
releaseBody:
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 19 additions & 13 deletions src/create-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ function allReleases(
);
}

export async function createRelease(
/// Try to get release by tag. If there's none, releaseName is required to create one.
export async function getOrCreateRelease(
owner: string,
repo: string,
tagName: string,
releaseName: string,
releaseName?: string,
body?: string,
commitish?: string,
draft = true,
Expand Down Expand Up @@ -91,18 +92,23 @@ export async function createRelease(
// @ts-expect-error Catching errors in typescript is a headache
if (error.status === 404 || error.message === 'release not found') {
console.log(`Couldn't find release with tag ${tagName}. Creating one.`);
const createdRelease = await github.rest.repos.createRelease({
owner,
repo,
tag_name: tagName,
name: releaseName,
body: bodyFileContent || body,
draft,
prerelease,
target_commitish: commitish || context.sha,
});

release = createdRelease.data;
if (!releaseName) {
console.error('"releaseName" not set but required to create release.');
} else {
const createdRelease = await github.rest.repos.createRelease({
owner,
repo,
tag_name: tagName,
name: releaseName,
body: bodyFileContent || body,
draft,
prerelease,
target_commitish: commitish || context.sha,
});

release = createdRelease.data;
}
} else {
console.log(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
Expand Down
21 changes: 7 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as core from '@actions/core';
import { context } from '@actions/github';
import stringArgv from 'string-argv';

import { createRelease } from './create-release';
import { getOrCreateRelease } from './create-release';
import { uploadAssets as uploadReleaseAssets } from './upload-release-assets';
import { uploadVersionJSON } from './upload-version-json';
import { buildProject } from './build';
Expand Down Expand Up @@ -49,16 +49,6 @@ async function run(): Promise<void> {
const updaterJsonPreferNsis =
core.getInput('updaterJsonPreferNsis')?.toLowerCase() === 'true';

// If releaseId is set we'll use this to upload the assets to.
// If tagName is set we also require releaseName to create a new release.
// If neither releaseId nor tagName are set we won't try to upload anything at the end.
if (!releaseId) {
if (Boolean(tagName) && !releaseName)
throw new Error(
'`releaseName` is required if `tagName` is set when creating a release.',
);
}

const buildOptions: BuildOptions = {
tauriScript,
args,
Expand Down Expand Up @@ -111,7 +101,7 @@ async function run(): Promise<void> {
const artifacts = releaseArtifacts.concat(debugArtifacts);

if (artifacts.length === 0) {
if (releaseId || tagName || releaseName) {
if (releaseId || tagName) {
throw new Error('No artifacts were found.');
} else {
console.log(
Expand Down Expand Up @@ -161,6 +151,9 @@ async function run(): Promise<void> {
}
}

// If releaseId is set we'll use this to upload the assets to.
// If tagName is set we will try to upload assets to the release associated with the given tagName.
// If there's no release for that tag, we require releaseName to create a new one.
if (tagName && !releaseId) {
const templates = [
{
Expand All @@ -176,11 +169,11 @@ async function run(): Promise<void> {
body = body.replace(regex, template.value);
});

const releaseData = await createRelease(
const releaseData = await getOrCreateRelease(
owner,
repo,
tagName,
releaseName,
releaseName || undefined,
body,
commitish || undefined,
draft,
Expand Down
2 changes: 1 addition & 1 deletion src/upload-release-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function uploadAssets(

console.log(`Uploading ${assetName}...`);

await github.rest.repos.uploadReleaseAsset({
return await github.rest.repos.uploadReleaseAsset({
headers,
name: assetName,
// https://github.com/tauri-apps/tauri-action/pull/45
Expand Down

0 comments on commit fb76925

Please sign in to comment.