Skip to content

Commit ff973c4

Browse files
authored
Merge pull request #27 from s0/feature/tags
Implement Tags feature
2 parents 5111e9e + 0186901 commit ff973c4

File tree

5 files changed

+168
-15
lines changed

5 files changed

+168
-15
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ All configuration options are passed in via `env`, as environment variables.
169169
| `GITHUB_TOKEN` | Should always be equal to `${{ secrets.GITHUB_TOKEN }}` | When `REPO = self` |
170170
| `SQUASH_HISTORY` | If set to `true`, all previous commits on the target branch will be discarded. For example, if you are deploying a static site with lots of binary artifacts, this can help the repository becoming overly bloated. | No |
171171
| `MESSAGE` | A custom template to use as the commit message pushed to the target branch. See [custom commit messages](#custom-commit-messages). | No |
172+
| `TAG` | A string following the [git-check-ref-format](https://git-scm.com/docs/git-check-ref-format) that tags the commit with a lightweight git-tag. | No |
172173

173174
## Custom commit messages
174175

Diff for: action/dist/index.js

+24-8
Original file line numberDiff line numberDiff line change
@@ -8017,6 +8017,7 @@ var config = (function () {
80178017
var folder = ENV.FOLDER;
80188018
var squashHistory = ENV.SQUASH_HISTORY === 'true';
80198019
var message = ENV.MESSAGE || DEFAULT_MESSAGE;
8020+
var tag = ENV.TAG;
80208021
// Determine the type of URL
80218022
if (repo === REPO_SELF) {
80228023
if (!ENV.GITHUB_TOKEN)
@@ -8031,6 +8032,7 @@ var config = (function () {
80318032
squashHistory: squashHistory,
80328033
mode: 'self',
80338034
message: message,
8035+
tag: tag,
80348036
};
80358037
return config_1;
80368038
}
@@ -8048,6 +8050,7 @@ var config = (function () {
80488050
privateKey: ENV.SSH_PRIVATE_KEY,
80498051
knownHostsFile: ENV.KNOWN_HOSTS_FILE,
80508052
message: message,
8053+
tag: tag,
80518054
};
80528055
return config_2;
80538056
}
@@ -8082,7 +8085,7 @@ var writeToProcess = function (command, args, opts) { return new Promise(functio
80828085
});
80838086
}); };
80848087
(function () { return __awaiter(void 0, void 0, void 0, function () {
8085-
var TMP_PATH, REPO_TEMP, SSH_AUTH_SOCK, event, _a, _b, name, email, getGitInformation, gitInfo, env, known_hosts, sshAgentMatch, _c, _d, branchCheck, folder, message, forceArg, push;
8088+
var TMP_PATH, REPO_TEMP, SSH_AUTH_SOCK, event, _a, _b, name, email, tag, getGitInformation, gitInfo, env, known_hosts, sshAgentMatch, _c, _d, branchCheck, folder, message, forceArg, tagsArg, push;
80868089
var _e, _f;
80878090
return __generator(this, function (_g) {
80888091
switch (_g.label) {
@@ -8099,6 +8102,7 @@ var writeToProcess = function (command, args, opts) { return new Promise(functio
80998102
event = _b.apply(_a, [(_g.sent()).toString()]);
81008103
name = ((_e = event.pusher) === null || _e === void 0 ? void 0 : _e.name) || ENV.GITHUB_ACTOR || 'Git Publish Subdirectory';
81018104
email = ((_f = event.pusher) === null || _f === void 0 ? void 0 : _f.email) || (ENV.GITHUB_ACTOR ? ENV.GITHUB_ACTOR + "@users.noreply.github.com" : 'nobody@nowhere');
8105+
tag = ENV.TAG;
81028106
// Set Git Config
81038107
return [4 /*yield*/, exec("git config --global user.name \"" + name + "\"")];
81048108
case 3:
@@ -8294,24 +8298,36 @@ var writeToProcess = function (command, args, opts) { return new Promise(functio
82948298
fs: fs,
82958299
dir: REPO_TEMP,
82968300
message: message,
8297-
author: { email: email, name: name }
8301+
author: { email: email, name: name },
82988302
})];
82998303
case 28:
83008304
_g.sent();
8305+
if (!tag) return [3 /*break*/, 30];
8306+
console.log("##[info] Tagging commit with " + tag);
8307+
return [4 /*yield*/, isomorphic_git_1.default.tag({
8308+
fs: fs,
8309+
dir: REPO_TEMP,
8310+
ref: tag,
8311+
})];
8312+
case 29:
8313+
_g.sent();
8314+
_g.label = 30;
8315+
case 30:
83018316
console.log("##[info] Pushing");
83028317
forceArg = config.squashHistory ? '-f' : '';
8303-
return [4 /*yield*/, exec("git push " + forceArg + " origin \"" + config.branch + "\"", { env: env, cwd: REPO_TEMP })];
8304-
case 29:
8318+
tagsArg = tag ? '--tags' : '';
8319+
return [4 /*yield*/, exec("git push " + forceArg + " origin \"" + config.branch + "\" " + tagsArg, { env: env, cwd: REPO_TEMP })];
8320+
case 31:
83058321
push = _g.sent();
83068322
console.log(push.stdout);
83078323
console.log("##[info] Deployment Successful");
8308-
if (!(config.mode === 'ssh')) return [3 /*break*/, 31];
8324+
if (!(config.mode === 'ssh')) return [3 /*break*/, 33];
83098325
console.log("##[info] Killing ssh-agent");
83108326
return [4 /*yield*/, exec("ssh-agent -k", { env: env })];
8311-
case 30:
8327+
case 32:
83128328
_g.sent();
8313-
_g.label = 31;
8314-
case 31: return [2 /*return*/];
8329+
_g.label = 33;
8330+
case 33: return [2 /*return*/];
83158331
}
83168332
});
83178333
}); })().catch(function (err) {

Diff for: action/src/index.ts

+25-7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export interface EnvironmentVariables {
5656
* * `{msg}` - the commit message for the HEAD of the current branch
5757
*/
5858
MESSAGE?: string;
59+
/**
60+
* An optional string in git-check-ref-format to use for tagging the commit
61+
*/
62+
TAG?: string;
5963

6064
// Implicit environment variables passed by GitHub
6165

@@ -67,7 +71,7 @@ export interface EnvironmentVariables {
6771

6872
declare global {
6973
namespace NodeJS {
70-
interface ProcessEnv extends EnvironmentVariables {}
74+
interface ProcessEnv extends EnvironmentVariables { }
7175
}
7276
}
7377

@@ -116,6 +120,7 @@ interface BaseConfig {
116120
repo: string;
117121
squashHistory: boolean;
118122
message: string;
123+
tag?: string;
119124
}
120125

121126
interface SshConfig extends BaseConfig {
@@ -154,6 +159,7 @@ const config: Config = (() => {
154159
const folder = ENV.FOLDER;
155160
const squashHistory = ENV.SQUASH_HISTORY === 'true';
156161
const message = ENV.MESSAGE || DEFAULT_MESSAGE;
162+
const tag = ENV.TAG;
157163

158164
// Determine the type of URL
159165
if (repo === REPO_SELF) {
@@ -169,6 +175,7 @@ const config: Config = (() => {
169175
squashHistory,
170176
mode: 'self',
171177
message,
178+
tag,
172179
};
173180
return config;
174181
}
@@ -187,13 +194,14 @@ const config: Config = (() => {
187194
privateKey: ENV.SSH_PRIVATE_KEY,
188195
knownHostsFile: ENV.KNOWN_HOSTS_FILE,
189196
message,
197+
tag,
190198
};
191199
return config;
192200
}
193201
throw new Error('Unsupported REPO URL');
194202
})();
195203

196-
const writeToProcess = (command: string, args: string[], opts: {env: { [id: string]: string | undefined }; data: string;} ) => new Promise((resolve, reject) => {
204+
const writeToProcess = (command: string, args: string[], opts: { env: { [id: string]: string | undefined }; data: string; }) => new Promise((resolve, reject) => {
197205
const child = child_process.spawn(command, args, {
198206
env: opts.env,
199207
stdio: "pipe"
@@ -236,6 +244,7 @@ const writeToProcess = (command: string, args: string[], opts: {env: { [id: stri
236244

237245
const name = event.pusher?.name || ENV.GITHUB_ACTOR || 'Git Publish Subdirectory';
238246
const email = event.pusher?.email || (ENV.GITHUB_ACTOR ? `${ENV.GITHUB_ACTOR}@users.noreply.github.com` : 'nobody@nowhere');
247+
const tag = ENV.TAG
239248

240249
// Set Git Config
241250
await exec(`git config --global user.name "${name}"`);
@@ -309,13 +318,13 @@ const writeToProcess = (command: string, args: string[], opts: {env: { [id: stri
309318
if (!known_hosts) {
310319
console.warn(KNOWN_HOSTS_WARNING);
311320
} else {
312-
await mkdir(SSH_FOLDER, {recursive: true});
321+
await mkdir(SSH_FOLDER, { recursive: true });
313322
await copyFile(known_hosts, KNOWN_HOSTS_TARGET);
314323
}
315324

316325
// Setup ssh-agent with private key
317326
console.log(`Setting up ssh-agent on ${SSH_AUTH_SOCK}`);
318-
const sshAgentMatch = SSH_AGENT_PID_EXTRACT.exec((await exec(`ssh-agent -a ${SSH_AUTH_SOCK}`, {env})).stdout);
327+
const sshAgentMatch = SSH_AGENT_PID_EXTRACT.exec((await exec(`ssh-agent -a ${SSH_AUTH_SOCK}`, { env })).stdout);
319328
/* istanbul ignore if */
320329
if (!sshAgentMatch)
321330
throw new Error('Unexpected output from ssh-agent');
@@ -358,7 +367,7 @@ const writeToProcess = (command: string, args: string[], opts: {env: { [id: stri
358367

359368
// Check if branch already exists
360369
console.log(`##[info] Checking if branch ${config.branch} exists already`);
361-
const branchCheck = await exec(`git branch --list "${config.branch}"`, {env, cwd: REPO_TEMP });
370+
const branchCheck = await exec(`git branch --list "${config.branch}"`, { env, cwd: REPO_TEMP });
362371
if (branchCheck.stdout.trim() === '') {
363372
// Branch does not exist yet, let's check it out as an orphan
364373
console.log(`##[info] ${config.branch} does not exist, creating as orphan`);
@@ -396,11 +405,20 @@ const writeToProcess = (command: string, args: string[], opts: {env: { [id: stri
396405
fs,
397406
dir: REPO_TEMP,
398407
message,
399-
author: { email, name }
408+
author: { email, name },
400409
});
410+
if (tag) {
411+
console.log(`##[info] Tagging commit with ${tag}`)
412+
await git.tag({
413+
fs,
414+
dir: REPO_TEMP,
415+
ref: tag,
416+
});
417+
}
401418
console.log(`##[info] Pushing`);
402419
const forceArg = config.squashHistory ? '-f' : '';
403-
const push = await exec(`git push ${forceArg} origin "${config.branch}"`, { env, cwd: REPO_TEMP });
420+
const tagsArg = tag ? '--tags' : '';
421+
const push = await exec(`git push ${forceArg} origin "${config.branch}" ${tagsArg}`, { env, cwd: REPO_TEMP });
404422
console.log(push.stdout);
405423
console.log(`##[info] Deployment Successful`);
406424

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Test custom tags 1`] = `
4+
"msg:This is another commit follow up with no content changes
5+
6+
tree:8bf87c66655949e66937b11593cc4ae732d1f610
7+
author:s0 <s0@users.noreply.github.com>
8+
msg:Update branch-a to output generated at <sha>
9+
10+
tree:8bf87c66655949e66937b11593cc4ae732d1f610
11+
author:s0 <s0@users.noreply.github.com>"
12+
`;
13+
14+
exports[`Test custom tags 2`] = `
15+
"msg:This is another commit follow up with no content changes
16+
17+
tree:8bf87c66655949e66937b11593cc4ae732d1f610
18+
author:s0 <s0@users.noreply.github.com>
19+
msg:Update branch-a to output generated at <sha>
20+
21+
tree:8bf87c66655949e66937b11593cc4ae732d1f610
22+
author:s0 <s0@users.noreply.github.com>"
23+
`;

Diff for: action/test/specs/ssh-custom-tags.spec.ts

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
import git from 'isomorphic-git';
4+
5+
import * as util from '../util';
6+
7+
const REPO_DIR = path.join(util.REPOS_DIR, 'ssh-custom-tags.git');
8+
const DATA_DIR = path.join(util.DATA_DIR, 'ssh-custom-tags');
9+
10+
it('Test custom tags', async () => {
11+
12+
// Create empty repo
13+
await util.mkdir(REPO_DIR);
14+
await util.execWithOutput('git init --bare', { cwd: REPO_DIR });
15+
16+
// Create dummy data
17+
await util.mkdir(DATA_DIR);
18+
await util.mkdir(path.join(DATA_DIR, 'dummy'));
19+
await util.writeFile(path.join(DATA_DIR, 'dummy', 'baz'), 'foobar');
20+
await util.writeFile(path.join(DATA_DIR, 'dummy', '.bat'), 'foobar');
21+
22+
// Run Action
23+
await util.runWithGithubEnv(
24+
path.basename(__filename),
25+
{
26+
REPO: 'ssh://git@git-ssh/git-server/repos/ssh-custom-tags.git',
27+
BRANCH: 'branch-a',
28+
FOLDER: DATA_DIR,
29+
SSH_PRIVATE_KEY: (await util.readFile(util.SSH_PRIVATE_KEY)).toString(),
30+
KNOWN_HOSTS_FILE: util.KNOWN_HOSTS,
31+
},
32+
's0/test',
33+
{},
34+
's0',
35+
);
36+
// Run the action again to make sure that a commit is added even when there are
37+
// no content changes
38+
await util.runWithGithubEnv(
39+
path.basename(__filename),
40+
{
41+
REPO: 'ssh://git@git-ssh/git-server/repos/ssh-custom-tags.git',
42+
BRANCH: 'branch-a',
43+
FOLDER: DATA_DIR,
44+
SSH_PRIVATE_KEY: (await util.readFile(util.SSH_PRIVATE_KEY)).toString(),
45+
KNOWN_HOSTS_FILE: util.KNOWN_HOSTS,
46+
MESSAGE: 'This is another commit follow up with no content changes',
47+
TAG: 'foo-bar-tag-v0.1.2',
48+
},
49+
's0/test',
50+
{},
51+
's0',
52+
);
53+
54+
{
55+
// Check that the log of the branch is as expected
56+
let log = (await util.exec(
57+
'git log --pretty="format:msg:%B%ntree:%T%nauthor:%an <%ae>" branch-a',
58+
{
59+
cwd: REPO_DIR
60+
}
61+
)).stdout;
62+
const fullSha = await util.getFullRepoSha();
63+
const sha = fullSha.substr(0, 7);
64+
const cleanedLog = log.replace(fullSha, '<long-sha>').replace(sha, '<sha>');
65+
expect(cleanedLog).toMatchSnapshot();
66+
}
67+
68+
{
69+
// Check that the log got the tag is also as expected
70+
let log = (await util.exec(
71+
'git log --pretty="format:msg:%B%ntree:%T%nauthor:%an <%ae>" foo-bar-tag-v0.1.2',
72+
{
73+
cwd: REPO_DIR
74+
}
75+
)).stdout;
76+
const fullSha = await util.getFullRepoSha();
77+
const sha = fullSha.substr(0, 7);
78+
const cleanedLog = log.replace(fullSha, '<long-sha>').replace(sha, '<sha>');
79+
expect(cleanedLog).toMatchSnapshot();
80+
}
81+
82+
// Ensure that commits for branch and tag are identical
83+
const tagSha = await git.resolveRef({
84+
fs,
85+
gitdir: REPO_DIR,
86+
ref: 'refs/tags/foo-bar-tag-v0.1.2',
87+
});
88+
const branchSha = await git.resolveRef({
89+
fs,
90+
gitdir: REPO_DIR,
91+
ref: 'refs/heads/branch-a',
92+
});
93+
expect(tagSha).toEqual(branchSha);
94+
95+
});

0 commit comments

Comments
 (0)