Created
February 1, 2019 12:24
-
-
Save kooba/e279cd716b2d3836dfd8c4ce86abf621 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const kubernetes = require('@kubernetes/client-node'); | |
const yaml = require('js-yaml'); | |
const ulid = require('ulid'); | |
const crypto = require('crypto'); | |
const k8sClient = kubernetes.Config.defaultClient(); | |
const BRIGADE_NAMESPACE = 'brigade'; | |
const GITHUB_API_URL = 'https://api.github.com/repos'; | |
const deployProjects = async (environmentName) => { | |
const environmentConfigMap = await k8sClient.readNamespacedConfigMap( | |
`environment-config-${environmentName}`, BRIGADE_NAMESPACE, | |
); | |
const environmentsConfig = yaml.safeLoad( | |
environmentConfigMap.body.data.environment, | |
); | |
for (const key of Object.keys(environmentsConfig)) { | |
const projectConfig = environmentsConfig[key]; | |
const url = `${GITHUB_API_URL}/${projectConfig.org}/${projectConfig.repo}`; | |
const tagUrl = `${url}/git/refs/tags/${projectConfig.tag}`; | |
const response = await fetch( | |
tagUrl, { | |
method: 'GET', | |
headers: { | |
'Content-Type': 'application/json', | |
Authorization: `token ${process.env.BRIGADE_REPO_AUTH_TOKEN}`, | |
}, | |
}, | |
); | |
if (!response.ok && response.status === 404) { | |
console.log( | |
`'${projectConfig.tag}' tag not found in ` | |
+ `${projectConfig.org}/${projectConfig.repo} repository`, | |
); | |
} else { | |
console.log( | |
`Triggering deployment of '${projectConfig.repo}' for tag '${projectConfig.tag}'`, | |
); | |
const commit = await response.json(); | |
const gitSha = commit.object.sha; | |
const projectSha = crypto.createHash('sha256').update( | |
`${projectConfig.org}/${projectConfig.repo}`, | |
).digest('hex').substring(0, 54); | |
const projectId = `brigade-${projectSha}`; | |
const buildId = ulid().toLowerCase(); | |
const buildName = `environment-worker-${buildId}`; | |
const deployEventSecret = new kubernetes.V1Secret(); | |
deployEventSecret.metadata = new kubernetes.V1ObjectMeta(); | |
deployEventSecret.metadata.name = buildName; | |
deployEventSecret.metadata.labels = { | |
build: buildId, | |
component: 'build', | |
heritage: 'brigade', | |
project: projectId, | |
}; | |
deployEventSecret.type = 'brigade.sh/build'; | |
deployEventSecret.stringData = { | |
build_id: buildName, | |
build_name: buildName, | |
commit_id: gitSha, | |
commit_ref: projectConfig.tag, | |
event_provider: 'brigade-cli', | |
event_type: 'exec', | |
log_level: 'log', | |
payload: `{"name": "${environmentName}"}`, | |
project_id: projectId, | |
script: '', | |
}; | |
await k8sClient.createNamespacedSecret(BRIGADE_NAMESPACE, deployEventSecret); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment