how to fetch jenkins fqdn from jenkins_url in groovy script

Team,
I am unable to store splitted var and use it in bash script in groovy.

below is the env var of the jenkins container on which bash script is to run.

JENKINS_URL=https://dev.blsm.comp.com/stuff-verify/

I want to pull just the dev.blsm.comp.com and use it in bash script. so I did below but yes it won’t work because I don’t know how to split in a way to get hostname alone.

def hostfqdn = "${env.JENKINS_URL}".split("https://stackoverflow.com/")

any hint?

            steps {
                container('main') {
                    script {
                        withCredentials([[$class: 'VaultUsernamePasswordCredentialBinding', credentialsId: 'vaulted-jenkins-api-token' , passwordVariable: 'JENKINS_USER_TOKEN', usernameVariable: 'JENKINS_USERNAME']]) {
                            sh ''' echo $hostfqdn '''
                            sh '''export JENKINS_CRUMB=$(curl -u "$JENKINS_USERNAME:$JENKINS_USER_TOKEN" -s --cookie-jar /tmp/cookies $JENKINS_URL'/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')'''
                            sh(script:'''#!/bin/bash
                                echo ${hostfqdn}
                                curl -X POST https://${JENKINS_USERNAME}:${JENKINS_USER_TOKEN}@${hostfqdn}/credentials/store/system/domain/_/createCredentials \
                                --data-urlencode 'json={
                                "credentials": {
                                    "scope": "GLOBAL",
                                    "id": "identification",
                                    "username": "manu",
                                    "password": "bar",
                                    "description": "linda1",
                                    "$class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"
                                }
                                }'
                                echo "Creds link is ${credsLink}"
                            ''')
                        }
                    }
                }
            }

error

curl: (3) URL using bad/illegal format or missing URL
Creds link is 
[Pipeline] }

Leave a Comment