Skip to content

Commit

Permalink
Always fallback (#45)
Browse files Browse the repository at this point in the history
* feat: add always fallback option
* docs: add PLUGIN_ALWAYS_FALLBACK to README.md
* test: add test cases for always fallback
* fix: rename "always fallback" to "always run all"
  • Loading branch information
Ben10k authored Oct 6, 2023
1 parent 0e32a0d commit 5aeabbc
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ Currently supports

* `PLUGIN_CONCAT`: Concats all found configs to a multi-machine build. Defaults to `false`.
* `PLUGIN_FALLBACK`: Rebuild all .drone.yml if no changes where made. Defaults to `false`.
* `PLUGIN_MAXDEPTH`: Max depth to search for `.drone.yml`, only active in fallback mode. Defaults to `2` (would still find `/a/b/.drone.yml`).
* `PLUGIN_ALWAYS_RUN_ALL`: Always rebuild all .drone.yml. Useful when repository has a global dependency, like executing tests on all projects in repo before building individual artefacts. Defaults to `false`.
* `PLUGIN_MAXDEPTH`: Max depth to search for `.drone.yml`, only active in fallback and always fallback modes or when pipeline was triggered by cron. Defaults to `2` (would still find `/a/b/.drone.yml`).
* `PLUGIN_DEBUG`: Set this to `true` to enable debug messages.
* `PLUGIN_ADDRESS`: Listen address for the plugins webserver. Defaults to `:3000`.
* `PLUGIN_SECRET`: Shared secret with drone. You can generate the token using `openssl rand -hex 16`.
Expand Down Expand Up @@ -180,4 +181,4 @@ Example (expire after 30 minutes);

Depending on the size and the complexity of the repository, using a cache can significantly reduce the number of API
calls made to the provider (github, bitbucket, other). The reduction in API calls reduces the risk of being rate
limited and can result in less processing time for drone-tree-config.
limited and can result in less processing time for drone-tree-config.
2 changes: 2 additions & 0 deletions cmd/drone-tree-config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type (
AllowListFile string `envconfig:"PLUGIN_ALLOW_LIST_FILE"`
Concat bool `envconfig:"PLUGIN_CONCAT"`
MaxDepth int `envconfig:"PLUGIN_MAXDEPTH" default:"2"`
AlwaysRunAll bool `envconfig:"PLUGIN_ALWAYS_RUN_ALL"`
Fallback bool `envconfig:"PLUGIN_FALLBACK"`
Debug bool `envconfig:"PLUGIN_DEBUG"`
Address string `envconfig:"PLUGIN_ADDRESS" default:":3000"`
Expand Down Expand Up @@ -58,6 +59,7 @@ func main() {
plugin.New(
plugin.WithConcat(spec.Concat),
plugin.WithFallback(spec.Fallback),
plugin.WithAlwaysRunAll(spec.AlwaysRunAll),
plugin.WithMaxDepth(spec.MaxDepth),
plugin.WithServer(spec.Server),
plugin.WithAllowListFile(spec.AllowListFile),
Expand Down
7 changes: 7 additions & 0 deletions plugin/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ func WithFallback(fallback bool) func(*Plugin) {
}
}

// WithAlwaysRunAll configures always run all enabled or disabled
func WithAlwaysRunAll(alwaysRunAll bool) func(*Plugin) {
return func(p *Plugin) {
p.alwaysRunAll = alwaysRunAll
}
}

// WithMaxDepth configures with max depth to search for 'drone.yml'. Requires fallback to be enabled.
func WithMaxDepth(maxDepth int) func(*Plugin) {
return func(p *Plugin) {
Expand Down
10 changes: 9 additions & 1 deletion plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type (

concat bool
fallback bool
alwaysRunAll bool
maxDepth int
allowListFile string
considerFile string
Expand Down Expand Up @@ -137,7 +138,14 @@ func (p *Plugin) getConfigData(ctx context.Context, req *request) (string, error

// get drone.yml for changed files or all of them if no changes/cron
configData := ""
if changedFiles != nil {

if p.alwaysRunAll {
logrus.Warnf("%s always run all enabled, rebuilding all", req.UUID)
if p.considerFile == "" {
logrus.Warnf("recursively scanning for config files with max depth %d", p.maxDepth)
}
configData, err = p.getConfigForTree(ctx, req, "", 0)
} else if changedFiles != nil {
configData, err = p.getConfigForChanges(ctx, req, changedFiles)
} else if req.Build.Trigger == "@cron" {
logrus.Warnf("%s @cron, rebuilding all", req.UUID)
Expand Down
61 changes: 61 additions & 0 deletions plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,67 @@ func TestCronConcat(t *testing.T) {
t.Errorf("Want\n %q\ngot\n %q", want, got)
}
}
func TestAlwaysRunAll(t *testing.T) {
req := &config.Request{
Build: drone.Build{
Before: "2897b31ec3a1b59279a08a8ad54dc360686327f7",
After: "8ecad91991d5da985a2a8dd97cc19029dc1c2899",
Source: "master",
},
Repo: drone.Repo{
Namespace: "foosinn",
Name: "dronetest",
Slug: "foosinn/dronetest",
Config: ".drone.yml",
},
}
plugin := New(
WithServer(ts.URL),
WithGithubToken(mockToken),
WithAlwaysRunAll(true),
WithMaxDepth(2),
)
droneConfig, err := plugin.Find(noContext, req)
if err != nil {
t.Error(err)
return
}

if want, got := "---\nkind: pipeline\nname: default\n\nsteps:\n- name: frontend\n image: node\n commands:\n - npm install\n - npm test\n\n- name: backend\n image: golang\n commands:\n - go build\n - go test\n", droneConfig.Data; want != got {
t.Errorf("Want\n %q\ngot\n %q", want, got)
}
}
func TestAlwaysRunAllConcat(t *testing.T) {
req := &config.Request{
Build: drone.Build{
Before: "2897b31ec3a1b59279a08a8ad54dc360686327f7",
After: "8ecad91991d5da985a2a8dd97cc19029dc1c2899",
Source: "master",
},
Repo: drone.Repo{
Namespace: "foosinn",
Name: "dronetest",
Slug: "foosinn/dronetest",
Config: ".drone.yml",
},
}
plugin := New(
WithServer(ts.URL),
WithGithubToken(mockToken),
WithConcat(true),
WithAlwaysRunAll(true),
WithMaxDepth(2),
)
droneConfig, err := plugin.Find(noContext, req)
if err != nil {
t.Error(err)
return
}

if want, got := "---\nkind: pipeline\nname: default\n\nsteps:\n- name: frontend\n image: node\n commands:\n - npm install\n - npm test\n\n- name: backend\n image: golang\n commands:\n - go build\n - go test\n---\nkind: pipeline\nname: default\n\nsteps:\n- name: build\n image: golang\n commands:\n - go build\n - go test -short\n\n- name: integration\n image: golang\n commands:\n - go test -v\n", droneConfig.Data; want != got {
t.Errorf("Want\n %q\ngot\n %q", want, got)
}
}

func TestStarlark(t *testing.T) {
req := &config.Request{
Expand Down

0 comments on commit 5aeabbc

Please sign in to comment.