Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
parseBoolean,
parseGcloudIgnore,
pinnedToHeadWarning,
toPosixPath,
} from '@google-github-actions/actions-utils';
import ignore from 'ignore';

Expand Down Expand Up @@ -114,16 +115,25 @@ export async function run(): Promise<void> {
}

for (let i = 0; i < files.length; i++) {
const name = path.join(root, files[i]);
try {
if (ignores.ignores(name)) {
core.debug(`Ignoring ${name} because of ignore file`);
files.splice(i, 1);
i--;
}
} catch (err) {
const msg = errorMessage(err);
core.error(`Failed to process ignore for ${name}, skipping: ${msg}`);
// The ignore file is relative to the repository root, so the value
// we test has to be as well. Building it from "root" breaks when
// "path" is absolute, because "ignore" rejects absolute paths.
const rel = path.relative(githubWorkspace, path.resolve(absoluteRoot, files[i]));
if (path.isAbsolute(rel) || rel === '..' || rel.startsWith(`..${path.sep}`)) {
core.error(
`Ignoring ${files[i]} because it is outside of $GITHUB_WORKSPACE and ` +
`cannot be matched against ${gcloudIgnorePath}`,
);
files.splice(i, 1);
i--;
continue;
}

const name = toPosixPath(rel);
if (ignores.ignores(name)) {
core.debug(`Ignoring ${name} because of ignore file`);
files.splice(i, 1);
i--;
}
}
} else {
Expand Down
52 changes: 52 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,56 @@ 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 gcloudignore for an absolute path', async (t) => {
const uploadMock = t.mock.method(Bucket.prototype, 'upload', mockUpload);

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

// Add gcloudignore
await fs.writeFile(path.join(githubWorkspace, '.gcloudignore'), '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'),
]);
});

await suite.test('does not upload files outside of the workspace', async (t) => {
t.mock.method(core, 'error', () => {});
const uploadMock = t.mock.method(Bucket.prototype, 'upload', mockUpload);

const outsidePath = await fs.mkdtemp(path.join(os.tmpdir(), 'gha-outside-'));
await fs.writeFile(path.join(outsidePath, 'outside.txt'), 'hello');

setInputs({
path: outsidePath,
destination: 'my-bucket/sub/path',
process_gcloudignore: 'true',
});

// Add gcloudignore
await fs.writeFile(path.join(githubWorkspace, '.gcloudignore'), '**/*.txt');

await run();

// Check call sites
assert.deepStrictEqual(uploadMock.mock.calls.length, 0);

await forceRemove(outsidePath);
});
});