Skip to content

Commit b659124

Browse files
Daniel-Aaron-BloomDaniel Bloom
andauthored
feat: add detection for Cloud Run Jobs (#2120)
Co-authored-by: Daniel Bloom <Daniel.Aaron.Bloom@gmail.com>
1 parent b97321f commit b659124

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/auth/envDetect.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export enum GCPEnv {
2020
CLOUD_FUNCTIONS = 'CLOUD_FUNCTIONS',
2121
COMPUTE_ENGINE = 'COMPUTE_ENGINE',
2222
CLOUD_RUN = 'CLOUD_RUN',
23+
CLOUD_RUN_JOBS = 'CLOUD_RUN_JOBS',
2324
NONE = 'NONE',
2425
}
2526

@@ -48,6 +49,8 @@ async function getEnvMemoized(): Promise<GCPEnv> {
4849
env = GCPEnv.KUBERNETES_ENGINE;
4950
} else if (isCloudRun()) {
5051
env = GCPEnv.CLOUD_RUN;
52+
} else if (isCloudRunJob()) {
53+
env = GCPEnv.CLOUD_RUN_JOBS;
5154
} else {
5255
env = GCPEnv.COMPUTE_ENGINE;
5356
}
@@ -74,6 +77,10 @@ function isCloudRun() {
7477
return !!process.env.K_CONFIGURATION;
7578
}
7679

80+
function isCloudRunJob() {
81+
return !!process.env.CLOUD_RUN_JOB;
82+
}
83+
7784
async function isKubernetesEngine() {
7885
try {
7986
await gcpMetadata.instance('attributes/cluster-name');

test/test.googleauth.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,14 @@ describe('googleauth', () => {
14491449
assert.strictEqual(env, envDetect.GCPEnv.CLOUD_RUN);
14501450
});
14511451

1452+
it('should get the current environment if Cloud Run Jobs', async () => {
1453+
envDetect.clear();
1454+
mockEnvVar('CLOUD_RUN_JOB', 'KITTY');
1455+
const {auth} = mockGCE();
1456+
const env = await auth.getEnv();
1457+
assert.strictEqual(env, envDetect.GCPEnv.CLOUD_RUN_JOBS);
1458+
});
1459+
14521460
it('should make the request via `#fetch`', async () => {
14531461
const url = 'http://example.com';
14541462
const {auth, scopes} = mockGCE();

0 commit comments

Comments
 (0)