Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Add support for git commit sha #587

Merged
merged 2 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions cmd/git-init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ func run(logger *zap.SugaredLogger, cmd string, args ...string) {
}
}

func runOrFail(logger *zap.SugaredLogger, cmd string, args ...string) {
func runOrFail(logger *zap.SugaredLogger, cmd string, args ...string) error {
c := exec.Command(cmd, args...)
var output bytes.Buffer
c.Stderr = &output
c.Stdout = &output

if err := c.Run(); err != nil {
logger.Fatalf("Unexpected error running %v %v: %v\n%v", cmd, args, err, output.String())
logger.Errorf("Unexpected error running %v %v: %v\n%v", cmd, args, err, output.String())
return err
}
return nil
}

func main() {
Expand Down Expand Up @@ -86,8 +88,15 @@ func main() {
}

run(logger, "git", "remote", "add", "origin", *url)
runOrFail(logger, "git", "fetch", "--depth=1", "--recurse-submodules=yes", "origin", *revision)
runOrFail(logger, "git", "reset", "--hard", "FETCH_HEAD")
err = runOrFail(logger, "git", "fetch", "--depth=1", "--recurse-submodules=yes", "origin", *revision)
if err != nil {
// Fetch can fail if an old commitid was used so try git pull, performing regardless of error
// as no guarantee that the same error is returned by all git servers gitlab, github etc...
run(logger, "git", "pull", "--recurse-submodules=yes", "origin")
runOrFail(logger, "git", "checkout", *revision)
} else {
runOrFail(logger, "git", "reset", "--hard", "FETCH_HEAD")
}

logger.Infof("Successfully cloned %q @ %q in path %q", *url, *revision, dir)
}
17 changes: 16 additions & 1 deletion test/e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
corev1 "k8s.io/api/core/v1"
kuberrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -108,6 +109,20 @@ func createTestNamespace(t *testing.T) (string, *clients) {
return buildTestNamespace, clients
}

func verifyDefaultServiceAccountCreation(namespace string, t *testing.T, c *clients) {
defaultSA := "default"
t.Logf("Verify SA %q is created in namespace %q", defaultSA, namespace)
if err := wait.PollImmediate(interval, timeout, func() (bool, error) {
_, err := c.kubeClient.Kube.CoreV1().ServiceAccounts(namespace).Get(defaultSA, metav1.GetOptions{})
if err != nil && kuberrors.IsNotFound(err) {
return false, nil
}
return true, err
}); err != nil {
t.Fatalf("Failed to get SA %q in namespace %q for tests: %s", defaultSA, namespace, err)
}
}

func newClients(configPath string, clusterName string, namespace string) (*clients, error) {
overrides := clientcmd.ConfigOverrides{}
// Override the cluster name if provided.
Expand Down Expand Up @@ -204,7 +219,7 @@ func initialize(t *testing.T) (string, *clients) {
}

buildTestNamespace, clients := createTestNamespace(t)

verifyDefaultServiceAccountCreation(buildTestNamespace, t, clients)
// Cleanup namespace
test.CleanupOnInterrupt(func() { teardownNamespace(t, clients, buildTestNamespace) }, t.Logf)

Expand Down
26 changes: 26 additions & 0 deletions test/git-source/1-commitid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2019 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: build.knative.dev/v1alpha1
kind: Build
metadata:
name: test-git-commitid
labels:
expect: succeeded
spec:
source:
git:
url: https://github.com/bazelbuild/rules_docker
revision: 4025a58c156a4ba0fd3606df50051423043495bf
template:
name: dump-workspace-template