Skip to content

Commit d3dfd46

Browse files
authored
feat: Don't fail when ephemeral environment already exists (#9)
* Action succeeds if ephemeral environment already exists * PR fixes * Remove useless functionality that tried to retrieve the existing environment * WIP * Got working, added tests * Clean up versions * Update test name * Fixed workflow * Add more logging * Add more test, fixed nits * Update to latest version of apiclient * Fix test
1 parent 99db60a commit d3dfd46

File tree

7 files changed

+397
-77
lines changed

7 files changed

+397
-77
lines changed

.github/workflows/test-create-environment.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ jobs:
3333
service_account_id: ${{ inputs.service_account_id || vars.TEST_INSTANCE_SERVICE_ACCOUNT_ID }}
3434

3535
- name: Create Ephemeral Environment
36+
uses: ./
37+
with:
38+
name: ${{ inputs.name }}
39+
project: ${{ inputs.project || vars.TEST_PROJECT_NAME}}
40+
space: ${{ inputs.space || vars.TEST_SPACE_NAME}}
41+
42+
- name: Recreate Ephemeral Environment
43+
# Checks that we can handle the case when an environment already exists
3644
uses: ./
3745
with:
3846
name: ${{ inputs.name }}

dist/index.js

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4566,6 +4566,23 @@ var EnvironmentRepository = /** @class */ (function (_super) {
45664566
});
45674567
});
45684568
};
4569+
EnvironmentRepository.prototype.getEphemeralEnvironmentProjectStatus = function (environmentId, projectId) {
4570+
return __awaiter(this, void 0, void 0, function () {
4571+
var response;
4572+
return __generator(this, function (_a) {
4573+
switch (_a.label) {
4574+
case 0: return [4 /*yield*/, this.client.request("".concat(__1.spaceScopedRoutePrefix, "/projects/{projectId}/environments/ephemeral/{id}/status"), {
4575+
spaceName: this.spaceName,
4576+
projectId: projectId,
4577+
id: environmentId,
4578+
})];
4579+
case 1:
4580+
response = _a.sent();
4581+
return [2 /*return*/, response];
4582+
}
4583+
});
4584+
});
4585+
};
45694586
EnvironmentRepository.prototype.deprovisionEphemeralEnvironmentForProject = function (environmentId, projectId) {
45704587
return __awaiter(this, void 0, void 0, function () {
45714588
var response;
@@ -4612,7 +4629,7 @@ var EnvironmentRepository = /** @class */ (function (_super) {
46124629
})];
46134630
case 1:
46144631
listResponse = _a.sent();
4615-
matchingEnvironments = listResponse.Items.filter(function (env) { return env.Name === environmentName; });
4632+
matchingEnvironments = listResponse.Items.filter(function (env) { return env.Name.toLowerCase() === environmentName.toLowerCase(); });
46164633
if (matchingEnvironments.length > 1) {
46174634
throw (0, console_1.error)("Multiple environments found with the name '".concat(environmentName));
46184635
}
@@ -67444,13 +67461,14 @@ exports.ActionContextImplementation = ActionContextImplementation;
6744467461
Object.defineProperty(exports, "__esModule", ({ value: true }));
6744567462
exports.createEphemeralEnvironmentFromInputs = createEphemeralEnvironmentFromInputs;
6744667463
exports.GetProjectByName = GetProjectByName;
67464+
exports.GetExistingEnvironmentIdByName = GetExistingEnvironmentIdByName;
67465+
exports.GetEnvironmentProjectStatus = GetEnvironmentProjectStatus;
6744767466
const api_client_1 = __nccwpck_require__(1212);
6744867467
async function createEphemeralEnvironmentFromInputs(client, parameters, context) {
6744967468
client.info('🐙 Creating an ephemeral environment in Octopus Deploy...');
6745067469
const project = await GetProjectByName(client, parameters.project, parameters.space, context);
6745167470
const environmentRepository = new api_client_1.EnvironmentRepository(client, parameters.space);
6745267471
const response = await environmentRepository.createEphemeralEnvironment(parameters.name, project.Id);
67453-
client.info(`🎉 Ephemeral environment '${parameters.name}' created successfully!`);
6745467472
return response.Id;
6745567473
}
6745667474
async function GetProjectByName(client, projectName, spaceName, context) {
@@ -67462,16 +67480,29 @@ async function GetProjectByName(client, projectName, spaceName, context) {
6746267480
project = projects.find(p => p.Name === projectName);
6746367481
}
6746467482
catch (error) {
67465-
context.error?.(`Error getting project by name: ${error}`);
67483+
context.error(`Error getting project by name: ${error}`);
6746667484
}
6746767485
if (project !== null && project !== undefined) {
6746867486
return project;
6746967487
}
6747067488
else {
67471-
context.error?.(`Project, "${projectName}" not found`);
67489+
context.error(`Project, "${projectName}" not found`);
6747267490
throw new Error(`Project, "${projectName}" not found`);
6747367491
}
6747467492
}
67493+
async function GetExistingEnvironmentIdByName(client, environmentName, spaceName) {
67494+
const environmentRepository = new api_client_1.EnvironmentRepository(client, spaceName);
67495+
const existingEnvironment = await environmentRepository.getEnvironmentByName(environmentName);
67496+
if (existingEnvironment) {
67497+
return existingEnvironment.Id;
67498+
}
67499+
return null;
67500+
}
67501+
async function GetEnvironmentProjectStatus(client, environmentId, projectId, spaceName) {
67502+
const environmentRepository = new api_client_1.EnvironmentRepository(client, spaceName);
67503+
const projectStatus = await environmentRepository.getEphemeralEnvironmentProjectStatus(environmentId, projectId);
67504+
return projectStatus.Status;
67505+
}
6747567506
//# sourceMappingURL=api-wrapper.js.map
6747667507

6747767508
/***/ }),
@@ -67493,11 +67524,36 @@ async function createEnvironment(context) {
6749367524
instanceURL: parameters.server,
6749467525
apiKey: parameters.apiKey,
6749567526
accessToken: parameters.accessToken,
67496-
logging: context
67527+
logging: context,
6749767528
};
6749867529
const client = await api_client_1.Client.create(config);
67499-
await (0, api_wrapper_1.createEphemeralEnvironmentFromInputs)(client, parameters, context);
67500-
context.writeStepSummary(`🐙 Octopus Deploy created an ephemeral environment **${parameters.name}** for project **${parameters.project}**.`);
67530+
context.info(`🐙 Creating ephemeral environment with name ${parameters.name}...`);
67531+
const environmentId = await (0, api_wrapper_1.GetExistingEnvironmentIdByName)(client, parameters.name, parameters.space);
67532+
if (!environmentId) {
67533+
context.info(`🆕 Environment not found - creating new environment`);
67534+
await (0, api_wrapper_1.createEphemeralEnvironmentFromInputs)(client, parameters, context);
67535+
client.info(`🎉 Ephemeral environment '${parameters.name}' created successfully!`);
67536+
context.writeStepSummary(`🐙 Octopus Deploy created an ephemeral environment **${parameters.name}** for project **${parameters.project}**.`);
67537+
return;
67538+
}
67539+
else {
67540+
context.info(`✅ Environment found - checking project connection`);
67541+
const project = await (0, api_wrapper_1.GetProjectByName)(client, parameters.project, parameters.space, context);
67542+
const environmentProjectStatus = await (0, api_wrapper_1.GetEnvironmentProjectStatus)(client, environmentId, project.Id, parameters.space);
67543+
context.info(`🔗 Environment project status: ${environmentProjectStatus}`);
67544+
if (environmentProjectStatus == 'NotConnected') {
67545+
context.info(`🔌 Connecting existing ephemeral environment ${parameters.name} to project ${parameters.project}.`);
67546+
await (0, api_wrapper_1.createEphemeralEnvironmentFromInputs)(client, parameters, context);
67547+
context.info(`🔗 Connected existing environment ${parameters.name} to project ${parameters.project}`);
67548+
context.writeStepSummary(`🐙 Octopus Deploy connected ephemeral environment **${parameters.name}** to project **${parameters.project}**.`);
67549+
return;
67550+
}
67551+
else {
67552+
context.info(`♻️ Ephemeral environment ${parameters.name} already exists and is connected to project ${parameters.project}. Reusing existing environment.`);
67553+
context.writeStepSummary(`🐙 Octopus Deploy reused the existing ephemeral environment **${parameters.name}** for project **${parameters.project}**.`);
67554+
return;
67555+
}
67556+
}
6750167557
}
6750267558
//# sourceMappingURL=createEnvironment.js.map
6750367559

package-lock.json

Lines changed: 4 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@
3737
"dependencies": {
3838
"@actions/core": "1.11.1",
3939
"@actions/github": "6.0.1",
40-
"@octopusdeploy/api-client": "3.9.1"
40+
"@octopusdeploy/api-client": "3.10.1"
4141
}
4242
}

0 commit comments

Comments
 (0)