Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-28466

Can't use environment variables like JENKINS_URL、JOB_URL、BUILD_URL in a new jenkins before it's save-button which is in system settings is clicked

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Major Major
    • core
    • Jenkins ver. 1.592

      when I start to use a new Jenkins, some environment variables like JENKINS_URL、JOB_URL、BUILD_URL can't be used.But when I click the save-button in system settings ,those environment variables works.So i doubt is it right?

          [JENKINS-28466] Can't use environment variables like JENKINS_URL、JOB_URL、BUILD_URL in a new jenkins before it's save-button which is in system settings is clicked

          Daniel Beck added a comment -

          This value is present immediately after provisioning the container.

          … and it's based on the request URL that is only available in the context of a request. If Jenkins is running on a different port, or you access it with a different host name, those would be reflected here.

          You opening the config page: In an HTTP request context.
          A build running: Not in an HTTP request context.

          So your argument based on this form value doesn't make sense.

          FWIW just setting 'localhost:8080' as the default would be wrong for pretty much all deployments to production, and not allow the automatism of filling the Configure System form field with a real default.

          Daniel Beck added a comment - This value is present immediately after provisioning the container. … and it's based on the request URL that is only available in the context of a request. If Jenkins is running on a different port, or you access it with a different host name, those would be reflected here. You opening the config page: In an HTTP request context. A build running: Not in an HTTP request context. So your argument based on this form value doesn't make sense. FWIW just setting 'localhost:8080' as the default would be wrong for pretty much all deployments to production, and not allow the automatism of filling the Configure System form field with a real default.

          Barry Laffoy added a comment - - edited

          Okay, that actually makes a lot more sense to me. I didn't realise that the context from which it was populating the field in the form was the http request to open the form.

          I withdraw my assertion that this is definitely a bug.

          It just gets frustrating that a tool like Jenkins which is designed for automation, fights me every step of the way when I try to build automation around it.

          P.S. Thanks for the quick response

          Barry Laffoy added a comment - - edited Okay, that actually makes a lot more sense to me. I didn't realise that the context from which it was populating the field in the form was the http request to open the form. I withdraw my assertion that this is definitely a bug. It just gets frustrating that a tool like Jenkins which is designed for automation, fights me every step of the way when I try to build automation around it. P.S. Thanks for the quick response

          Alex Earl added a comment -

          It might be useful for you to post to the users mailing list with some issues you've been having. I know there are lots of people who are automating the setup and configuration of Jenkins, so maybe there is someone who can point you to some existing blogs or similar that have information you might find useful.

          Alex Earl added a comment - It might be useful for you to post to the users mailing list with some issues you've been having. I know there are lots of people who are automating the setup and configuration of Jenkins, so maybe there is someone who can point you to some existing blogs or similar that have information you might find useful.

          Barry Laffoy added a comment - - edited

          Another work-around copy a groovy script to /usr/share/jenkins/ref/init.groovy.d/ before starting the Jenkins process, with the environment variable _JENKINS_URL set to the desired value:

          import jenkins.model.JenkinsLocationConfiguration
          def jlc = jenkins.model.JenkinsLocationConfiguration.get()
          def url = System.getenv('_JENKINS_URL')
          jlc.setUrl(url)
          

          slide_o_mixl Thanks for the suggestion, I've been over there quite a bit and found some helpful info. The jenkinsci/docker image is the best resource I've found to ease this pain, but it still needs a lot of end-user work in order to fit with existing Jenkins deployments. I just find it frustrating that a tool so popular with a community obsessed with automation almost seems to have been designed purposefully to thwart any attempt at automation.

          Barry Laffoy added a comment - - edited Another work-around copy a groovy script to /usr/share/jenkins/ref/init.groovy.d/ before starting the Jenkins process, with the environment variable _JENKINS_URL set to the desired value: import jenkins.model.JenkinsLocationConfiguration def jlc = jenkins.model.JenkinsLocationConfiguration.get() def url = System .getenv( '_JENKINS_URL' ) jlc.setUrl(url) slide_o_mix l Thanks for the suggestion, I've been over there quite a bit and found some helpful info. The jenkinsci/docker image is the best resource I've found to ease this pain, but it still needs a lot of end-user work in order to fit with existing Jenkins deployments. I just find it frustrating that a tool so popular with a community obsessed with automation almost seems to have been designed purposefully to thwart any attempt at automation.

          Alex Earl added a comment -

          Please file issues on the things you run into, it won't get better if people don't tell developers what issues they have.

          Alex Earl added a comment - Please file issues on the things you run into, it won't get better if people don't tell developers what issues they have.

          Barry Laffoy added a comment -

          slide_o_mix, I will bear that in mind. It's likely that every issue I want to file can be summarised as "Why can't I configure <something> with an environment variable and/or a configuration file to aid automation?"

          On this topic, even if the current behaviour of JENKINS_URL isn't being considered a bug, I think that the fact that the config settings screen is actively misleading to users should be considered one. Either the field should not be populated from the HTTP context of the request, or it should be clearly marked as a suggestion that has not actually been added to Jenkins' configuration.

          Barry Laffoy added a comment - slide_o_mix , I will bear that in mind. It's likely that every issue I want to file can be summarised as "Why can't I configure <something> with an environment variable and/or a configuration file to aid automation?" On this topic, even if the current behaviour of JENKINS_URL isn't being considered a bug, I think that the fact that the config settings screen is actively misleading to users should be considered one. Either the field should not be populated from the HTTP context of the request, or it should be clearly marked as a suggestion that has not actually been added to Jenkins' configuration.

          Chris Weaver added a comment -

          I have the same problem for the same reasons (fully automating a Jenkins install) and I still don't understand how this is not a bug. I see a configuration page that is full of configuration, but only half of it is actually in the system until I click "Save". So how can I trust the information on this page? Or any page?

          There must be a better solution than "go through every configuration page clicking Save as soon as Jenkins comes up".

          Chris Weaver added a comment - I have the same problem for the same reasons (fully automating a Jenkins install) and I still don't understand how this is not a bug. I see a configuration page that is full of configuration, but only half of it is actually in the system until I click "Save". So how can I trust the information on this page? Or any page? There must be a better solution than "go through every configuration page clicking Save as soon as Jenkins comes up".

          Mark Waite added a comment -

          cweaver, no one suggested that anyone needs to

          go through every configuration page clicking Save as soon as Jenkins comes up

          The JENKINS_URL needs to be configured before it can be returned. It is configured through the XML file jenkins.model.JenkinsLocationConfiguration.xml or through the groovy script that laffoyb provided in an earlier comment.

          I use the XML file in a private copy of my Docker image. It has worked very well for me automating deployment to multiple machines in multiple environments.

          Mark Waite added a comment - cweaver , no one suggested that anyone needs to go through every configuration page clicking Save as soon as Jenkins comes up The JENKINS_URL needs to be configured before it can be returned. It is configured through the XML file jenkins.model.JenkinsLocationConfiguration.xml or through the groovy script that laffoyb provided in an earlier comment . I use the XML file in a private copy of my Docker image . It has worked very well for me automating deployment to multiple machines in multiple environments.

          Pardeep Chahal added a comment - - edited

          We struggled for two days to find out this issue. Thanks for the help. Our Jenkins version is 2.85 and I am not sure if this issue works fine in older versions or not. 

          To replicate and fix this issue, please follow the below steps:

          1. Jenkins_URL or Jenkins parameters values must be coming null when you launch a fresh instance of Jenkins and trying to trigger some automation jobs, which uses environment variables. 
          2. To fix issue manually, Go to Manage Jenkins -> Configure System -> Save settings (without making any change). This should fix the issue if you are doing Jenkins configuration manually.
          3. To fix it during installation, follow steps mentioned by markewaite
          4. Create JENKINS_HOME/data/jenkins.model.JenkinsLocationConfiguration.xml and add below contents
          5. <?xml version='1.0' encoding='UTF-8'?>

          <jenkins.model.JenkinsLocationConfiguration>  <adminAddress>address not configured yet <nobody@nowhere></adminAddress>  <jenkinsUrl>JENKINS_URL_VALUE/</jenkinsUrl></jenkins.model.JenkinsLocationConfiguration>

          Pardeep Chahal added a comment - - edited We struggled for two days to find out this issue. Thanks for the help. Our Jenkins version is 2.85 and I am not sure if this issue works fine in older versions or not.  To replicate and fix this issue, please follow the below steps: Jenkins_URL or Jenkins parameters values must be coming null when you launch a fresh instance of Jenkins and trying to trigger some automation jobs, which uses environment variables.  To fix issue manually, Go to Manage Jenkins -> Configure System -> Save settings (without making any change). This should fix the issue if you are doing Jenkins configuration manually. To fix it during installation, follow steps mentioned by markewaite .  Create JENKINS_HOME/data/jenkins.model.JenkinsLocationConfiguration.xml and add below contents <?xml version='1.0' encoding='UTF-8'?> <jenkins.model.JenkinsLocationConfiguration>   <adminAddress>address not configured yet <nobody@nowhere></adminAddress>   <jenkinsUrl>JENKINS_URL_VALUE/</jenkinsUrl> </jenkins.model.JenkinsLocationConfiguration>

          Daniel Beck added a comment -

          Fix in progress (admin monitor if it's unset, and adding it to the setup wizard) in the context of JENKINS-31661 so resolving as duplicate. The issues are related enough.

          Daniel Beck added a comment - Fix in progress (admin monitor if it's unset, and adding it to the setup wizard) in the context of JENKINS-31661 so resolving as duplicate. The issues are related enough.

            Unassigned Unassigned
            hz_tc liu yecheng
            Votes:
            3 Vote for this issue
            Watchers:
            12 Start watching this issue

              Created:
              Updated:
              Resolved: