From: Thanh Ha Date: Wed, 18 Jul 2018 18:30:36 +0000 (-0400) Subject: Add cURL command to push an artifact to Nexus X-Git-Tag: v0.15.0~4 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=4a923812ad17355d18955f909db2301cf56774c4;p=releng%2Flftools.git Add cURL command to push an artifact to Nexus Issue: RELENG-1081 Change-Id: If36810924d2d0f91e04bae469067495a25c0b2dc Signed-off-by: Thanh Ha --- diff --git a/lftools/cli/deploy.py b/lftools/cli/deploy.py index 24259221..05e1a606 100644 --- a/lftools/cli/deploy.py +++ b/lftools/cli/deploy.py @@ -48,6 +48,42 @@ def archives(ctx, nexus_url, nexus_path, workspace, pattern): sys.exit(status) +@click.command() +@click.argument('nexus-url', envvar='NEXUS_URL') +@click.argument('nexus-repo-id') +@click.argument('group-id') +@click.argument('artifact-id') +@click.argument('version') +@click.argument('packaging') +@click.argument('file') +@click.pass_context +def file(ctx, + nexus_url, + nexus_repo_id, + group_id, + artifact_id, + version, + packaging, + file): + """Upload file to Nexus as a Maven artifact using cURL. + + This function will upload an artifact to Nexus while providing all of + the usual Maven pom.xml information so that it conforms to Maven 2 repo + specs. + """ + status = subprocess.call([ + 'deploy', 'file', + nexus_url, + nexus_repo_id, + group_id, + artifact_id, + version, + packaging, + file + ]) + sys.exit(status) + + @click.command() @click.argument('nexus-url', envvar='NEXUS_URL') @click.argument('nexus-path', envvar='NEXUS_PATH') @@ -238,6 +274,7 @@ def nexus_zip(ctx, nexus_url, nexus_repo, nexus_path, deploy_zip): deploy.add_command(archives) +deploy.add_command(file) deploy.add_command(logs) deploy.add_command(maven_file) deploy.add_command(nexus) diff --git a/shell/deploy b/shell/deploy index 2cfce192..4893f460 100755 --- a/shell/deploy +++ b/shell/deploy @@ -76,6 +76,11 @@ deploy() { deploy_maven_file "$@" exit 0 ;; + file ) + echo "Deploying file..." + upload_maven_file_to_nexus "$@" + exit 0 + ;; files ) echo "Deploying files..." echo "ERROR: Unimplemented." @@ -745,6 +750,54 @@ deploy_nexus_zip() { fi } +upload_maven_file_to_nexus() { + # Upload file to Nexus as a Maven artifact using cURL. + # + # This function will upload an artifact to Nexus while providing all of + # the usual Maven pom.xml information so that it conforms to Maven 2 repo + # specs. + # + # Parameters: + # nexus_url: The URL to the Nexus repo. + # (Ex: https://nexus.example.org) + # nexus_repo_id: Repo ID of repo to push artifact to. + # group_id: Maven style Group ID to upload artifact as. + # artifact_id: Maven style Artifact ID to upload artifact as. + # version: Maven style Version to upload artifact as. + # packaging: Packaging type to upload as (Eg. tar.xz) + # file: File to upload. + + local nexus_url="${1%/}" + local nexus_repo_id="$2" + local group_id="$3" + local artifact_id="$4" + local version="$5" + local packaging="$6" + local file="$7" + + local resp + local status + resp=$(curl -s -w " %{http_code}" --netrc -X POST \ + -F "r=$nexus_repo_id" \ + -F "g=$group_id" \ + -F "a=$artifact_id" \ + -F "v=$version" \ + -F "p=$packaging" \ + -F "file=@$file" \ + "${nexus_url}/service/local/artifact/maven/content") + status=$(echo "$resp" | awk 'END {print $NF}') + + if echo "$resp" | grep -q nexus-error; then + local msg + msg=$(sed -n -e 's/.*\(.*\)<\/msg>.*/\1/p' <<< $resp) + echo "ERROR: $msg" + exit "$status" + elif [ "$status" != "201" ]; then + echo "ERROR: Failed with status code $status" + exit "$status" + fi +} + upload_to_nexus() { # Helper function to push a file to Nexus via cURL #