From 3c734e5af5e8710238f956b9fc9811cea26fd383 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Thu, 23 Jun 2022 16:55:14 +0300 Subject: [PATCH] Add support for working with tag pushes Change-type: minor --- src/action.ts | 14 ++++++++++++-- src/balena-utils.ts | 20 +++++++++++++------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/action.ts b/src/action.ts index 020f2add..2286302a 100644 --- a/src/action.ts +++ b/src/action.ts @@ -90,11 +90,21 @@ export async function run( let buildOptions = null; // If we are pushing directly to the target branch then just build a release without draft flag - if (context.eventName === 'push' && context.ref === `refs/heads/${target}`) { + if ( + context.eventName === 'push' && + (context.ref === `refs/heads/${target}` || + context.ref.startsWith('refs/tags/')) + ) { + // TODO: Update this to use ref_type & ref_name once that becomes available + // See: https://github.com/actions/toolkit/pull/935/files + const tagName = context.ref.match(/^refs\/tags\/(.+)$/)?.[1]; // Make a final release because context is master workflow buildOptions = { draft: false, - tags: { sha: context.sha }, + tags: { + sha: context.sha, + ...(!!tagName && { tag: tagName }), + }, }; } else if (context.eventName !== 'pull_request') { // Make sure the only events now are Pull Requests diff --git a/src/balena-utils.ts b/src/balena-utils.ts index c5890691..91f1d907 100644 --- a/src/balena-utils.ts +++ b/src/balena-utils.ts @@ -5,9 +5,16 @@ import * as balena from 'balena-sdk'; import { Release } from './types'; +const TagKeyMap = { + sha: 'balena-ci-commit-sha', + pullRequestId: 'balena-ci-id', + tag: 'balena-ci-git-tag', +}; + type Tags = { sha: string; pullRequestId?: number; + tag?: string; }; type BuildOptions = { @@ -76,15 +83,14 @@ export async function push( '--source', source, '--release-tag', - 'balena-ci-commit-sha', - buildOpt.tags.sha, + ...Object.entries(buildOpt.tags).flatMap(([key, value]) => [ + TagKeyMap[key as keyof typeof TagKeyMap], + typeof value === 'string' && value.includes(' ') + ? `"${value}"` + : String(value), + ]), ]; - if (buildOpt.tags.pullRequestId) { - pushOpt.push('balena-ci-id'); - pushOpt.push(buildOpt.tags.pullRequestId.toString()); - } - if (buildOpt.draft) { pushOpt.push('--draft'); }