feat: add deploy to custom repo (#457)

* add owner and repo

* fix error

* update dist

* fix

* add releaseCommitish info

* refactor

* refactor

* wording

* rebuild dist

---------

Co-authored-by: FabianLars <fabianlars@fabianlars.de>
This commit is contained in:
Pavel Kuzmin
2023-05-09 18:33:44 +05:00
committed by GitHub
parent e3735934ba
commit 683dc8624e
8 changed files with 58 additions and 30 deletions

5
.changes/custom-repo.md Normal file
View File

@@ -0,0 +1,5 @@
---
'action': patch
---
Add support for modifying the target repo for the release.

View File

@@ -217,15 +217,17 @@ These inputs allow you to change how your Tauri project will be build.
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 | string | |
| `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 | |
| `releaseBody` | false | The body of the release to create | string | |
| `releaseDraft` | false | Whether the release to 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 |
| Name | Required | Description | Type | Default |
| ------------------ | :------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------ | ------------------------- |
| `releaseId` | false | The id of the release to upload artifacts as release assets | string | |
| `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 | |
| `releaseBody` | false | The body of the release to create | string | |
| `releaseDraft` | false | Whether the release to 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 |
| `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 |
## Outputs

View File

@@ -47,6 +47,10 @@ inputs:
description: 'arguments for the tauri command'
bundleIdentifier:
description: 'the bundle identifier to inject when initializing the Tauri app'
owner:
description: 'The account owner of the repository'
repo:
description: 'The name of the repository'
outputs:
releaseId:
description: 'The ID of the created release'

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -27,6 +27,8 @@ function allReleases(
}
export async function createRelease(
owner: string,
repo: string,
tagName: string,
releaseName: string,
body?: string,
@@ -41,9 +43,6 @@ export async function createRelease(
// Get authenticated GitHub client (Ocktokit): https://github.com/actions/toolkit/tree/master/packages/github#usage
const github = getOctokit(process.env.GITHUB_TOKEN);
// Get owner and repo from context of payload that triggered the action
const { owner, repo } = context.repo;
const bodyPath = core.getInput('body_path', { required: false });
let bodyFileContent: string | null = null;
if (bodyPath !== '' && !!bodyPath) {

View File

@@ -2,6 +2,7 @@ import { existsSync } from 'fs';
import { resolve, dirname, basename } from 'path';
import * as core from '@actions/core';
import { context } from '@actions/github';
import stringArgv from 'string-argv';
import { createRelease } from './create-release';
@@ -34,6 +35,8 @@ async function run(): Promise<void> {
let releaseId = Number(core.getInput('releaseId'));
let releaseName = core.getInput('releaseName').replace('refs/tags/', '');
let body = core.getInput('releaseBody');
const owner = core.getInput('owner') || context.repo.owner;
const repo = core.getInput('repo') || context.repo.repo;
const draft = core.getBooleanInput('releaseDraft');
const prerelease = core.getBooleanInput('prerelease');
const commitish = core.getInput('releaseCommitish') || null;
@@ -113,6 +116,8 @@ async function run(): Promise<void> {
});
const releaseData = await createRelease(
owner,
repo,
tagName,
releaseName,
body,
@@ -152,10 +157,12 @@ async function run(): Promise<void> {
}
}
await uploadReleaseAssets(releaseId, artifacts);
await uploadReleaseAssets(owner, repo, releaseId, artifacts);
if (includeUpdaterJson) {
await uploadVersionJSON({
owner,
repo,
version: info.version,
notes: body,
tagName,

View File

@@ -1,11 +1,16 @@
import fs from 'fs';
import { getOctokit, context } from '@actions/github';
import { getOctokit } from '@actions/github';
import { getAssetName } from './utils';
import type { Artifact } from './types';
export async function uploadAssets(releaseId: number, assets: Artifact[]) {
export async function uploadAssets(
owner: string,
repo: string,
releaseId: number,
assets: Artifact[]
) {
if (process.env.GITHUB_TOKEN === undefined) {
throw new Error('GITHUB_TOKEN is required');
}
@@ -14,8 +19,8 @@ export async function uploadAssets(releaseId: number, assets: Artifact[]) {
const existingAssets = (
await github.rest.repos.listReleaseAssets({
owner: context.repo.owner,
repo: context.repo.repo,
owner: owner,
repo: repo,
release_id: releaseId,
per_page: 50,
})
@@ -38,8 +43,8 @@ export async function uploadAssets(releaseId: number, assets: Artifact[]) {
if (existingAsset) {
console.log(`Deleting existing ${assetName}...`);
await github.rest.repos.deleteReleaseAsset({
owner: context.repo.owner,
repo: context.repo.repo,
owner: owner,
repo: repo,
asset_id: existingAsset.id,
});
}
@@ -52,8 +57,8 @@ export async function uploadAssets(releaseId: number, assets: Artifact[]) {
// https://github.com/tauri-apps/tauri-action/pull/45
// @ts-ignore error TS2322: Type 'Buffer' is not assignable to type 'string'.
data: fs.readFileSync(asset.path),
owner: context.repo.owner,
repo: context.repo.repo,
owner: owner,
repo: repo,
release_id: releaseId,
});
}

View File

@@ -1,7 +1,7 @@
import { readFileSync, writeFileSync } from 'fs';
import { resolve } from 'path';
import { getOctokit, context } from '@actions/github';
import { getOctokit } from '@actions/github';
import { uploadAssets } from './upload-release-assets';
import { getAssetName } from './utils';
@@ -23,6 +23,8 @@ type VersionContent = {
};
export async function uploadVersionJSON({
owner,
repo,
version,
notes,
tagName,
@@ -32,6 +34,8 @@ export async function uploadVersionJSON({
updaterJsonPreferNsis,
updaterJsonKeepUniversal,
}: {
owner: string;
repo: string;
version: string;
notes: string;
tagName: string;
@@ -57,8 +61,8 @@ export async function uploadVersionJSON({
};
const assets = await github.rest.repos.listReleaseAssets({
owner: context.repo.owner,
repo: context.repo.repo,
owner: owner,
repo: repo,
release_id: releaseId,
per_page: 50,
});
@@ -69,8 +73,8 @@ export async function uploadVersionJSON({
await github.request(
'GET /repos/{owner}/{repo}/releases/assets/{asset_id}',
{
owner: context.repo.owner,
repo: context.repo.repo,
owner: owner,
repo: repo,
asset_id: asset.id,
headers: {
accept: 'application/octet-stream',
@@ -162,15 +166,17 @@ export async function uploadVersionJSON({
if (asset) {
// https://docs.github.com/en/rest/releases/assets#update-a-release-asset
await github.rest.repos.deleteReleaseAsset({
owner: context.repo.owner,
repo: context.repo.repo,
owner: owner,
repo: repo,
release_id: releaseId,
asset_id: asset.id,
});
}
console.log(`Uploading ${versionFile}...`);
await uploadAssets(releaseId, [{ path: versionFile, arch: '' }]);
await uploadAssets(owner, repo, releaseId, [
{ path: versionFile, arch: '' },
]);
} else {
const missing = downloadUrl
? 'Signature'