diff --git a/.github/workflows/o11_deploy.yml b/.github/workflows/o11_deploy.yml index 6ee919e..715d249 100644 --- a/.github/workflows/o11_deploy.yml +++ b/.github/workflows/o11_deploy.yml @@ -1,7 +1,6 @@ name: (O11) Deploy Plugin on: - pull_request: workflow_dispatch: inputs: to: @@ -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 }} diff --git a/.github/workflows/o11_release.yml b/.github/workflows/o11_release.yml new file mode 100644 index 0000000..3cccef6 --- /dev/null +++ b/.github/workflows/o11_release.yml @@ -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 }} + diff --git a/scripts/deploy.js b/scripts/deploy.js index 8d46d69..68d678e 100644 --- a/scripts/deploy.js +++ b/scripts/deploy.js @@ -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`; @@ -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', @@ -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 } } @@ -90,6 +46,7 @@ async function createDeploymentPlan(base, fromEnv, toEnv, pluginKey, auth) { } ] }; + console.log(body) const response = await fetch(url, { method: "POST", @@ -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; } } @@ -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!"); } } @@ -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'){ @@ -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) + + +} + + +