Fix: Update Gradle publish job 35/72935/3
authorJessica Wagantall <jwagantall@linuxfoundation.org>
Wed, 5 Jun 2024 18:09:16 +0000 (11:09 -0700)
committerJessica Wagantall <jwagantall@linuxfoundation.org>
Wed, 5 Jun 2024 18:18:48 +0000 (11:18 -0700)
Update job to add flexibility of releasing Nexus artifacts from
a specific directory location.

Issue: RELENG-5405

Signed-off-by: Jessica Wagantall <jwagantall@linuxfoundation.org>
Change-Id: Ib2a6fe8f738f335e5d70327fe7cf76aa198edd42

docs/jjb/lf-gradle-jobs.rst
jjb/lf-gradle-jobs.yaml
releasenotes/notes/gradle-publish-update-f32aed526eb72afd.yaml [new file with mode: 0644]
shell/nexus-upload.sh [new file with mode: 0644]

index d9a8a28..4a8a962 100644 (file)
@@ -57,6 +57,11 @@ Runs a gradle publish command to publish the jar.
     :java-version: Version of Java to execute Maven build. (default: openjdk17)
     :jenkins-ssh-credential: Credential to use for SSH. (Generally configured in defaults.yaml)
     :mvn-settings: Maven settings.xml file containing credentials to use.
+    :publish-credential: Project credential used for accessing Nexus
+    :publish-directory: Location of built artifacts
+    :publish-file-extension: Artifact's file extension
+    :publish-url: Nexus publishing repo location
+
     :wrapper: Use the gradle wrapper (default: true)
 
 :Optional parameters:
index ba15f11..a7ba72b 100644 (file)
     git-url: "$GIT_URL/$PROJECT"
     github-url: https://github.com
     java-version: openjdk17
+    publish-credential: ""
+    publish-directory: ""
+    publish-file-extension: jar
+    publish-url: ""
     stream: master
     submodule-recursive: true
     submodule-timeout: 10
       - lf-infra-wrappers:
           build-timeout: "{build-timeout}"
           jenkins-ssh-credential: "{jenkins-ssh-credential}"
-
+      - credentials-binding:
+          - username-password-separated:
+              credential-id: "{publish-credential}"
+              username: NEXUS_USERNAME
+              password: NEXUS_PASSWORD
     publishers:
       - lf-infra-publish
 
           executable: true
           tasks: |
             shadowJar
-            publish
-
+      - inject:
+          properties-content: |
+            NEXUS_URL={publish-url}
+            DIRECTORY={publish-directory}
+            FILE_EXTENSION={publish-file-extension}
+      - shell: !include-raw-escape:
+          - ../shell/nexus-upload.sh
     scm:
       - lf-infra-gerrit-scm:
           jenkins-ssh-credential: "{jenkins-ssh-credential}"
diff --git a/releasenotes/notes/gradle-publish-update-f32aed526eb72afd.yaml b/releasenotes/notes/gradle-publish-update-f32aed526eb72afd.yaml
new file mode 100644 (file)
index 0000000..14b82d1
--- /dev/null
@@ -0,0 +1,5 @@
+---
+fixes:
+  - |
+    Update Gradle publish job to add flexibility of releasing Nexus artifacts from
+    a specific directory location.
diff --git a/shell/nexus-upload.sh b/shell/nexus-upload.sh
new file mode 100644 (file)
index 0000000..0141f24
--- /dev/null
@@ -0,0 +1,119 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: Apache-2.0
+# Copyright 2024 The LMinux Foundation <matthew.watkins@linuxfoundation.org>
+# Uncomment to enable debugging
+# set -vx
+# Initialise variables
+DIRECTORY="."
+FILE_EXTENSION=""
+# Count file upload successes/failures
+SUCCESSES="0"; FAILURES="0"
+# Shared functions
+show_help() {
+    # Command usage help
+    cat << EOF
+Usage: ${0##*/} [-h] [-u user] [-p password] [-s upload-url] [-e extensions] [-d folder]
+    -h  display this help and exit
+    -u  username (or export variable NEXUS_USERNAME)
+    -p  password (or export variable NEXUS_PASSWORD)
+    -s  upload URL (or export variable NEXUS_URL)
+        e.g. https://nexus3.o-ran-sc.org/repository/datasets/
+    -e  file extensions to match, e.g. csv, txt
+    -d  local directory hosting files/content to be uploaded
+EOF
+}
+error_help() {
+    show_help >&2
+    exit 1
+}
+transfer_report() {
+    echo "Successes: $SUCCESSES   Failures: $FAILURES"
+    if [ "$FAILURES" -gt 0 ]; then
+        exit 1
+    else
+        exit 0
+    fi
+}
+curl_upload() {
+    FILE="$1"
+    echo "Sending: ${FILE}"
+    # echo "Running: $CURL --fail [CREDENTIALS] --upload-file $FILE $NEXUS_URL"
+    if ("$CURL" --fail -u "$CREDENTIALS" --upload-file "$FILE" "$NEXUS_URL"); then #> /dev/null 2>&1
+        SUCCESSES=$((SUCCESSES+1))
+    else
+        FAILURES=$((FAILURES+1))
+    fi
+}
+process_files() {
+    for FILE in "${UPLOAD_FILES_ARRAY[@]}"; do
+        curl_upload "$FILE"
+    done
+}
+# Validate/check arguments and variables
+CURL=$(which curl)
+if [ ! -x "$CURL" ];then
+    echo "CURL was not found in your PATH"; exit 1
+fi
+while getopts hu:p:s:d:e: opt; do
+    case $opt in
+        u)  NEXUS_USERNAME="$OPTARG"
+            ;;
+        p)  NEXUS_PASSWORD="$OPTARG"
+            ;;
+        s)  NEXUS_URL="$OPTARG"
+            ;;
+        e)  FILE_EXTENSION="$OPTARG"
+            ;;
+        d)  DIRECTORY="$OPTARG"
+            if [ ! -d "$DIRECTORY" ]; then
+                echo "Error: specified directory invalid"; exit 1
+            fi
+            ;;
+        h|?)
+            show_help
+            exit 0
+            ;;
+        *)
+            error_help
+        esac
+done
+shift "$((OPTIND -1))"   # Discard the options
+# Gather location of files to upload (in an array)
+mapfile -t UPLOAD_FILES_ARRAY < <(find "$DIRECTORY" -name "*$FILE_EXTENSION" -type f -print )
+if [ "${#UPLOAD_FILES_ARRAY[@]}" -ne 0 ]; then
+    echo "Files found to upload: ${#UPLOAD_FILES_ARRAY[@]}"
+    # echo "Files matching pattern:"  # Uncomment for debugging
+    # echo "${UPLOAD_FILES_ARRAY[@]}"  # Uncomment for debugging
+else
+    echo "Error: no files found to process matching pattern"
+    exit 1
+fi
+if [ -z "$NEXUS_URL" ]; then
+    echo "ERROR: Specifying the upload/repository URL is mandatory"; exit 1
+else
+    if  [[ ! "$NEXUS_URL" == "http://"* ]] && \
+        [[ ! "$NEXUS_URL" == "https://"* ]]; then
+        echo "Error: Nexus server must be specified as a URL"; exit 1
+    fi
+fi
+# Prompt for credentials if not specified explicitly or present in the shell environment
+if [ -z "$NEXUS_USERNAME" ]; then
+    echo -n "Enter username: "
+    read -r NEXUS_USERNAME
+    if [[ -z "$NEXUS_USERNAME" ]]; then
+        echo "ERROR: Username cannot be empty"; exit 1
+    fi
+fi
+if [ -z "$NEXUS_PASSWORD" ]; then
+    echo -n "Enter password: "
+    read -s -r NEXUS_PASSWORD  # Does not echo to terminal/console
+    echo ""
+    if [[ -z "$NEXUS_PASSWORD" ]]; then
+        echo "ERROR: Password cannot be empty"; exit 1
+    fi
+fi
+CREDENTIALS="$NEXUS_USERNAME:$NEXUS_PASSWORD"
+# Main script entry point
+echo "Uploading to: $NEXUS_URL"
+process_files
+transfer_report