Skip to content

Commit

Permalink
[eas-json] allow env vars in android.serviceAccountKeyPath (#604)
Browse files Browse the repository at this point in the history
* [eas-json] allow env vars in android.serviceAccountKeyPath

* update CHANGELOG
  • Loading branch information
dsokal authored Sep 10, 2021
1 parent cebd29e commit 22b06b4
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This is the log of notable changes to EAS CLI and related packages.
- Implement support for `--platform all` in `eas submit`. ([#598](https://github.com/expo/eas-cli/pull/598) by [@dsokal](https://github.com/dsokal))
- Implement support for `--non-interactive` in `eas submit`. ([#600](https://github.com/expo/eas-cli/pull/600) by [@dsokal](https://github.com/dsokal))
- Add auto-submit feature. Run `eas build --submit` to submit automatically on build complete. ([#603](https://github.com/expo/eas-cli/pull/603) by [@dsokal](https://github.com/dsokal))
- Allow using env var for `android.serviceAccountKeyPath` in submit profiles. ([#604](https://github.com/expo/eas-cli/pull/604) by [@dsokal](https://github.com/dsokal))

### 🐛 Bug fixes

Expand Down
1 change: 1 addition & 0 deletions packages/eas-json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"bugs": "https://github.com/expo/eas-cli/issues",
"dependencies": {
"@expo/eas-build-job": "0.2.46",
"env-string": "1.0.1",
"fs-extra": "10.0.0",
"joi": "17.4.2",
"tslib": "2.3.1"
Expand Down
27 changes: 25 additions & 2 deletions packages/eas-json/src/EasJsonReader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Platform } from '@expo/eas-build-job';
import envString from 'env-string';
import fs from 'fs-extra';
import path from 'path';

Expand All @@ -10,7 +11,11 @@ import {
IosSubmitProfileSchema,
MinimalEasJsonSchema,
} from './EasJsonSchema';
import { SubmitProfile } from './EasSubmit.types';
import {
AndroidSubmitProfileFieldsToEvaluate,
IosSubmitProfileFieldsToEvaluate,
SubmitProfile,
} from './EasSubmit.types';

interface EasJsonPreValidation {
build: { [profile: string]: object };
Expand Down Expand Up @@ -93,7 +98,7 @@ export class EasJsonReader {
if (!profile) {
throw new Error(`There is no profile named ${profileName} in eas.json for ${platform}.`);
}
return profile as SubmitProfile<T>;
return this.evaluateFields(platform, profile as SubmitProfile<T>);
}

public async readAndValidateAsync(): Promise<EasJson> {
Expand Down Expand Up @@ -151,6 +156,24 @@ export class EasJsonReader {
throw new Error(`There is no profile named ${profileName} in eas.json.`);
}
}

private evaluateFields<T extends Platform>(
platform: T,
profile: SubmitProfile<T>
): SubmitProfile<T> {
const fields =
platform === Platform.ANDROID
? AndroidSubmitProfileFieldsToEvaluate
: IosSubmitProfileFieldsToEvaluate;
const evaluatedProfile = { ...profile };
for (const field of fields) {
if (field in evaluatedProfile) {
// @ts-ignore
evaluatedProfile[field] = envString(evaluatedProfile[field], process.env);
}
}
return evaluatedProfile;
}
}

export function profileMerge(base: RawBuildProfile, update: RawBuildProfile): RawBuildProfile {
Expand Down
6 changes: 6 additions & 0 deletions packages/eas-json/src/EasSubmit.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export interface AndroidSubmitProfile {
changesNotSentForReview: boolean;
}

export const AndroidSubmitProfileFieldsToEvaluate: (keyof AndroidSubmitProfile)[] = [
'serviceAccountKeyPath',
];

export interface IosSubmitProfile {
appleId?: string;
ascAppId?: string;
Expand All @@ -31,6 +35,8 @@ export interface IosSubmitProfile {
appName?: string;
}

export const IosSubmitProfileFieldsToEvaluate: (keyof IosSubmitProfile)[] = [];

export type SubmitProfile<TPlatform extends Platform = Platform> =
TPlatform extends Platform.ANDROID
? AndroidSubmitProfile
Expand Down
29 changes: 29 additions & 0 deletions packages/eas-json/src/__tests__/EasJsonReader-submit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,35 @@ test('android config with all required values', async () => {
});
});

test('android config with serviceAccountKeyPath set to env var', async () => {
await fs.writeJson('/project/eas.json', {
submit: {
release: {
android: {
serviceAccountKeyPath: '$GOOGLE_SERVICE_ACCOUNT',
track: 'beta',
releaseStatus: 'completed',
},
},
},
});

try {
process.env.GOOGLE_SERVICE_ACCOUNT = './path.json';
const reader = new EasJsonReader('/project');
const androidProfile = await reader.readSubmitProfileAsync(Platform.ANDROID, 'release');

expect(androidProfile).toEqual({
serviceAccountKeyPath: './path.json',
track: 'beta',
releaseStatus: 'completed',
changesNotSentForReview: false,
});
} finally {
process.env.GOOGLE_SERVICE_ACCOUNT = undefined;
}
});

test('ios config with all required values', async () => {
await fs.writeJson('/project/eas.json', {
submit: {
Expand Down
3 changes: 3 additions & 0 deletions ts-declarations/env-string/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'env-string' {
export default function envString(str: string, env?: Record<string, string | undefined>): string;
}
5 changes: 5 additions & 0 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 22b06b4

Please sign in to comment.