|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +function fetchBranchConfig(branchName) { |
| 4 | + const fs = require('fs'); |
| 5 | + const yaml = require('js-yaml'); |
| 6 | + |
| 7 | + let fileContents = fs.readFileSync('./appspec.yml', 'utf8'); |
| 8 | + let data = yaml.safeLoad(fileContents); |
| 9 | + |
| 10 | + for (var prop in data.branch_config) { |
| 11 | + var regex = new RegExp('^' + prop + '$', 'i'); |
| 12 | + if (branchName.match(regex)) { |
| 13 | + if (data.branch_config[prop] == null) { |
| 14 | + console.log(`🤷🏻♂️ Found an empty appspec.yml -> branch_config for '${branchName}' – skipping deployment`); |
| 15 | + process.exit(); |
| 16 | + } |
| 17 | + console.log(`💡 Using appspec.yml -> branch_config '${prop}' for branch '${branchName}'`); |
| 18 | + return data.branch_config[prop]; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + console.log(`❓ Found no matching appspec.yml -> branch_config for '${branchName}' – skipping deployment`); |
| 23 | + process.exit(); |
| 24 | +} |
| 25 | + |
| 26 | +exports.createDeployment = async function(applicationName, fullRepositoryName, branchName, commitId, core) { |
| 27 | + const branchConfig = fetchBranchConfig(branchName); |
| 28 | + const safeBranchName = branchName.replace(/[^a-z0-9-/]+/gi, '-').replace(/\/+/, '--'); |
| 29 | + const deploymentGroupName = branchConfig.deploymentGroupName ? branchConfig.deploymentGroupName.replace('$BRANCH', safeBranchName) : safeBranchName; |
| 30 | + const deploymentGroupConfig = branchConfig.deploymentGroupConfig; |
| 31 | + const deploymentConfig = branchConfig.deploymentConfig; |
| 32 | + |
| 33 | + console.log(`🎳 Using deployment group '${deploymentGroupName}'`); |
| 34 | + |
| 35 | + const client = require('aws-sdk/clients/codedeploy'); |
| 36 | + const codeDeploy = new client(); |
| 37 | + |
| 38 | + try { |
| 39 | + await codeDeploy.updateDeploymentGroup({ |
| 40 | + ...deploymentGroupConfig, |
| 41 | + ...{ |
| 42 | + applicationName: applicationName, |
| 43 | + currentDeploymentGroupName: deploymentGroupName |
| 44 | + } |
| 45 | + }).promise(); |
| 46 | + console.log(`⚙️ Updated deployment group '${deploymentGroupName}'`); |
| 47 | + core.setOutput('deploymentGroupCreated', false); |
| 48 | + } catch (e) { |
| 49 | + if (e.code == 'DeploymentGroupDoesNotExistException') { |
| 50 | + await codeDeploy.createDeploymentGroup({ |
| 51 | + ...deploymentGroupConfig, |
| 52 | + ...{ |
| 53 | + applicationName: applicationName, |
| 54 | + deploymentGroupName: deploymentGroupName, |
| 55 | + } |
| 56 | + }).promise(); |
| 57 | + console.log(`🎯 Created deployment group '${deploymentGroupName}'`); |
| 58 | + core.setOutput('deploymentGroupCreated', true); |
| 59 | + } else { |
| 60 | + core.setFailed(`🌩 Unhandled exception`); |
| 61 | + throw e; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + let tries = 0; |
| 66 | + while (true) { |
| 67 | + |
| 68 | + if (++tries > 5) { |
| 69 | + core.setFailed('🤥 Unable to create a new deployment (too much concurrency?)'); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + try { |
| 74 | + var {deploymentId: deploymentId} = await codeDeploy.createDeployment({ |
| 75 | + ...deploymentConfig, |
| 76 | + ...{ |
| 77 | + applicationName: applicationName, |
| 78 | + deploymentGroupName: deploymentGroupName, |
| 79 | + revision: { |
| 80 | + revisionType: 'GitHub', |
| 81 | + gitHubLocation: { |
| 82 | + commitId: commitId, |
| 83 | + repository: fullRepositoryName |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + }).promise(); |
| 88 | + console.log(`🚚️ Created deployment ${deploymentId} – https://console.aws.amazon.com/codesuite/codedeploy/deployments/${deploymentId}?region=${codeDeploy.config.region}`); |
| 89 | + core.setOutput('deploymentId', deploymentId); |
| 90 | + core.setOutput('deploymentGroupName', deploymentGroupName); |
| 91 | + break; |
| 92 | + } catch (e) { |
| 93 | + if (e.code == 'DeploymentLimitExceededException') { |
| 94 | + var [, otherDeployment] = e.message.toString().match(/is already deploying deployment \'(d-\w+)\'/); |
| 95 | + console.log(`😶 Waiting for another pending deployment ${otherDeployment}`); |
| 96 | + try { |
| 97 | + await codeDeploy.waitFor('deploymentSuccessful', {deploymentId: otherDeployment}).promise(); |
| 98 | + console.log(`🙂 The pending deployment ${otherDeployment} sucessfully finished.`); |
| 99 | + } catch (e) { |
| 100 | + console.log(`🤔 The other pending deployment ${otherDeployment} seems to have failed.`); |
| 101 | + } |
| 102 | + continue; |
| 103 | + } else { |
| 104 | + core.setFailed(`🌩 Unhandled exception`); |
| 105 | + throw e; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + console.log(`⏲ Waiting for deployment ${deploymentId} to finish`); |
| 111 | + |
| 112 | + try { |
| 113 | + await codeDeploy.waitFor('deploymentSuccessful', {deploymentId: deploymentId}).promise(); |
| 114 | + console.log('🥳 Deployment successful'); |
| 115 | + } catch (e) { |
| 116 | + core.setFailed(`😱 The deployment ${deploymentId} seems to have failed.`); |
| 117 | + throw e; |
| 118 | + } |
| 119 | +} |
0 commit comments