Skip to content

Commit aa65d0a

Browse files
Allow passing gcloudignore path and fix root computation (#376)
- Fixes #336 - Fixes #357 --------- Co-authored-by: Eddie Lin <eddielin0926@gmail.com>
1 parent 94471cf commit aa65d0a

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

action.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ inputs:
160160
required: false
161161
default: '100'
162162

163+
gcloudignore_path:
164+
description: |-
165+
Path to a gcloudignore file within the repository.
166+
167+
```yaml
168+
gcloudignore_path: '.gcloudignore.dev'
169+
```
170+
required: false
171+
default: '.gcloudignore'
172+
163173
process_gcloudignore:
164174
description: |-
165175
Process a `.gcloudignore` file present in the top-level of the repository.

src/main.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export async function run(): Promise<void> {
7171
const predefinedAcl =
7272
predefinedAclInput === '' ? undefined : (predefinedAclInput as PredefinedAcl);
7373
const headersInput = core.getInput('headers');
74+
const gcloudIgnorePath = core.getInput('gcloudignore_path') || '.gcloudignore';
7475
const processGcloudIgnore = parseBoolean(core.getInput('process_gcloudignore'));
7576
const metadata = headersInput === '' ? {} : parseHeadersInput(headersInput);
7677

@@ -89,18 +90,18 @@ export async function run(): Promise<void> {
8990
// - Format all files to be posix relative to input.path
9091
// - Filter out items that match
9192
if (processGcloudIgnore) {
92-
core.debug(`Processing gcloudignore`);
93+
core.debug(`Processing gcloudignore at ${gcloudIgnorePath}`);
9394

9495
const ignores = ignore();
9596

9697
// Look for a .gcloudignore in the repository root.
9798
const githubWorkspace = process.env.GITHUB_WORKSPACE;
9899
if (githubWorkspace) {
99-
const gcloudIgnorePath = path.join(githubWorkspace, '.gcloudignore');
100-
const ignoreList = await parseGcloudIgnore(gcloudIgnorePath);
100+
const gcloudIgnorePathAbs = path.join(githubWorkspace, gcloudIgnorePath);
101+
const ignoreList = await parseGcloudIgnore(gcloudIgnorePathAbs);
101102

102103
if (ignoreList && ignoreList.length) {
103-
core.debug(`Using .gcloudignore at: ${gcloudIgnorePath}`);
104+
core.debug(`Using .gcloudignore at: ${gcloudIgnorePathAbs}`);
104105
core.debug(`Parsed ignore list: ${JSON.stringify(ignoreList)}`);
105106

106107
ignores.add(ignoreList);
@@ -113,7 +114,7 @@ export async function run(): Promise<void> {
113114
}
114115

115116
for (let i = 0; i < files.length; i++) {
116-
const name = files[i];
117+
const name = path.join(root, files[i]);
117118
try {
118119
if (ignores.ignores(name)) {
119120
core.debug(`Ignoring ${name} because of ignore file`);

tests/client.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { forceRemove, randomFilepath, writeSecureFile } from '@google-github-act
2323

2424
import { Client } from '../src/client';
2525
import { Bucket, UploadOptions } from '@google-cloud/storage';
26+
import { GoogleAuth } from 'google-auth-library';
2627

2728
import { mockUpload } from './helpers.test';
2829

@@ -231,6 +232,7 @@ describe('Client', { concurrency: true }, async () => {
231232
test('#upload', async (suite) => {
232233
await suite.test('calls uploadFile', async (t) => {
233234
const uploadMock = t.mock.method(Bucket.prototype, 'upload', mockUpload);
235+
t.mock.method(GoogleAuth.prototype, 'getClient', () => {});
234236

235237
// Do the upload
236238
const client = await Client.build();

tests/main.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { promises as fs } from 'fs';
2424
import * as core from '@actions/core';
2525
import { clearEnv, forceRemove, setInputs } from '@google-github-actions/actions-utils';
2626
import { Bucket, UploadOptions } from '@google-cloud/storage';
27+
import { GoogleAuth } from 'google-auth-library';
2728

2829
import { mockUpload } from './helpers.test';
2930

@@ -47,6 +48,9 @@ test('#run', { concurrency: true }, async (suite) => {
4748
suite.mock.method(core, 'endGroup', () => {});
4849
suite.mock.method(core, 'addPath', () => {});
4950
suite.mock.method(core, 'exportVariable', () => {});
51+
52+
// We do not care about authentication in the unit tests
53+
suite.mock.method(GoogleAuth.prototype, 'getClient', () => {});
5054
});
5155

5256
suite.beforeEach(async () => {
@@ -194,7 +198,41 @@ test('#run', { concurrency: true }, async (suite) => {
194198
});
195199

196200
// Add gcloudignore
197-
await fs.writeFile(path.join(githubWorkspace, '.gcloudignore'), '*.txt');
201+
await fs.writeFile(path.join(githubWorkspace, '.gcloudignore'), 'testdata/**/*.txt');
202+
203+
await run();
204+
205+
// Check call sites
206+
const uploadedFiles = uploadMock.mock.calls.map((call) => call?.arguments?.at(0));
207+
assert.deepStrictEqual(uploadedFiles, [
208+
path.join(githubWorkspace, 'testdata', 'test.css'),
209+
path.join(githubWorkspace, 'testdata', 'test.js'),
210+
path.join(githubWorkspace, 'testdata', 'test.json'),
211+
path.join(githubWorkspace, 'testdata', 'testfile'),
212+
]);
213+
214+
// Check arguments
215+
const call = uploadMock.mock.calls.at(0)?.arguments?.at(1) as UploadOptions;
216+
assert.deepStrictEqual(call?.destination, 'sub/path/testdata/test.css');
217+
});
218+
219+
await suite.test('processes a custom gcloudignore path', async (t) => {
220+
const uploadMock = t.mock.method(Bucket.prototype, 'upload', mockUpload);
221+
const gcloudIgnorePath = path.join(githubWorkspace, '.gcloudignore-other');
222+
223+
setInputs({
224+
path: './testdata',
225+
destination: 'my-bucket/sub/path',
226+
gzip: 'true',
227+
resumable: 'true',
228+
parent: 'true',
229+
concurrency: '10',
230+
process_gcloudignore: 'true',
231+
gcloudignore_path: '.gcloudignore-other',
232+
});
233+
234+
// Add gcloudignore
235+
await fs.writeFile(gcloudIgnorePath, 'testdata/**/*.txt');
198236

199237
await run();
200238

0 commit comments

Comments
 (0)