argoproj / argoproj/argo-workflows
Conditionally Run Workflows Based on Git Artifact Files Changed
- Dominant language
- Go
- Stars
- 17k
- Forks
- 3.7k
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 138
Description
# Summary
It would be useful to add a ```key:list(values)``` option to the git artifact stanza of the Workflow/Workflow-Template CRD where an operator can specify the paths of files that, when modified, will trigger the workflow.
- Example:
``` yaml
artifacts:
- name: my-git-repo
path: "/home/argo"
git:
repo: "my-repo.git"
depth: n
filter:
watchPaths:
- service1/*
```
# Use Cases
I ran into a scenario where I needed this functionality to set a working directory for a later stage in my workflow, based on the files changed in the repo. That was specifically for a Terraform workflow, but I could see this also being useful in monorepos containing multiple microservices where builds should only happen when code specific directories has changed.
I was able to achieve this by developing a custom go program that leverages the [go-git](https://pkg.go.dev/github.com/go-git/go-git/v5) package to derive file changes that occur when an Event triggers a Workflow. This runs as a pre-build stage in the workflow currently, and passes the working directories from one stage to the next.
Here is a snippet of my code which is triggered if the event is a pull request that I think could likely be a good starting point to add this feature. (If the event is just a commit to main or any branch that builds, there is less logic because you only have to compare the 2 latest commits instead of every ancestor of main)
``` go
ref, _ := repo.Log(&git.LogOptions{
From: myBranchRef,
})
ref.ForEach(func(c *object.Commit) error {
ancestor, _ := c.IsAncestor(currentCommit)
if logger.Check() {
log.Printf("Is ancestor: %t", ancestor)
}
if ancestor {
appendOn = false
return nil
}
if appendOn {
commits = append(commits, c)
}
return nil
})
for _, v := range commits {
diff, err := v.Patch(currentCommit)
if err != nil {
log.Fatal(err)
}
filesPatched := diff.FilePatches()
for _, fileList := range filesPatched {
if fileList.IsBinary() {
log.Println("Ignoring binary files")
continue
}
from, to := fileList.Files()
if from.Path() == to.Path() {
filesChanged = append(filesChanged, from.Path())
}
}
}
```
---
**Message from the maintainers**:
Love this enhancement proposal? Give it a 👍. We prioritise the proposals with the most 👍.
Contributor guide
Research direction
The issue names the Workflow/Workflow-Template CRD and git artifact stanza, but no repository files or tests. Start by locating those CRD definitions and the git artifact handling path, then inspect how branch and pull-request changes are compared. Done means watchPaths is accepted and workflows trigger only when matching files change, with tests for matching and non-matching paths.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- git, go, kubernetes
- Domain
- ci-cd, devops
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100