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

legacyIds missing when creating jobs via REST API or CLI

    • 2.460

      When creating a new job from the REST API using an XML file:

      curl -u <user>:<pass> \
      	 -H "Content-Type:application/xml" \
      	 -X POST http://jenkins.example.com/createItem?name=TEST_JOB \
      	 -d @config.xml
      

      The job is correctly created BUT the builds directory does not contains the legacyIds file. The same happens when using the create-job command with the Jenkins CLI, we see the same behavior.

      When creating a job from the UI however, the legacyIds is correctly added.

      Impact

      The absence of the legacyIds file cause the RunIdMigrator to "migrate" that job on the next startup.

      jenkins.model.RunIdMigrator#migrate: Migrating build records in <pathToJobDirectory>/builds
      

      If many jobs are created using the REST API, this can slow down the next startup considerably. Some users that are automating the creating of items could be impacted.

      Workaround

      Create the job first "from scratch" then update the XML file:

      # create a pipeline job from scratch
      curl -X POST \
      	 -u <user>:<pass> \
      	 -H "Content-Type:application/x-www-form-urlencoded" \
      	 "http://jenkins.example.com/createItem?name=TEST_JOB&mode=org.jenkinsci.plugins.workflow.job.WorkflowJob"
      
      # update the job configuration by providing the XML
      curl -X POST \
      	 -u <user>:<pass> \
      	 -H "Content-Type:application/xml" \
      	 -d @config.xml \
      	 "http://jenkins.example.com/job/TEST_JOB/config.xml"
      

      Further Note

      The "RunIdMigrator" is instantiated in Job and AbstractProject when the Item#onCreatedFromScratch is called. But this is not called when using the REST API to copy a job or to create an item from XML:

          [JENKINS-64356] legacyIds missing when creating jobs via REST API or CLI

          Allan BURDAJEWICZ created issue -
          Allan BURDAJEWICZ made changes -
          Description Original: When creating a new job from the REST API using an XML file:

          {code}
          curl -u <user>:<pass> \
          -H "Content-Type:application/xml" \
          -X POST http://jenkins.example.com/createItem?name=TEST_JOB \
          -d @config.xml
          {code}

          The job is correctly created BUT the builds directory does not contains the {{legacyIds}} file. The same happens when using the {{create-job}} command with the Jenkins CLI, we see the same behavior.

          When creating a job from the UI however, the {{legacyIds}} is correctly added.

          h3. Impact

          The absence of the {{legacyIds}} file cause the {{RunIdMigrator}} to "migrate" that job on the next startup. If many jobs are created using the REST API, this can slow down the next startup considerably. Some users that are automating the creating of items could be impacted.

          h3. Workaround

          Create the job first "from scratch" then update the XML file:

          {code}
          # create a pipeline job from scratch
          curl -X POST \
          -u <user>:<pass> \
          -H "Content-Type:application/x-www-form-urlencoded" \
          "http://jenkins.example.com/createItem?name=TEST_JOB&mode=org.jenkinsci.plugins.workflow.job.WorkflowJob"

          # update the job configuration by providing the XML
          curl -X POST \
          -u <user>:<pass> \
          -H "Content-Type:application/xml" \
          -d @config.xml \
          "http://jenkins.example.com/job/TEST_JOB/config.xml"
          {code}

          h3. Further Note

          The "RunIdMigrator" is instantiated in {{Job}} and {{AbstractProject}} when the [Item#onCreatedFromScratch|https://github.com/jenkinsci/jenkins/blob/jenkins-2.269/core/src/main/java/hudson/model/Item.java#L228-L236] is called. But this is not called when using the REST API to copy a job or to create an item from XML:

          * https://github.com/jenkinsci/jenkins/blob/jenkins-2.269/core/src/main/java/hudson/model/ItemGroupMixIn.java#L219-L261
          * https://github.com/jenkinsci/jenkins/blob/jenkins-2.269/core/src/main/java/hudson/model/ItemGroupMixIn.java#L263-L306
          New: When creating a new job from the REST API using an XML file:

          {code}
          curl -u <user>:<pass> \
          -H "Content-Type:application/xml" \
          -X POST http://jenkins.example.com/createItem?name=TEST_JOB \
          -d @config.xml
          {code}

          The job is correctly created BUT the builds directory does not contains the {{legacyIds}} file. The same happens when using the {{create-job}} command with the Jenkins CLI, we see the same behavior.

          When creating a job from the UI however, the {{legacyIds}} is correctly added.

          h3. Impact

          The absence of the {{legacyIds}} file cause the {{RunIdMigrator}} to "migrate" that job on the next startup.

          {code}
          jenkins.model.RunIdMigrator#migrate: Migrating build records in <pathToJobDirectory>/builds
          {code}

          If many jobs are created using the REST API, this can slow down the next startup considerably. Some users that are automating the creating of items could be impacted.

          h3. Workaround

          Create the job first "from scratch" then update the XML file:

          {code}
          # create a pipeline job from scratch
          curl -X POST \
          -u <user>:<pass> \
          -H "Content-Type:application/x-www-form-urlencoded" \
          "http://jenkins.example.com/createItem?name=TEST_JOB&mode=org.jenkinsci.plugins.workflow.job.WorkflowJob"

          # update the job configuration by providing the XML
          curl -X POST \
          -u <user>:<pass> \
          -H "Content-Type:application/xml" \
          -d @config.xml \
          "http://jenkins.example.com/job/TEST_JOB/config.xml"
          {code}

          h3. Further Note

          The "RunIdMigrator" is instantiated in {{Job}} and {{AbstractProject}} when the [Item#onCreatedFromScratch|https://github.com/jenkinsci/jenkins/blob/jenkins-2.269/core/src/main/java/hudson/model/Item.java#L228-L236] is called. But this is not called when using the REST API to copy a job or to create an item from XML:

          * https://github.com/jenkinsci/jenkins/blob/jenkins-2.269/core/src/main/java/hudson/model/ItemGroupMixIn.java#L219-L261
          * https://github.com/jenkinsci/jenkins/blob/jenkins-2.269/core/src/main/java/hudson/model/ItemGroupMixIn.java#L263-L306
          Allan BURDAJEWICZ made changes -
          Summary Original: legacyIds missing when creating job via REST API New: legacyIds missing when creating jobs via REST API or CLI
          David C. Bradley made changes -
          Link New: This issue causes JENKINS-59402 [ JENKINS-59402 ]

          David C. Bradley added a comment - For documentation, here's a CloudBees article related to this issue: https://docs.cloudbees.com/docs/cloudbees-ci-kb/latest/client-and-managed-masters/jenkins-startup-logs-show-runidmigrator-logs
          Basil Crow made changes -
          Labels New: help-wanted newbie-friendly

          Basil Crow added a comment -

          Two ignored unit tests were added in https://github.com/jenkinsci/jenkins/pull/8758 so this task should now be as simple as removing @Ignore and then adding the missing call to Item#onCreatedFromScratch to get the tests to pass.

          Basil Crow added a comment - Two ignored unit tests were added in https://github.com/jenkinsci/jenkins/pull/8758 so this task should now be as simple as removing @Ignore and then adding the missing call to Item#onCreatedFromScratch to get the tests to pass.
          Alexander Brandes made changes -
          Released As New: 2.438
          Resolution New: Fixed [ 1 ]
          Status Original: Open [ 1 ] New: Closed [ 6 ]

          Basil Crow added a comment -

          notmyfault Why was this ticket closed?

          Basil Crow added a comment - notmyfault Why was this ticket closed?
          Alexander Brandes made changes -
          Resolution Original: Fixed [ 1 ]
          Status Original: Closed [ 6 ] New: Reopened [ 4 ]

            mawinter69 Markus Winter
            allan_burdajewicz Allan BURDAJEWICZ
            Votes:
            4 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: