Skip to content

Commit

Permalink
Allow passing a gcloudignore path
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Aug 23, 2024
1 parent d9d7d72 commit 2c3b47b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
10 changes: 10 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ inputs:
required: false
default: '100'

gcloudignore_path:
description: |-
Path to a file within the repository to a gcloudignore exists.
```yaml
gcloudignore_path: '.gcloudignore.dev'
```
required: false
default: '.gcloudignore'

process_gcloudignore:
description: |-
Process a `.gcloudignore` file present in the top-level of the repository.
Expand Down
9 changes: 5 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export async function run(): Promise<void> {
const predefinedAcl =
predefinedAclInput === '' ? undefined : (predefinedAclInput as PredefinedAcl);
const headersInput = core.getInput('headers');
const gcloudIgnorePath = core.getInput('gcloudignore_path') || '.gcloudignore';
const processGcloudIgnore = parseBoolean(core.getInput('process_gcloudignore'));
const metadata = headersInput === '' ? {} : parseHeadersInput(headersInput);

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

const ignores = ignore();

// Look for a .gcloudignore in the repository root.
const githubWorkspace = process.env.GITHUB_WORKSPACE;
if (githubWorkspace) {
const gcloudIgnorePath = path.join(githubWorkspace, '.gcloudignore');
const ignoreList = await parseGcloudIgnore(gcloudIgnorePath);
const gcloudIgnorePathAbs = path.join(githubWorkspace, gcloudIgnorePath);
const ignoreList = await parseGcloudIgnore(gcloudIgnorePathAbs);

if (ignoreList && ignoreList.length) {
core.debug(`Using .gcloudignore at: ${gcloudIgnorePath}`);
core.debug(`Using .gcloudignore at: ${gcloudIgnorePathAbs}`);
core.debug(`Parsed ignore list: ${JSON.stringify(ignoreList)}`);

ignores.add(ignoreList);
Expand Down
2 changes: 2 additions & 0 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { forceRemove, randomFilepath, writeSecureFile } from '@google-github-act

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

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

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

// Do the upload
const client = await Client.build();
Expand Down
38 changes: 38 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { promises as fs } from 'fs';
import * as core from '@actions/core';
import { clearEnv, forceRemove, setInputs } from '@google-github-actions/actions-utils';
import { Bucket, UploadOptions } from '@google-cloud/storage';
import { GoogleAuth } from 'google-auth-library';

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

Expand All @@ -47,6 +48,9 @@ test('#run', { concurrency: true }, async (suite) => {
suite.mock.method(core, 'endGroup', () => {});
suite.mock.method(core, 'addPath', () => {});
suite.mock.method(core, 'exportVariable', () => {});

// We do not care about authentication in the unit tests
suite.mock.method(GoogleAuth.prototype, 'getClient', () => {});
});

suite.beforeEach(async () => {
Expand Down Expand Up @@ -211,4 +215,38 @@ test('#run', { concurrency: true }, async (suite) => {
const call = uploadMock.mock.calls.at(0)?.arguments?.at(1) as UploadOptions;
assert.deepStrictEqual(call?.destination, 'sub/path/testdata/test.css');
});

await suite.test('processes a custom gcloudignore path', async (t) => {
const uploadMock = t.mock.method(Bucket.prototype, 'upload', mockUpload);
const gcloudIgnorePath = path.join(githubWorkspace, '.gcloudignore-other');

setInputs({
path: './testdata',
destination: 'my-bucket/sub/path',
gzip: 'true',
resumable: 'true',
parent: 'true',
concurrency: '10',
process_gcloudignore: 'true',
gcloudignore_path: '.gcloudignore-other',
});

// Add gcloudignore
await fs.writeFile(gcloudIgnorePath, 'testdata/**/*.txt');

await run();

// Check call sites
const uploadedFiles = uploadMock.mock.calls.map((call) => call?.arguments?.at(0));
assert.deepStrictEqual(uploadedFiles, [
path.join(githubWorkspace, 'testdata', 'test.css'),
path.join(githubWorkspace, 'testdata', 'test.js'),
path.join(githubWorkspace, 'testdata', 'test.json'),
path.join(githubWorkspace, 'testdata', 'testfile'),
]);

// Check arguments
const call = uploadMock.mock.calls.at(0)?.arguments?.at(1) as UploadOptions;
assert.deepStrictEqual(call?.destination, 'sub/path/testdata/test.css');
});
});

0 comments on commit 2c3b47b

Please sign in to comment.