New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add expression parser evaulator for build filter #2200
Add expression parser evaulator for build filter #2200
Conversation
server/rpc.go
Outdated
@@ -89,12 +91,27 @@ func (s *RPC) Next(c context.Context, filter rpc.Filter) (*rpc.Pipeline, error) | |||
} | |||
} | |||
|
|||
var st *expr.Selector | |||
var err error | |||
if filter.Expr != "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts on moving this logic into the fn
declaration? That would reduce the need for having the extra variable declarations. Or is catching a potential error from the selector initialization important?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, for caching, otherwise it would re-parse the sql string every time :)
.drone.yml
Outdated
@@ -7,6 +7,7 @@ pipeline: | |||
image: golang:1.8 | |||
commands: | |||
- go get -u github.com/drone/drone-ui/dist | |||
- go get -u github.com/drone/expr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can vendor this repository, and then remove this line. Will add a comment with instructions shortly
ok, quick guide to vendoring ... download the govendor tool
and make sure you have a local copy of the expr library
and then you can run this command:
this will add the code to the vendor directory, which gets checked into the repository. |
I'm guessing you were looking to clean up / simplify the code. That file is definitely getting a bit unruly after a lot of changes and years of evolution ... If you wanted to clean things up a bit, you could break out the function like this: func createFilterFunc(filter rpc.Filter) (queue.Filter, error) {
var st *expr.Selector
var err error
if filter.Expr != "" {
st, err = expr.ParseString(filter.Expr)
if err != nil {
return nil, err
}
}
return func(task *queue.Task) bool {
if st != nil {
match, _ := st.Eval(expr.NewRow(task.Labels))
return match
}
for k, v := range filter.Labels {
if task.Labels[k] != v {
return false
}
}
return true
}, nil
} You could then invoke this above function to get the filter, and pass to the poller: fn, err := createFilterFunc(filter)
if err != nil {
return nil, err
}
task, err := s.queue.Poll(c, fn) I am fine either way, so I'll leave the decision up to you :) |
034afa8
to
1e9e2ec
Compare
Vendor github.com/drone/expr Vendor github.com/drone/expr/parse
1e9e2ec
to
4c2ff78
Compare
good recommendation! |
Nice job, well done! Could you provide some instructions on how to use this feature? |
I am planning on writing official docs, but in the mean time, here is a quick overview. The first step is to define a query used to filter which builds the agent will accept. The query expression is similar to sql. For example: platform = 'linux/amd64' AND repo GLOB octocat/* There are two fields available by default:
The user can define additional fields in the labels section of the .drone.yml file. These fields can then be evaluated by the query expression:
For example: platform = 'linux/amd64' AND ram >= 32 AND cpu >= 16 The query expression string is passed to the agent using the |
@bradrydzewski is it possible to define labels at each step so individual steps are executed by a given agent? It does not look like is possible right now but I just wanted to confirm. |
It is not possible, but perhaps this is something that would be possible when we launch the .drone.js file, which will have more advanced scripting and conditional capabilities. |
Quick question though: this new upcoming functionality in .drone.js, is something that can be tested? Like we're in the process of choosing our CI tool and I personally like Drone and features such as this one would really help in the decision process (as well as the monorepo one ;)). |
There should be a release candidate for 0.9 end of month, which will include mono-repo support. The .drone.js will probably be part of 0.10. So will be a while. |
continuation of #2171 (comment), using the expression library from https://github.com/drone/expr