Skip to content

Commit

Permalink
Add new Artifactory Go task
Browse files Browse the repository at this point in the history
  • Loading branch information
RobiNino committed Oct 31, 2019
1 parent 747501e commit 0efdcc3
Show file tree
Hide file tree
Showing 23 changed files with 520 additions and 67 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ To run the tests, use the following commands:

### Skipping Tests
In order to skip tests, set the ADO_ARTIFACTORY_SKIP_TESTS environment variable with the tests you wish to skip, separated by commas.
The supported values are: **maven**, **npm**, **nuget**, **conan** and **docker**.
The supported values are: **maven**, **npm**, **go**, **nuget**, **conan** and **docker**.

For example, for setting the nuget and docker tests:
```
Expand Down
3 changes: 2 additions & 1 deletion artifactory-tasks-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"azure-pipelines-task-lib": "^2.7.1",
"azure-pipelines-tool-lib": "^0.11.0",
"fs-extra": "^7.0.0",
"typed-rest-client": "1.0.9"
"typed-rest-client": "1.0.9",
"js-yaml": "^3.13.1"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
60 changes: 59 additions & 1 deletion artifactory-tasks-utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const execSync = require('child_process').execSync;
const toolLib = require('azure-pipelines-tool-lib/tool');
const clientHandlers = require('typed-rest-client/Handlers');
const localTools = require('./tools');
const yaml = require('js-yaml');

const fileName = getCliExecutableName();
const toolName = "jfrog";
Expand All @@ -13,6 +14,7 @@ const jfrogFolderPath = encodePath(path.join(tl.getVariable("Agent.WorkFolder"),
const jfrogCliVersion = "1.30.1";
const customCliPath = encodePath(path.join(jfrogFolderPath, "current", fileName)); // Optional - Customized jfrog-cli path.
const jfrogCliBintrayDownloadUrl = 'https://api.bintray.com/content/jfrog/jfrog-cli-go/' + jfrogCliVersion + '/' + btPackage + '/' + fileName + "?bt_package=" + btPackage;
const buildToolsConfigVersion = 1;

let cliConfigCommand = "rt c";
let runTaskCbk = null;
Expand All @@ -36,7 +38,11 @@ module.exports = {
configureCliServer: configureCliServer,
deleteCliServers: deleteCliServers,
writeSpecContentToSpecPath: writeSpecContentToSpecPath,
stripTrailingSlash: stripTrailingSlash
stripTrailingSlash: stripTrailingSlash,
determineCliWorkDir: determineCliWorkDir,
createBuildToolConfigFile: createBuildToolConfigFile,
assembleBuildToolServerId: assembleBuildToolServerId,
appendBuildFlagsToCliCommand: appendBuildFlagsToCliCommand
};

// Url and AuthHandlers are optional. Using jfrogCliBintrayDownloadUrl by default.
Expand Down Expand Up @@ -418,3 +424,55 @@ function stripTrailingSlash(str) {
str.slice(0, -1) :
str;
}

/**
* Determines the required working directory for running the cli.
* Decision is based on the default path to run, and the provided path by the user.
*/
function determineCliWorkDir(defaultPath, providedPath) {
if (providedPath) {
if (path.isAbsolute(providedPath)) {
return providedPath;
}
return path.join(defaultPath, providedPath);
}
return defaultPath;
}

/**
* Creates a build tool config file at a desired absolute path.
* Resolver / Deployer object should consist serverID and repos according to the build tool used. For example, for maven:
* {snapshotRepo: 'jcenter', releaseRepo: 'jcenter', serverID: 'local'}
*/
function createBuildToolConfigFile(configPath, buildToolType, resolverObj, deployerObj) {
let yamlDocument = {};
yamlDocument.version = buildToolsConfigVersion;
yamlDocument.type = buildToolType;
if (resolverObj && Object.keys(resolverObj).length > 0) {
yamlDocument.resolver = resolverObj;
}
if (deployerObj && Object.keys(deployerObj).length > 0) {
yamlDocument.deployer = deployerObj;
}
let configInfo = yaml.safeDump(yamlDocument);
console.log(configInfo);
fs.outputFileSync(configPath, configInfo);
}

function assembleBuildToolServerId(buildToolType, serverType) {
let buildName = tl.getVariable('Build.DefinitionName');
let buildNumber = tl.getVariable('Build.BuildNumber');
return [buildName, buildNumber, buildToolType, serverType].join("-");
}

/**
* Appends build name and number to provided cli command if collectBuildInfo is selected.
* */
function appendBuildFlagsToCliCommand(cliCommand) {
if (tl.getBoolInput("collectBuildInfo")) {
// Construct the build-info collection flags.
let buildName = tl.getInput('buildName', true);
let buildNumber = tl.getInput('buildNumber', true);
return cliJoin(cliCommand, "--build-name=" + quote(buildName), "--build-number=" + quote(buildNumber));
}
}
3 changes: 2 additions & 1 deletion tasks/ArtifactoryConan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"uuid": "^3.3.2",
"fs-extra": "^7.0.0",
"azure-pipelines-task-lib": "^2.7.1",
"request-promise-lite": "^0.13.1"
"request-promise-lite": "^0.13.1",
"artifactory-tasks-utils": "file:../../artifactory-tasks-utils/artifactory-tasks-utils-1.0.0.tgz"
}
}
116 changes: 116 additions & 0 deletions tasks/ArtifactoryGo/goBuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
const tl = require('azure-pipelines-task-lib/task');
const fs = require('fs-extra');
const utils = require('artifactory-tasks-utils');
const path = require('path');

const cliGoNativeCommand = "rt go";
const cliGoPublishCommand = "rt gp";
let configuredServerId;

function RunTaskCbk(cliPath) {
let defaultWorkDir = tl.getVariable('System.DefaultWorkingDirectory');
if (!defaultWorkDir) {
tl.setResult(tl.TaskResult.Failed, "Failed getting default working directory.");
return;
}

// Determine working directory for the cli.
let inputWorkingDirectory = tl.getInput("workingDirectory", false);
let requiredWorkDir = utils.determineCliWorkDir(defaultWorkDir, inputWorkingDirectory);
if (!fs.existsSync(requiredWorkDir) || !fs.lstatSync(requiredWorkDir).isDirectory()) {
tl.setResult(tl.TaskResult.Failed, "Provided 'Working Directory': " + requiredWorkDir + " neither exists nor a directory.");
return;
}

// Determine go command and run cli.
let inputCommand = tl.getInput("command", true);
switch(inputCommand) {
// Fall-through if command is build / test / get
case 'build':
case 'test':
case 'get':
performGoNativeCommand(inputCommand, cliPath, requiredWorkDir);
break;
case 'custom':
let customCommand = tl.getInput("customCommand", true);
performGoNativeCommand(customCommand, cliPath, requiredWorkDir);
break;
case 'publish':
performGoPublishCommand(cliPath, requiredWorkDir);
break;
}
}

function performGoNativeCommand(goCommand, cliPath, requiredWorkDir) {
// Create config file and configure cli server
let configPath = path.join(requiredWorkDir, ".jfrog", "projects", "go.yaml");
try {
createGoConfigFile(configPath, cliPath, requiredWorkDir);
} catch (ex) {
tl.setResult(tl.TaskResult.Failed, ex);
return;
}

// Build go command with arguments and execute.
let cliCommand = utils.cliJoin(cliPath, cliGoNativeCommand, goCommand);
let goArguments = tl.getInput("goArguments", false);
if (goArguments) {
cliCommand = utils.cliJoin(cliCommand, goArguments);
}
executeGoCliCommand(cliCommand, cliPath, requiredWorkDir);
}

function createGoConfigFile(configPath, cliPath, requiredWorkDir) {
configureGoCliServer(cliPath, requiredWorkDir, "resolver");
let resolutionRepo = tl.getInput("resolutionRepo", true);
let resolverObj = {serverId: configuredServerId, repo: resolutionRepo};
utils.createBuildToolConfigFile(configPath, 'go', resolverObj, {});
}

function performGoPublishCommand(cliPath, requiredWorkDir) {
try {
configureGoCliServer(cliPath, requiredWorkDir, "deployer");
} catch (ex) {
tl.setResult(tl.TaskResult.Failed, ex);
return;
}

// Build go publish command and execute
let targetRepo = tl.getInput("targetRepo", true);
let version = tl.getInput("version", false);
let cliCommand = utils.cliJoin(cliPath, cliGoPublishCommand, targetRepo, version);
executeGoCliCommand(cliCommand, cliPath, requiredWorkDir);
}

function executeGoCliCommand(cliCommand, cliPath, requiredWorkDir) {
// Add build info collection.
cliCommand = utils.appendBuildFlagsToCliCommand(cliCommand);

// Execute cli.
try {
utils.executeCliCommand(cliCommand, requiredWorkDir, null);
} catch (ex) {
tl.setResult(tl.TaskResult.Failed, ex);
} finally {
cleanup(cliPath, requiredWorkDir);
}
// Ignored if the build's result was previously set to 'Failed'.
tl.setResult(tl.TaskResult.Succeeded, "Build Succeeded.")
}

function configureGoCliServer(cliPath, buildDir, serverType) {
configuredServerId = utils.assembleBuildToolServerId('go', serverType);
let artifactoryService = tl.getInput("artifactoryService", false);
utils.configureCliServer(artifactoryService, configuredServerId, cliPath, buildDir);
}

function cleanup(cliPath, workDir) {
// Delete servers.
try {
utils.deleteCliServers(cliPath, workDir, [configuredServerId]);
} catch (deleteServersException) {
tl.setResult(tl.TaskResult.Failed, deleteServersException);
}
}

utils.executeCliTask(RunTaskCbk);
Binary file added tasks/ArtifactoryGo/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions tasks/ArtifactoryGo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "artifactory-go",
"version": "1.0.0",
"author": "JFrog",
"private": true,
"license": "Apache-2.0",
"main": "goBuild.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"fs-extra": "^7.0.0",
"azure-pipelines-task-lib": "^2.7.1",
"artifactory-tasks-utils": "file:../../artifactory-tasks-utils/artifactory-tasks-utils-1.0.0.tgz"
}
}
Loading

0 comments on commit 0efdcc3

Please sign in to comment.