Skip to content

Commit

Permalink
Adding support for error handling (#11752)
Browse files Browse the repository at this point in the history
* Adding support for error handling

* Addressing review comments

* addressing review comments

* Changes
  • Loading branch information
prebansa authored and leantk committed Dec 23, 2019
1 parent 79d9001 commit e768bba
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Tasks/ContainerBuildV0/src/buildcontainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ tl.setResourcePath(path.join(__dirname, '..', 'task.json'));
async function buildContainer() {
if (process.env["RUNNING_ON"] == "KUBERNETES") {
tl.debug("Building image using buildctl");
buildctl.buildctlBuildAndPush();
return buildctl.buildctlBuildAndPush();
}
else {
tl.debug("Building image using docker");
docker.dockerBuildAndPush();
return docker.dockerBuildAndPush();
}
}

Expand Down
8 changes: 6 additions & 2 deletions Tasks/ContainerBuildV0/src/buildctl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,14 @@ export async function buildctlBuildAndPush() {
buildctlTool.arg('--exporter=image');
buildctlTool.arg(`--exporter-opt=name=${imageNameandTag}`);
buildctlTool.arg('--exporter-opt=push=true');
buildctlTool.exec();
buildctlTool.exec().then(() => {}).catch((error) => {
throw new Error(error.message);
});
}
else {
// only build the image
await buildctlTool.exec();
await buildctlTool.exec().then(() => {}).catch((error) => {
throw new Error(error.message);
});
}
}
8 changes: 6 additions & 2 deletions Tasks/ContainerBuildV0/src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ export async function dockerBuildAndPush() {

/* tslint:disable:no-var-requires */
let commandImplementation = require("./dockerbuild");
await commandImplementation.runBuild(connection)
await commandImplementation.runBuild(connection).then(() => {}).catch((error) => {
throw new Error(error.message);
});

if (tl.getInput("dockerRegistryServiceConnection")) {
commandImplementation = require("./dockerpush")
await commandImplementation.run(connection)
await commandImplementation.run(connection).then(() => {}).catch((error) => {
throw new Error(error.message);
});
}
}
40 changes: 22 additions & 18 deletions Tasks/ContainerBuildV0/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,34 +100,38 @@ export async function getServiceDetails() {
};
var serviceResponse = kubectlTool.execSync(executionOption);

if (serviceResponse && serviceResponse.stdout) {
namespace = JSON.parse(serviceResponse.stdout).metadata.namespace ? JSON.parse(serviceResponse.stdout).metadata.namespace : namespace;
port = JSON.parse(serviceResponse.stdout).spec.ports[0].port ? JSON.parse(serviceResponse.stdout).spec.ports[0].port : port;
clusteruri = JSON.parse(serviceResponse.stdout).status.loadBalancer.ingress[0].ip ? JSON.parse(serviceResponse.stdout).status.loadBalancer.ingress[0].ip : clusteruri;
if (serviceResponse && serviceResponse.stderr) {
throw new Error(serviceResponse.stderr);
}
else if (serviceResponse && serviceResponse.stdout) {
var responseOutput = JSON.parse(serviceResponse.stdout);
namespace = responseOutput.metadata.namespace ? responseOutput.metadata.namespace : namespace;
port = responseOutput.spec.ports[0].port ? responseOutput.spec.ports[0].port : port;
clusteruri = responseOutput.status.loadBalancer.ingress[0].ip ? responseOutput.status.loadBalancer.ingress[0].ip : clusteruri;
}
}

export async function getBuildKitPod() {

await getServiceDetails();
await getServiceDetails();

let request = new webclient.WebRequest();
let headers = {
"key": tl.getVariable('Build.Repository.Name') + tl.getInput("Dockerfile", true)
};
let webRequestOptions: webclient.WebRequestOptions = { retriableErrorCodes: [], retriableStatusCodes: [], retryCount: 1, retryIntervalInSeconds: 5, retryRequestTimedout: true };
let request = new webclient.WebRequest();
let headers = {
"key": tl.getVariable('Build.Repository.Name') + tl.getInput("Dockerfile", true)
};
let webRequestOptions: webclient.WebRequestOptions = { retriableErrorCodes: [], retriableStatusCodes: [], retryCount: 1, retryIntervalInSeconds: 5, retryRequestTimedout: true };

request.uri = `http://${clusteruri}:${port}/buildPod`;
request.headers = headers
request.method = "GET";
request.uri = `http://${clusteruri}:${port}/buildPod`;
request.headers = headers
request.method = "GET";

var response = await webclient.sendRequest(request, webRequestOptions);
var podname = response.body.Message;
var response = await webclient.sendRequest(request, webRequestOptions);
var podname = response.body.Message;

tl.debug("Podname " + podname);
tl.debug("Podname " + podname);

// set the environment variable
process.env["BUILDKIT_HOST"] = "kube-pod://" + podname + "?namespace=" + namespace;
// set the environment variable
process.env["BUILDKIT_HOST"] = "kube-pod://" + podname + "?namespace=" + namespace;
}

function findBuildctl(rootFolder: string) {
Expand Down
6 changes: 3 additions & 3 deletions Tasks/ContainerBuildV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 160,
"Patch": 5
"Minor": 161,
"Patch": 0
},
"demands": [],
"satisfies": [
Expand Down Expand Up @@ -47,7 +47,7 @@
"name": "Dockerfile",
"type": "filePath",
"label": "Dockerfile",
"defaultValue": ".",
"defaultValue": "Dockerfile",
"required": true,
"helpMarkDown": "Path to Dockerfile."
},
Expand Down
6 changes: 3 additions & 3 deletions Tasks/ContainerBuildV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 160,
"Patch": 5
"Minor": 161,
"Patch": 0
},
"demands": [],
"satisfies": [
Expand Down Expand Up @@ -47,7 +47,7 @@
"name": "Dockerfile",
"type": "filePath",
"label": "ms-resource:loc.input.label.Dockerfile",
"defaultValue": ".",
"defaultValue": "Dockerfile",
"required": true,
"helpMarkDown": "ms-resource:loc.input.help.Dockerfile"
},
Expand Down

0 comments on commit e768bba

Please sign in to comment.