dave-hagedorn / jenkins-runner

VS Code plugin to develop and run Jenkins Pipeline scripts
MIT License
30 stars 8 forks source link

Allow configured jobs at not the top level of the Jenkins instance #17

Closed mcascone closed 4 years ago

mcascone commented 4 years ago

I LOVE this extension.

My only issue (so far) is that it appears that the jobs it can run have to be at the top level of the jenkins environment. Is there a way to put them in a folder, so the home page doesn't get cluttered with test jobs?

Again, I LOVE this!

atiniir commented 4 years ago

you can use folders. Set the path in your job entry name param like "Folder1/jobnamehere" are you trying that and it's giving you an error? if so post the info.

mcascone commented 4 years ago

sorry I haven't been able to revisit this recently, I'll update as soon as I can.

mcascone commented 4 years ago

Hi, back at this now, and seeing the same issue. My job on Jenkins path is:

http://localhost:8080/job/workflows/job/procsync/job/PLATDEV-5649-PS-jenkins-deploy-pipeline/

And I've set the config in VSC to many combinations of that... job/workflows/job/procsync/job/PLATDEV-5649-PS-jenkins-deploy-pipeline/, job/workflows/job/procsync/, /workflows/job/procsync/job/PLATDEV-5649-PS-jenkins-deploy-pipeline/, etc. I can't seem to find the right combination of paths.

And now that I think about it, i'm not necessarily talking about Jenkins folders. I should have said, "jobs that are not at the top level of the Jenkins instance."

FWIW this is a multi-branch pipeline.

thanks in advance.

mcascone commented 4 years ago

I seem to have found the trick by removing all the instances of job in the path. Now, I get a CSRF error, even after turning off Security in my local instance.

Error starting job /workflows/procsync/PLATDEV-5649-PS-jenkins-deploy-pipeline #undefined @http://localhost:8080}: Error: jenkins: job.config: jenkins: crumbIssuer.get: not found
dave-hagedorn commented 4 years ago

Hi @mcascone - multi-branch jobs aren't currently supported. Mainly because the Jenkinsfile is in SCM and not stored as part of the job config inside Jenkins - so there's no way for the plugin to update the job's script from your editor.

I'd like to support this, and am thinking of a few options, but there's no great solution that I know of.

For the CSRF error - does it make difference if you set useCrumbeIssuer to true or false in the host config?

mcascone commented 4 years ago

HI @dave-hagedorn - that seems to have done it:

      "jenkins-runner.hostConfigs": {
        "local": {
            "url": "http://localhost:8080",
            "user": "mcascone",
            "password": "<redacted token>",
            "useCrumbIssuer": false
        },

You've probably already thought of this, but could you use the Replay feature to overwrite an scm-sourced script?

dave-hagedorn commented 4 years ago

Hi @mcascone - yeah, that's a great idea and I did want to look into that. There's a few cons:

But, that might still be enough for some use cases.

dave-hagedorn commented 4 years ago

I think at the very least I can detect if the extension is trying to run a multi-branch job and provide a friendlier error.

mcascone commented 4 years ago

FWIW, i came up with a bit of a hack to let me use a single JF for either case. It's not perfect and i was running into an issue with scoping the env.BRANCH_NAME correctly, but this does have some potential. Also, obviously, you'd want to remove this from anything going into a main/master branch:

 stage('Init') {
      when { not { expression { env.BUILD_URL =~ 'localhost' } } }
      steps {
        checkout scm
      }
    }
    stage('Local Init') {
      when { expression { env.BUILD_URL =~ 'localhost' } }
      steps {
        echo 'Using local repo'
        script {
          branch = 'my_branch'
          env.BRANCH_NAME = branch
        }
        git branch: branch, url: '/path/to/local/repo/directory'
      }
    }
dave-hagedorn commented 4 years ago

FWIW, i came up with a bit of a hack to let me use a single JF for either case. It's not perfect and i was running into an issue with scoping the env.BRANCH_NAME correctly, but this does have some potential. Also, obviously, you'd want to remove this from anything going into a main/master branch:

 stage('Init') {
      when { not { expression { env.BUILD_URL =~ 'localhost' } } }
      steps {
        checkout scm
      }
    }
    stage('Local Init') {
      when { expression { env.BUILD_URL =~ 'localhost' } }
      steps {
        echo 'Using local repo'
        script {
          branch = 'my_branch'
          env.BRANCH_NAME = branch
        }
        git branch: branch, url: '/path/to/local/repo/directory'
      }
    }

Hi @mcascone - thanks for this - good to know. The most recent version lets you set env vars before running a job - this might work for you as well: https://github.com/dave-hagedorn/jenkins-runner#add-jobs--their-parameters-environments