GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: Deploy a function from a git repository without a local copy by gauron99 · Pull Request #4064 · knative/func

feat: Deploy a function from a git repository without a local copy #4064

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.

Sign up for GitHub

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

Merged
knative-prow merged 3 commits into knative:main from gauron99:push-zwpsoszkmytz
Sep 29, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

feat: Deploy a function from a git repository without a local copy #4064

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
07c6541
feat: load a function from a git repository without a checkout
gauron99 Sep 24, 2026
a4ac621
fix: remote pipeline builds a function with no local directory
gauron99 Sep 24, 2026
78a8d01
feat: deploy --remote --source reads the function from the repository
gauron99 Sep 24, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
fix: remote pipeline builds a function with no local directory
A function read from its git repository has no Root. The pipeline
provider now requires a Root only to upload sources, and does not look
for Pipeline or PipelineRun overrides in a project directory it does
not have.

Such a function carries the commit it was read at. The cluster fetches
exactly that commit, not whatever the branch points to by then, and
labels the image with it. Without a Root, the label is no longer taken
from whatever repository func happens to run in. Anything else is
fetched and labelled as before.

The revision is quoted in the PipelineRun: a commit hash can read as a
YAML number.
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@gauron99
gauron99 committed Sep 28, 2026
commit a4ac621101d4ae1d8afd69fa4684a7c9f700039a

There are no files selected for viewing

6 changes: 6 additions & 0 deletions 6 pkg/pipelines/tekton/pipelines_provider.go
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ func (pp *PipelinesProvider) Run(ctx context.Context, f fn.Function) (string, fn
return "", f, err
}

// The source is either a git repository or the local working tree, which
// is uploaded. A function loaded from git has no Root.
if f.Build.Source.URL == "" && f.Root == "" {
return "", f, errors.New("a local function directory is required to upload sources; set a git URL to build from a repository")
}

// Warn if the func-generated legacy .s2i/bin/assemble exists; it will be
// uploaded to the PVC and can interfere with the in-cluster build.
// Remote deploy doesn't go through Client.Build, so we re-check here.
Expand Down
48 changes: 37 additions & 11 deletions 48 pkg/pipelines/tekton/templates.go
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ type templateData struct {
PipelinesTargetBranch string

// Static entries
RepoUrl string
RepoUrl string
// Revision is rendered with %q in the PipelineRun templates: a git ref
// may contain a quote, and a commit hash could read as a YAML number.
Revision string

// Task references
Expand Down Expand Up @@ -347,6 +349,34 @@ func createAndApplyPipelineTemplate(f fn.Function, namespace string, labels map[
return createAndApplyResource(f.Root, pipelineFileName, template, "pipeline", getPipelineName(f), namespace, data)
}

// sourceRevision returns what the cluster fetches and what it labels the
// image with. A function read from its repository has the commit it was
// read at, so the cluster fetches exactly that. Anything else is fetched by
// the revision as configured (main when empty), and labelled from the git
// working tree on disk if it has one, HEAD marked dirty when there are
// uncommitted changes, as every builder labels a local source.
func sourceRevision(f fn.Function) (fetch, label string) {
if c := f.Build.Source.Commit; c != "" {
// NewFunctionFromGit sets a full hash; the guard spares a caller
// which set a shorter one a panic.
label = c
if len(label) > 7 {
label = label[:7]
}
return c, label
}
// Only a function on disk has a working tree to label from: with no
// Root, GitCommit would search whichever repository func runs in.
if f.Root != "" {
label, _ = fn.GitCommit(f.Root)
}
fetch = f.Build.Source.Revision
if fetch == "" {
fetch = defaultPipelinesTargetBranch
}
return fetch, label
}

// createAndApplyPipelineRunTemplate creates and applies PipelineRun template for a standard on-cluster build
// all resources are created on the fly, if there's a PipelineRun defined in the project directory, it is used instead
func createAndApplyPipelineRunTemplate(f fn.Function, namespace string, labels map[string]string) error {
Expand All @@ -357,11 +387,6 @@ func createAndApplyPipelineRunTemplate(f fn.Function, namespace string, labels m
contextDir = "."
}

pipelinesTargetBranch := f.Build.Source.Revision
if pipelinesTargetBranch == "" {
pipelinesTargetBranch = defaultPipelinesTargetBranch
}

buildEnvs := []string{}
if len(f.Build.BuildEnvs) == 0 {
buildEnvs = []string{"="}
Expand Down Expand Up @@ -389,7 +414,7 @@ func createAndApplyPipelineRunTemplate(f fn.Function, namespace string, labels m
tlsVerify = "false"
}

commit, _ := fn.GitCommit(f.Root)
fetch, label := sourceRevision(f)

data := templateData{
FunctionName: f.Name,
Expand All @@ -408,10 +433,10 @@ func createAndApplyPipelineRunTemplate(f fn.Function, namespace string, labels m

S2iImageScriptsUrl: s2iImageScriptsUrl,
TlsVerify: tlsVerify,
Commit: commit,
Commit: label,

RepoUrl: f.Build.Source.URL,
Revision: pipelinesTargetBranch,
Revision: fetch,
}

var template string
Expand All @@ -431,12 +456,13 @@ func createAndApplyPipelineRunTemplate(f fn.Function, namespace string, labels m
var manifestivalClient = k8s.GetManifestivalClient

// createAndApplyResource tries to create and apply a resource to the k8s cluster from the input template and data,
// if there's the same resource already created in the project directory, it is used instead
// if there's the same resource already created in the project directory, it is used instead.
// An empty projectRoot (a function loaded from git) has no such directory.
func createAndApplyResource(projectRoot, fileName, fileTemplate, kind, resourceName, namespace string, data interface{}) error {
var source manifestival.Source

filePath := path.Join(projectRoot, resourcesDirectory, fileName)
if _, err := os.Stat(filePath); !os.IsNotExist(err) {
if _, err := os.Stat(filePath); projectRoot != "" && !os.IsNotExist(err) {
source = manifestival.Path(filePath)
} else {
tmpl, err := template.New("template").Parse(fileTemplate)
Expand Down
2 changes: 1 addition & 1 deletion 2 pkg/pipelines/tekton/templates_pack.go
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ spec:
- name: gitRepository
value: "{{.RepoUrl}}"
- name: gitRevision
value: {{.Revision}}
value: {{printf "%q" .Revision}}
- name: contextDir
value: "{{.ContextDir}}"
- name: imageName
Expand Down
2 changes: 1 addition & 1 deletion 2 pkg/pipelines/tekton/templates_s2i.go
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ spec:
- name: gitRepository
value: "{{.RepoUrl}}"
- name: gitRevision
value: {{.Revision}}
value: {{printf "%q" .Revision}}
- name: contextDir
value: "{{.ContextDir}}"
- name: imageName
Expand Down
141 changes: 141 additions & 0 deletions 141 pkg/pipelines/tekton/templates_test.go
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
"strings"
"testing"
"text/template"
"time"

gogit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/manifestival/manifestival"
"github.com/manifestival/manifestival/fake"
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
Expand Down Expand Up @@ -315,6 +318,35 @@ var testData = []struct {
},
}

// Test_createAndApplyPipelineRunTemplate_NoRoot ensures a function loaded
// from a git repository, which has no Root, yields a Pipeline and a
// PipelineRun: nothing is read from a project directory in that case.
func Test_createAndApplyPipelineRunTemplate_NoRoot(t *testing.T) {
old := manifestivalClient
defer func() { manifestivalClient = old }()
manifestivalClient = func() (manifestival.Client, error) {
return fake.New(), nil
}

f := fn.Function{
Name: "remote-fn",
Runtime: "go",
Registry: TestRegistry,
Build: fn.BuildSpec{
Builder: builders.Pack,
Source: fn.Source{URL: "https://example.com/alice/remote-fn.git", Revision: "main"},
},
}
f.Deploy.Image = "docker.io/alice/remote-fn"

if err := createAndApplyPipelineTemplate(f, "test-ns", nil); err != nil {
t.Fatal(err)
}
if err := createAndApplyPipelineRunTemplate(f, "test-ns", nil); err != nil {
t.Fatal(err)
}
}

func Test_createAndApplyPipelineRunTemplate(t *testing.T) {
for _, tt := range testData {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -346,6 +378,69 @@ func Test_createAndApplyPipelineRunTemplate(t *testing.T) {
}
}

// Test_sourceRevision ensures a function read from its repository makes the
// cluster fetch, and label the image with, the commit it was read at, while
// any other function keeps the configured revision and the local HEAD.
func Test_sourceRevision(t *testing.T) {
const hash = "0123456789abcdef0123456789abcdef01234567"

f := fn.Function{Build: fn.BuildSpec{Source: fn.Source{URL: "https://example.com/repo.git", Revision: "main", Commit: hash}}}
if fetch, label := sourceRevision(f); fetch != hash || label != hash[:7] {
t.Errorf("read from git: expected %q, %q; got %q, %q", hash, hash[:7], fetch, label)
}

f = fn.Function{Build: fn.BuildSpec{Source: fn.Source{URL: "https://example.com/repo.git", Revision: "v1"}}}
if fetch, _ := sourceRevision(f); fetch != "v1" {
t.Errorf("git without a resolved commit: expected the revision as configured, got %q", fetch)
}

f = fn.Function{Root: t.TempDir()} // no git history, no repository
if fetch, label := sourceRevision(f); fetch != "main" || label != "" {
t.Errorf("upload without history: expected \"main\", \"\"; got %q, %q", fetch, label)
}

// A commit shorter than the label must not panic.
f = fn.Function{Build: fn.BuildSpec{Source: fn.Source{URL: "https://example.com/repo.git", Commit: "abc"}}}
if fetch, label := sourceRevision(f); fetch != "abc" || label != "abc" {
t.Errorf("short commit: expected \"abc\", \"abc\"; got %q, %q", fetch, label)
}

// With no Root there is no working tree to label from, even when func
// runs inside some unrelated repository.
t.Chdir(gitRepoWithCommit(t))
f = fn.Function{Build: fn.BuildSpec{Source: fn.Source{URL: "https://example.com/repo.git", Revision: "v1"}}}
if fetch, label := sourceRevision(f); fetch != "v1" || label != "" {
t.Errorf("rootless without a commit: expected \"v1\", \"\"; got %q, %q", fetch, label)
}
}

// gitRepoWithCommit returns a new directory holding a git repository with
// one commit.
func gitRepoWithCommit(t *testing.T) string {
t.Helper()
dir := t.TempDir()
repo, err := gogit.PlainInit(dir, false)
if err != nil {
t.Fatal(err)
}
wt, err := repo.Worktree()
if err != nil {
t.Fatal(err)
}
if err = os.WriteFile(filepath.Join(dir, "README.md"), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
if _, err = wt.Add("README.md"); err != nil {
t.Fatal(err)
}
if _, err = wt.Commit("initial", &gogit.CommitOptions{
Author: &object.Signature{Name: "test", Email: "[email protected]", When: time.Now()},
}); err != nil {
t.Fatal(err)
}
return dir
}

// strictTektonDecoder returns a strict deserializer that rejects unknown fields
// in Tekton v1 resources (Pipeline, PipelineRun, etc.).
func strictTektonDecoder(t *testing.T) runtime.Decoder {
Expand Down Expand Up @@ -474,3 +569,49 @@ func TestPipelineRunTemplatesValidate(t *testing.T) {
})
}
}

// TestPipelineRunTemplatesRevision ensures the revision reaches the
// PipelineRun as the string it is, including a git ref with a quote in it
// and a commit hash or ref which YAML would otherwise read as a number or a
// boolean.
func TestPipelineRunTemplatesRevision(t *testing.T) {
revisions := []string{
`feature"quoted`,
"0123456789012345678901234567890123456789",
"1e10",
"yes",
}
templates := []struct {
name string
tmplStr string
}{
{"packRunTemplate", packRunTemplate},
{"s2iRunTemplate", s2iRunTemplate},
}
decode := strictTektonDecoder(t)
for _, tt := range templates {
for _, revision := range revisions {
t.Run(tt.name+"/"+revision, func(t *testing.T) {
data := templateData{
PipelineName: "myfunc-pipeline",
PipelineRunName: "myfunc-pipeline-run-",
RepoUrl: "https://example.com/repo",
Revision: revision,
}
obj, _, err := decode.Decode(renderTemplate(t, tt.name, tt.tmplStr, data), nil, nil)
if err != nil {
t.Fatalf("failed to decode PipelineRun: %v", err)
}
for _, p := range obj.(*tektonv1.PipelineRun).Spec.Params {
if p.Name == "gitRevision" {
if p.Value.StringVal != revision {
t.Errorf("expected gitRevision %q, got %q", revision, p.Value.StringVal)
}
return
}
}
t.Error("no gitRevision param")
})
}
}
}
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.