Access to buildspec's properties from groovy script (OD-2666)
Tomasz Machalski opened 4 weeks ago

Is it possible to access buildspec's properties from groovy script?

I have a global deploy buildspec defined in parent repo, and need to parametrize it in child repos via properties (property LAB). I tried to access those properties from my get_lab_value script, but without success.

I tried to locate the relevant code and found this: https://code.onedev.io/onedev/server/~files/1d435bac543b41a8ca9c582fe765a30fd3d72904/server-core/src/main/java/io/onedev/server/job/DefaultJobService.java?position=source-776.1-794.53-1

There is a null passed as ParamCombination to resolveParams.

Any chance to have it changed? 🥺

obraz.png

  • Robin Shen commented 4 weeks ago

    Instead of defining job property in build spec, you may define property LAB in sidebar menu Settings / Build / Job Properties. Also define job executor in parent project as @property:LAB@-docker-executor. This way you can override the LAB property as necessary in child projects, without changing build spec.

  • Tomasz Machalski commented 4 weeks ago

    Thank you for the replay. Using job property seems to be nice workaround. But having LAB as param (of emum type) allows me to make deployments to multiple labs at once. Maybe I could get this job property via groovy script and use it as build param?

  • Robin Shen commented 4 weeks ago

    Thank you for the replay. Using job property seems to be nice workaround. But having LAB as param (of emum type) allows me to make deployments to multiple labs at once. Maybe I could get this job property via groovy script and use it as build param?

    I am confused. Seems that you are specifying choice values of LAB param to be a groovy script. You also want to read the LAB property in the groovy script. But choice values are expecting an array, while LAB property is a single value...

  • Tomasz Machalski commented 4 weeks ago

    I was planning to read LAB property as a string with comma separated lab names, and return it splitted as an array from the script.

  • Robin Shen commented 4 weeks ago

    Try below groovy script:

    import io.onedev.server.model.Project
    
    def labs = Project.get().getHierarchyJobProperties().find { it.name == 'LAB' }?.value
    return labs ? labs.split(/\s*,\s*/).toList() : []
    

    Property 'LAB' needs to be defined in project build settings taking comma separated values.

  • Tomasz Machalski commented 3 weeks ago

    Thanks for the tips. I will try to make it working for me. Closing the question for now.

  • Tomasz Machalski changed state to 'Closed' 3 weeks ago
    Previous Value Current Value
    Open
    Closed
  • Tomasz Machalski commented 3 weeks ago

    I was too fast with closing. I tried your snippet and got NPE. It looks like Project.get() returned null:

    Caused by: java.lang.RuntimeException: Error evaluating groovy script:

    def labs = Project.get().getHierarchyJobProperties().find { it.name == 'LAB' }?.value
    return labs ? labs.split(/\s*,\s*/).toList() : []
    	at io.onedev.server.util.GroovyUtils.evalScript(GroovyUtils.java:117)
    	at io.onedev.server.util.GroovyUtils.evalScriptByName(GroovyUtils.java:89)
    	... 26 common frames omitted
    Caused by: java.lang.NullPointerException: Cannot invoke method getHierarchyJobProperties() on null object
    
  • Tomasz Machalski changed state to 'Open' 3 weeks ago
    Previous Value Current Value
    Closed
    Open
  • Robin Shen commented 3 weeks ago

    Works at my side. Can you please create sample projects at this site demonstrating the issue?

  • Tomasz Machalski commented 3 weeks ago

    Works at my side. Can you please create sample projects at this site demonstrating the issue?

    I can try, but I don't have access to page where scripts are defined. Maybe you could temporarily add this sample script you mentioned earlier, and I will use it in my sample project.

  • Robin Shen commented 3 weeks ago

    Added script GetLabs

  • Tomasz Machalski commented 3 weeks ago

    Ok, I think I set it up correctly. Project is toomyem/test. Deploy buildspec is defined in parent project (toomyem), and imported in child (test). There is a trigger in parent which should run deploy on change in child. But when I commit a change in test, there is no new build. Probably because of the problem with null on Project.get(), but I do not have access to logs on this system to confirm that. But this is happening on mine system.

  • Robin Shen commented 3 weeks ago

    Thanks for demonstrating the issue. I ran the build from UI instead of pushing commit. The script currently can not access project object in that case. I filed an improvement request OD-2674, and close this now.

  • Robin Shen changed state to 'Closed' 3 weeks ago
    Previous Value Current Value
    Open
    Closed
  • Tomasz Machalski commented 2 weeks ago

    I tried to run the code provided in OD-2674, but sill getting NPE:

    service-1  | Caused by: java.lang.RuntimeException: Error evaluating groovy script:
    service-1  |
    service-1  | import io.onedev.server.util.ProjectScopedCommit
    service-1  |
    service-1  | def project = ProjectScopedCommit.get().getProject()
    service-1  | def props = project.getHierarchyJobProperties()
    service-1  | def labs = props.find { it.name == 'LAB' }?.value
    service-1  | return labs ? labs.split(/\s*,\s*/).toList() : []
    service-1  |    at io.onedev.server.util.GroovyUtils.evalScript(GroovyUtils.java:117)
    service-1  |    at io.onedev.server.util.GroovyUtils.evalScriptByName(GroovyUtils.java:89)
    service-1  |    ... 31 common frames omitted
    service-1  | Caused by: java.lang.NullPointerException: Cannot invoke method getProject() on null object
    
  • Tomasz Machalski changed state to 'Open' 2 weeks ago
    Previous Value Current Value
    Closed
    Open
  • Tomasz Machalski commented 2 weeks ago

    I'm running v14.0.7 by the way.

  • Robin Shen commented 2 weeks ago

    This improvement has been committed but not released yet.

  • Tomasz Machalski commented 2 weeks ago

    Ah, I see 😃 So I will wait...

  • Tomasz Machalski changed state to 'Closed' 2 weeks ago
    Previous Value Current Value
    Open
    Closed
  • Robin Shen commented 2 weeks ago

    Build OD-7034 (14.0.8) is available contain fix of issue OD-2674 . To avoid confusion, the workflow at code.onedev.io is changed so that when an issue is closed, the release will also be available.

  • Tomasz Machalski commented 2 weeks ago

    Thank you. It works fine for properties defined via Settings -> Build -> Job Properties. Any chance to extend it to Properties defined in .build-spec?

issue 1/1
Type
Question
Priority
Normal
Assignee
Labels
No labels
Issue Votes (0)
Watchers (3)
Reference
OD-2666
Please wait...
Connection lost or session expired, reload to recover
Page is in error, reload to recover