Skip to content

Commit 3d83e86

Browse files
committed
fix: Exit with non zero status code when the deployment fails and poll for 1 second more before deployment logs polling stops
1 parent 116b506 commit 3d83e86

File tree

6 files changed

+95
-3
lines changed

6 files changed

+95
-3
lines changed

src/adapters/file-upload.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { print } from '../util';
1414
import BaseClass from './base-class';
1515
import { getFileList } from '../util/fs';
1616
import { createSignedUploadUrlMutation, importProjectMutation } from '../graphql';
17-
import { SignedUploadUrlData, FileUploadMethod } from '../types/launch';
17+
import { SignedUploadUrlData, FileUploadMethod, DeploymentStatus } from '../types/launch';
1818
import config from '../config';
1919

2020
export default class FileUpload extends BaseClass {
@@ -34,6 +34,9 @@ export default class FileUpload extends BaseClass {
3434

3535
this.prepareLaunchConfig();
3636
await this.showLogs();
37+
if(this.config.currentDeploymentStatus === DeploymentStatus.FAILED) {
38+
this.exit(1);
39+
}
3740
this.showDeploymentUrl();
3841
this.showSuggestion();
3942
}

src/adapters/github.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { print } from '../util';
1212
import BaseClass from './base-class';
1313
import { getRemoteUrls } from '../util/create-git-meta';
1414
import { repositoriesQuery, userConnectionsQuery, importProjectMutation } from '../graphql';
15+
import { DeploymentStatus } from '../types';
1516

1617
export default class GitHub extends BaseClass {
1718
/**
@@ -30,6 +31,9 @@ export default class GitHub extends BaseClass {
3031

3132
this.prepareLaunchConfig();
3233
await this.showLogs();
34+
if(this.config.currentDeploymentStatus === DeploymentStatus.FAILED) {
35+
this.exit(1);
36+
}
3337
this.showDeploymentUrl();
3438
this.showSuggestion();
3539
}

src/types/launch.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ export type Environment = {
8888
frameworkPreset: string;
8989
};
9090

91+
export enum DeploymentStatus {
92+
QUEUED = 'QUEUED',
93+
LIVE = 'LIVE',
94+
DEPLOYED = 'DEPLOYED',
95+
ARCHIVED = 'ARCHIVED',
96+
DEPLOYING = 'DEPLOYING',
97+
SKIPPED = 'SKIPPED',
98+
FAILED = 'FAILED',
99+
CANCELLED = 'CANCELLED',
100+
}
101+
91102
export {
92103
LogFn,
93104
ExitFn,

src/util/logs-polling-utilities.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ApolloClient, ObservableQuery } from '@apollo/client/core';
44

55
import { LogPollingInput, ConfigType } from '../types';
66
import { deploymentQuery, deploymentLogsQuery, serverlessLogsQuery } from '../graphql';
7+
import { setTimeout as sleep } from "timers/promises";
78

89
export default class LogPolling {
910
private config: ConfigType;
@@ -79,6 +80,7 @@ export default class LogPolling {
7980
}
8081
this.deploymentStatus = data?.Deployment?.status;
8182
if (this.config.deploymentStatus.includes(this.deploymentStatus)) {
83+
this.config.currentDeploymentStatus = this.deploymentStatus;
8284
statusWatchQuery.stopPolling();
8385
}
8486
});
@@ -109,7 +111,7 @@ export default class LogPolling {
109111
>,
110112
): void {
111113
let timestamp: number = 0;
112-
logsWatchQuery.subscribe(({ data, errors, error }) => {
114+
logsWatchQuery.subscribe(async({ data, errors, error }) => {
113115
ux.action.start('Loading deployment logs...');
114116

115117
if (error) {
@@ -152,6 +154,7 @@ export default class LogPolling {
152154
}
153155

154156
if (this.config.deploymentStatus.includes(this.deploymentStatus)) {
157+
await sleep(1_000);
155158
logsWatchQuery.stopPolling();
156159
this.$event.emit('deployment-logs', {
157160
message: 'DONE',

test/unit/adapters/file-upload.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { FileUpload, BaseClass } from '../../../src/adapters';
77
import { BaseCommand } from '../../../src/base-command';
88
import { isNull } from 'util';
99
import { log } from 'console';
10-
import { FileUploadMethod } from '../../../src/types/launch';
10+
import { DeploymentStatus, FileUploadMethod } from '../../../src/types/launch';
1111

1212
describe('File Upload', () => {
1313
let inquireStub, exitStub, prepareApiClientsStub, prepareConfigStub, getConfigStub;
@@ -216,6 +216,44 @@ describe('File Upload', () => {
216216
expect(showSuggestionStub.calledOnce).to.be.true;
217217
});
218218

219+
it('should exit with non zero status code if deployment fails', async () => {
220+
getEnvironmentStub.resolves(defaultEnvironment);
221+
let adapterConstructorOptions = {
222+
config: {
223+
isExistingProject: true,
224+
currentConfig: { uid: '123244', organizationUid: 'bltxxxxxxxx' },
225+
'redeploy-latest': true,
226+
currentDeploymentStatus: DeploymentStatus.FAILED,
227+
},
228+
};
229+
let exitStatusCode;
230+
231+
try {
232+
await new FileUpload(adapterConstructorOptions).run();
233+
} catch (err) {
234+
exitStatusCode = err.message;
235+
}
236+
237+
expect(getEnvironmentStub.calledOnce).to.be.true;
238+
expect(processExitStub.calledOnceWithExactly(1)).to.be.true;
239+
expect(exitStatusCode).to.equal('1');
240+
expect(initApolloClientStub.calledOnce).to.be.true;
241+
expect(createSignedUploadUrlStub.calledOnce).to.be.true;
242+
expect(archiveStub.calledOnce).to.be.true;
243+
expect(uploadFileStub.calledOnce).to.be.true;
244+
expect(uploadFileStub.args[0]).to.deep.equal([zipName, zipPath, signedUploadUrlData]);
245+
expect(createNewDeploymentStub.calledOnce).to.be.true;
246+
expect(createNewDeploymentStub.args[0]).to.deep.equal([
247+
true,
248+
defaultEnvironment.uid,
249+
signedUploadUrlData.uploadUid,
250+
]);
251+
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
252+
expect(showLogsStub.calledOnce).to.be.true;
253+
expect(showDeploymentUrlStub.calledOnce).to.be.false;
254+
expect(showSuggestionStub.calledOnce).to.be.false;
255+
});
256+
219257
it('should exit with an error message when both --redeploy-last-upload and --redeploy-latest flags are passed', async () => {
220258
getEnvironmentStub.resolves(defaultEnvironment);
221259

test/unit/adapters/github.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { githubAdapterMockData } from '../mock/index';
66
import { GitHub, BaseClass } from '../../../src/adapters';
77
import { BaseCommand } from '../../../src/base-command';
88
import fs from 'fs';
9+
import { DeploymentStatus } from '../../../src/types';
910

1011
describe('GitHub', () => {
1112
let inquireStub, prepareApiClientsStub, prepareConfigStub, getConfigStub;
@@ -155,6 +156,38 @@ describe('GitHub', () => {
155156
expect(showSuggestionStub.calledOnce).to.be.true;
156157
});
157158

159+
it('should exit with non zero status code if deployment fails', async () => {
160+
getEnvironmentStub.resolves(defaultEnvironment);
161+
let adapterConstructorOptions = {
162+
config: {
163+
isExistingProject: true,
164+
'redeploy-latest': true,
165+
currentDeploymentStatus: DeploymentStatus.FAILED
166+
},
167+
};
168+
let exitStatusCode;
169+
170+
try {
171+
await new GitHub(adapterConstructorOptions).run();
172+
} catch (err) {
173+
exitStatusCode = err.message;
174+
}
175+
176+
expect(getEnvironmentStub.calledOnce).to.be.true
177+
expect(processExitStub.calledOnceWithExactly(1)).to.be.true;
178+
expect(exitStatusCode).to.equal('1');
179+
expect(initApolloClientStub.calledOnce).to.be.true;
180+
expect(createNewDeploymentStub.calledOnce).to.be.true;
181+
expect(createNewDeploymentStub.args[0]).to.deep.equal([
182+
false,
183+
defaultEnvironment.uid
184+
]);
185+
expect(prepareLaunchConfigStub.calledOnce).to.be.true;
186+
expect(showLogsStub.calledOnce).to.be.true;
187+
expect(showDeploymentUrlStub.calledOnce).to.be.false;
188+
expect(showSuggestionStub.calledOnce).to.be.false;
189+
});
190+
158191
it('should abort github flow for existing project when flag redeploy-last-upload is passed', async () => {
159192
getEnvironmentStub.resolves(defaultEnvironment)
160193

0 commit comments

Comments
 (0)