Access to buildspec's properties from groovy script (OD-2666)
Tomasz Machalski opened 1 week 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 1 week 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 1 week 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 1 week 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 1 week 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 1 week 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 5 days 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' 5 days ago
    Previous Value Current Value
    Open
    Closed
  • Tomasz Machalski commented 5 days 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' 5 days ago
    Previous Value Current Value
    Closed
    Open
  • Robin Shen commented 5 days ago

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

  • Tomasz Machalski commented 5 days 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 5 days ago

    Added script GetLabs

  • Tomasz Machalski commented 5 days 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 5 days 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' 5 days ago
    Previous Value Current Value
    Open
    Closed
issue 1/1
Type
Question
Priority
Normal
Assignee
Labels
No labels
Issue Votes (0)
Watchers (2)
Reference
OD-2666
Please wait...
Connection lost or session expired, reload to recover
Page is in error, reload to recover