Skip to content

Commit

Permalink
feat: add release github action
Browse files Browse the repository at this point in the history
  • Loading branch information
OS-martacarlos committed May 20, 2024
1 parent 58f68cc commit d131f66
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 60 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/o11_deploy.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: (O11) Deploy Plugin

on:
pull_request:
workflow_dispatch:
inputs:
to:
Expand Down Expand Up @@ -39,5 +38,5 @@ jobs:
run: npm install

- name: Deploying from DEV to TST
run: npm run deploy --plugin=BarcodeAutomation --from=Development --to=Testing --lifetime=${{ secrets.LIFETIME }} --authentication="${{ secrets.AUTOMATION_TOKEN }}"
run: npm run deploy --plugin=BarcodeAutomation --from=Development --to=Testing --lifetime=${{ secrets.LIFETIME }} --authentication=${{ secrets.AUTOMATION_TOKEN }}

27 changes: 27 additions & 0 deletions .github/workflows/o11_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: (O11) Release Plugin

on:
pull_request:

jobs:
deploy:
name: Release plugin via LifeTime
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 18

- name: Install dependencies
run: npm install

- name: Deploying from DEV to TST
run: npm run deploy --plugin=BarcodeAutomation --from=Development --to=Testing --lifetime=${{ secrets.LIFETIME }} --authentication=${{ secrets.AUTOMATION_TOKEN }}

- name: Deploying from TST to PROD
run: npm run deploy --plugin=BarcodeAutomation --from=Testing --to=Production --lifetime=${{ secrets.LIFETIME }} --authentication=${{ secrets.AUTOMATION_TOKEN }}

118 changes: 60 additions & 58 deletions scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,3 @@

if(process.env.npm_config_from == null || process.env.npm_config_to == null) {
throw new Error("Missing repositoryURL, branch, environment arguments");
}

if(process.env.npm_config_authentication == null) {
throw new Error("Missing authentication argument");
}

let pluginSpaceName = process.env.npm_config_plugin;
let fromEnvironment = process.env.npm_config_from;
let toEnvironment = process.env.npm_config_to;
let basicAuthentication = process.env.npm_config_authentication;
let baseURL = process.env.npm_config_lifetime;

baseURL = `https://${baseURL}/lifetimeapi/rest/v2`;

startDeploy(baseURL, fromEnvironment, toEnvironment, pluginSpaceName, basicAuthentication);

async function startDeploy(baseURL, fromEnvironment, toEnvironment, pluginSpaceName, auth){
let fromKey = await getEnvironmentKey(baseURL, fromEnvironment, auth);
let toKey = await getEnvironmentKey(baseURL, toEnvironment, auth);

let pluginKey = await getAppKey(baseURL, pluginSpaceName, fromKey, auth)

let deploymentKey = createDeploymentPlan(baseURL, fromKey, toKey, pluginKey, auth);

//TODO: check for conflicts + slack message for approval if conflicts were found
await startDeployment(baseURL, deploymentKey, auth);

while(!isFinished(baseURL, deploymentKey, basicAuthentication)){
//we wait until it's finished
sleep(5000);
}

}

function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}

async function getEnvironmentKey(base, env, auth){
let url = `${base}/environments`;

Expand All @@ -56,12 +11,12 @@ async function getEnvironmentKey(base, env, auth){

if(response.ok && response.status == 200){
let list = await response.json();
return list.filter((detail) => detail.EnvironmentType == env)[0]
return (list.filter((detail) => detail.Name == env)[0]).Key
}
}

async function getAppKey(base, pluginSpaceName, inEnv, auth){
let url = `${base}/applicationss?IncludeEnvStatus=true`;
let url = `${base}/applications?IncludeEnvStatus=true`;

let response = await fetch(url, {
method: 'GET',
Expand All @@ -70,11 +25,12 @@ async function getAppKey(base, pluginSpaceName, inEnv, auth){
Authorization: auth
}
})

if(response.ok && response.status == 200){
let list = await response.json();
let app = list.filter((a) => a.name == pluginSpaceName);
return app.AppStatusInEnvs.filter((status) => status.EnvironmentKey == inEnv)[0]

let app = list.filter((a) => a.Name == pluginSpaceName)[0];
return (app.AppStatusInEnvs.filter((status) => status.EnvironmentKey == inEnv)[0]).BaseApplicationVersionKey
}
}

Expand All @@ -90,6 +46,7 @@ async function createDeploymentPlan(base, fromEnv, toEnv, pluginKey, auth) {
}
]
};
console.log(body)

const response = await fetch(url, {
method: "POST",
Expand All @@ -99,12 +56,12 @@ async function createDeploymentPlan(base, fromEnv, toEnv, pluginKey, auth) {
},
body: JSON.stringify(body)
})
console.log(response)

if(response.ok && response.status == 200){
let res = await response.json()
console.log("Deployment Response:" + res);
return response.json();

if(response.ok && response.status == 201){
let key = await response.text()
console.log("Deployment Response:" + key);
return key;
}
}

Expand All @@ -118,9 +75,8 @@ async function startDeployment(base, deployKey, auth){
Authorization: auth
}
})
console.log(response)

if(response.ok && response.status == 202){
if(response.ok && response.status == 202){
console.log("Deployment Started Successfully!");
}
}
Expand All @@ -143,7 +99,6 @@ async function isFinished(base, deployKey, auth) {
return false
}
if(status.DeploymentStatus == 'aborted'){

throw Error ("!! Deployment aborted !!");
}
if(status.DeploymentStatus == 'finished_with_errors'){
Expand All @@ -162,3 +117,50 @@ async function isFinished(base, deployKey, auth) {
}
throw Error ("!! Something went wrong with the request: " + await res.json());
}


if(process.env.npm_config_from == null || process.env.npm_config_to == null) {
throw new Error("Missing repositoryURL, branch, environment arguments");
}

if(process.env.npm_config_authentication == null) {
throw new Error("Missing authentication argument");
}

let pluginSpaceName = process.env.npm_config_plugin;
let fromEnvironment = process.env.npm_config_from;
let toEnvironment = process.env.npm_config_to;
let basicAuthentication = process.env.npm_config_authentication;
let baseURL = process.env.npm_config_lifetime;

baseURL = `https://${baseURL}/lifetimeapi/rest/v2`;

startDeploy(baseURL, fromEnvironment, toEnvironment, pluginSpaceName, basicAuthentication);

async function startDeploy(baseURL, fromEnvironment, toEnvironment, pluginSpaceName, auth){
let fromKey = await getEnvironmentKey(baseURL, fromEnvironment, auth);
console.log("from env with key: " + fromKey)
let toKey = await getEnvironmentKey(baseURL, toEnvironment, auth);
console.log("to env with key: " + toKey)
let pluginKey = await getAppKey(baseURL, pluginSpaceName, fromKey, auth)

let deploymentKey = await createDeploymentPlan(baseURL, fromKey, toKey, pluginKey, auth);
console.log("deployment key: " + deploymentKey)

//TODO: check for conflicts + slack message for approval if conflicts were found
await startDeployment(baseURL, deploymentKey, auth);

let intervalId = setInterval(async () => {
let finished = await isFinished(baseURL, deploymentKey, basicAuthentication);
if(!finished) {
console.log("Will check again in a while...");
} else {
clearInterval(intervalId);
}
}, 10000)


}



0 comments on commit d131f66

Please sign in to comment.