kit-image-deployer
Advanced tools
Comparing version 3.1.4 to 3.2.0
{ | ||
"name": "kit-image-deployer", | ||
"version": "3.1.4", | ||
"version": "3.2.0", | ||
"description": "A service that can be used to update given yaml files within a git repository with a new docker image path.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -109,2 +109,3 @@ <p align="center"> | ||
| `IMAGE` | The full path and tag to the docker image (must provide this or `CI_COMMIT_ID`) | no | *empty* | | ||
| `RETRIES` | Specify the number of times you want to automatically retry the commit if it fails | yes | `10` | | ||
@@ -111,0 +112,0 @@ ## Contributing |
@@ -12,2 +12,3 @@ "use strict"; | ||
this.options = _.merge({ | ||
retries: 10, | ||
docker: { | ||
@@ -43,94 +44,136 @@ registry: undefined, | ||
deployImage(image, branch, committer, message, save) { | ||
var self = this; | ||
return new Promise(function(resolve, reject) { | ||
var imageFilePath, property; | ||
var configRequest = { | ||
user: self.options.github.user, | ||
repo: self.options.github.repo, | ||
path: "kit.yaml" | ||
}; | ||
getImageFile(config, branch) { | ||
const self = this; | ||
const imageFilePath = path.join(config.images.path.replace(/^\//, ""), self.options.docker.repo, branch + ".yaml"); | ||
const property = config.images.property; | ||
self.github.getContent(configRequest) | ||
.then(function(configResponse) { | ||
var rawConfig = new Buffer(configResponse.content, configResponse.encoding).toString("ascii"); | ||
var config = yaml.safeLoad(rawConfig); | ||
imageFilePath = path.join(config.images.path.replace(/^\//, ""), self.options.docker.repo, branch + ".yaml"); | ||
property = config.images.property; | ||
const imageRequest = { | ||
user: self.options.github.user, | ||
repo: self.options.github.repo, | ||
path: imageFilePath | ||
}; | ||
var imageRequest = { | ||
user: self.options.github.user, | ||
repo: self.options.github.repo, | ||
path: imageFilePath | ||
}; | ||
return self.github.getContent(imageRequest) | ||
.then(function(imageResponse) { | ||
// get | ||
var rawFile = new Buffer(imageResponse.content, imageResponse.encoding).toString("ascii"); | ||
var file = yaml.safeLoad(rawFile); | ||
return self.github | ||
.getContent(imageRequest) | ||
.then((imageResponse) => { | ||
// get | ||
const rawFile = new Buffer(imageResponse.content, imageResponse.encoding).toString("ascii"); | ||
const file = yaml.safeLoad(rawFile); | ||
// update specified property in file | ||
if (!_.isObject(file)) { | ||
throw new Error("Only support updating yaml type files"); | ||
} | ||
if (!property) { | ||
throw new Error("Require configuration missing: 'property'"); | ||
} | ||
// update specified property in file | ||
if (!_.isObject(file)) { | ||
throw new Error("Only support updating yaml type files"); | ||
} | ||
if (!property) { | ||
throw new Error("Require configuration missing: 'property'"); | ||
} | ||
return { | ||
path: imageFilePath, | ||
property: property, | ||
file: file, | ||
sha: imageResponse.sha | ||
} | ||
}) | ||
} | ||
// only save if the image has changed | ||
if (file[property] != image) { | ||
var commitMsg = "committed " + property + ": '" + image + "' to " + imageFilePath; | ||
attemptCommit(config, image, branch, committer, message, save) { | ||
return new Promise((resolve, reject) => { | ||
const self = this; | ||
var tries = 0; | ||
function attempt(lastError) { | ||
tries++; | ||
if (tries > self.options.retries) { | ||
return reject(lastError); | ||
} | ||
// only continue saving if commit is enabled | ||
if (!save) { | ||
return "Commit disabled, but would have " + commitMsg; | ||
} | ||
self | ||
.getImageFile(config, branch) | ||
.then((imageFile) => { | ||
// only save if the image has changed | ||
if (imageFile.file[imageFile.property] !== image) { | ||
const commitMsg = "committed " + imageFile.property + ": '" + image + "' to " + imageFile.path; | ||
file[property] = image; | ||
var updatedFile = new Buffer(yaml.safeDump(file)).toString("base64"); | ||
imageFile.file[imageFile.property] = image; | ||
const updatedFile = new Buffer(yaml.safeDump(imageFile.file)).toString("base64"); | ||
return self.github.updateFile({ | ||
// only continue saving if commit is enabled | ||
if (!save) { | ||
return "Commit disabled, but would have " + commitMsg; | ||
} | ||
self.github | ||
.updateFile({ | ||
user: self.options.github.user, | ||
repo: self.options.github.repo, | ||
path: imageFilePath, | ||
path: imageFile.path, | ||
message: message, | ||
content: updatedFile, | ||
committer: committer, | ||
sha: imageResponse.sha | ||
}).then(function() { | ||
return "Successfully " + commitMsg; | ||
sha: imageFile.sha | ||
}) | ||
.then(function() { | ||
resolve("Successfully " + commitMsg); | ||
}) | ||
.catch((err) => { | ||
if (err && err.code && err.code === 404) { | ||
// create file if it does not exist yet | ||
var newFile = {}; | ||
newFile[property] = image; | ||
var createdFile = new Buffer(yaml.safeDump(newFile)).toString("base64"); | ||
var commitMsg = "committed " + imageFile.property + ": '" + image + "' to " + imageFile.path; | ||
// only continue saving if commit is enabled | ||
if (!save) { | ||
return "Commit disabled, but would have " + commitMsg; | ||
} | ||
return self.github.createFile({ | ||
user: self.options.github.user, | ||
repo: self.options.github.repo, | ||
path: imageFile.path, | ||
message: message, | ||
content: createdFile, | ||
committer: committer | ||
}) | ||
.then(() => { | ||
return "Successfully " + commitMsg; | ||
}) | ||
.catch(() => { | ||
// try again | ||
attempt(err); | ||
}); | ||
} else { | ||
// try again | ||
attempt(err); | ||
} | ||
}); | ||
} else { | ||
return "No changes found"; | ||
} | ||
}) | ||
.catch(function(err) { | ||
if (err && err.code === 404) { | ||
// create file if it does not exist yet | ||
var newFile = {}; | ||
newFile[property] = image; | ||
var createdFile = new Buffer(yaml.safeDump(newFile)).toString("base64"); | ||
var commitMsg = "committed " + property + ": '" + image + "' to " + imageFilePath; | ||
} else { | ||
resolve("No changes found for " + imageFile.property + ": '" + image + "' to " + imageFile.path); | ||
} | ||
}) | ||
.catch((err) => { | ||
// try again | ||
attempt(err); | ||
}); | ||
} | ||
// only continue saving if commit is enabled | ||
if (!save) { | ||
return "Commit disabled, but would have " + commitMsg; | ||
} | ||
// start trying! | ||
attempt(); | ||
}); | ||
} | ||
return self.github.createFile({ | ||
user: self.options.github.user, | ||
repo: self.options.github.repo, | ||
path: imageFilePath, | ||
message: message, | ||
content: createdFile, | ||
committer: committer | ||
}) | ||
.then(function() { | ||
return "Successfully " + commitMsg; | ||
}); | ||
} else { | ||
// if it's not a `404` error, then throw again | ||
throw err; | ||
} | ||
}); | ||
deployImage(image, branch, committer, message, save) { | ||
const self = this; | ||
return new Promise(function(resolve, reject) { | ||
const configRequest = { | ||
user: self.options.github.user, | ||
repo: self.options.github.repo, | ||
path: "kit.yaml" | ||
}; | ||
self.github.getContent(configRequest) | ||
.then(function(configResponse) { | ||
const rawConfig = new Buffer(configResponse.content, configResponse.encoding).toString("ascii"); | ||
const config = yaml.safeLoad(rawConfig); | ||
return self.attemptCommit(config, image, branch, committer, message, save); | ||
}) | ||
@@ -137,0 +180,0 @@ .then(resolve) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14769
171
118