#!/bin/bash # Creates Jenkins credentials with username and password, # made for Harbor Robot Accounts, but can be used for any username/password json file # @author thabet.amer@gmail.com # @since Jenkins 2.222.1 # @params json file with {"name":"","token":""} tags # @output Jenkins credential with ID name "harbor-NAME" [ -f "$1" ] || exit 1 # Jenkins login info - please fill accordingly JENKINS_URL='' JENKINS_USR='' JENKINS_PSW='' JENKINS_JAR='~/jenkins-cli.jar' # parsed credential details CRED_FILE=$1 CRED_USR=`cat $CRED_FILE | jq -r '.name'` CRED_PSW=`cat $CRED_FILE | jq -r '.token'` CRED_NAME=harbor-`echo $USR | cut -d$ -f2` # temp file with xml format for Jenkins plugin credentials@2.3.0 OUT_FILE=`basename $CRED_FILE | cut -d. -f1`-cred.xml # creates the xml file cat > /tmp/$OUT_FILE << EOL GLOBAL $CRED_NAME $CRED_NAME $CRED_USR $CRED_PSW EOL # Applies for Jenkins, within the Global domain cat /tmp/$OUT_FILE | java -jar $JENKINS_JAR -s $JENKINS_URL -auth $JENKINS_USR:$JENKINS_PSW create-credentials-by-xml system::system::jenkins _ rm /tmp/$OUT_FILE