diff --git a/cmd/vela-worker/exec.go b/cmd/vela-worker/exec.go index 2f9e99c6..e04878ea 100644 --- a/cmd/vela-worker/exec.go +++ b/cmd/vela-worker/exec.go @@ -10,10 +10,11 @@ import ( "sync" "time" + "github.com/go-vela/types" + "github.com/go-vela/types/constants" "github.com/go-vela/worker/executor" "github.com/go-vela/worker/runtime" "github.com/go-vela/worker/version" - "github.com/sirupsen/logrus" ) @@ -70,6 +71,27 @@ func (w *Worker) exec(index int) error { "version": v.Semantic(), }) + // handle stale item queued before a Vela upgrade or downgrade. + if item.ItemVersion != types.ItemVersion { + // If the ItemVersion is older or newer than what we expect, then it might + // not be safe to process the build. Fail the build and loop to the next item. + // TODO: Ask the server to re-compile and requeue the build instead of failing it. + logrus.Errorf("Failing stale queued build due to wrong item version: want %d, got %d", types.ItemVersion, item.ItemVersion) + + build := item.Build + build.SetError("Unable to process stale build (queued before Vela upgrade/downgrade).") + build.SetStatus(constants.StatusError) + build.SetFinished(time.Now().UTC().Unix()) + + _, _, err := w.VelaClient.Build.Update(item.Repo.GetOrg(), item.Repo.GetName(), build) + if err != nil { + logrus.Errorf("Unable to set build status to %s: %s", constants.StatusFailure, err) + return err + } + + return nil + } + // setup the runtime // // https://pkg.go.dev/github.com/go-vela/worker/runtime?tab=doc#New diff --git a/go.mod b/go.mod index 5cd9d6ff..5403cd7f 100644 --- a/go.mod +++ b/go.mod @@ -8,9 +8,9 @@ require ( github.com/docker/docker v20.10.24+incompatible github.com/docker/go-units v0.5.0 github.com/gin-gonic/gin v1.9.0 - github.com/go-vela/sdk-go v0.19.2 - github.com/go-vela/server v0.19.3-0.20230510141710-f99a894333d4 - github.com/go-vela/types v0.19.2 + github.com/go-vela/sdk-go v0.19.3-0.20230525152744-4c6fc715258c + github.com/go-vela/server v0.19.3-0.20230523191014-5a9a92d279e0 + github.com/go-vela/types v0.19.3-0.20230523200921-35a0d5fc088c github.com/golang-jwt/jwt/v5 v5.0.0 github.com/google/go-cmp v0.5.9 github.com/joho/godotenv v1.5.1 diff --git a/go.sum b/go.sum index 50498b1c..95e90381 100644 --- a/go.sum +++ b/go.sum @@ -143,12 +143,12 @@ github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91 github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU= github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I= -github.com/go-vela/sdk-go v0.19.2 h1:bEMnEnEZxI27ZCjFx5fNRFc0EYHr2NsR2jb//0J5SJk= -github.com/go-vela/sdk-go v0.19.2/go.mod h1:p0WwKyBLslyhPjSQnA78SVZpLDkOL/P030BtzdRDjtE= -github.com/go-vela/server v0.19.3-0.20230510141710-f99a894333d4 h1:hAj76WkNt+CZfkionu86xlpM3YpupZkX1OWHzWbeEpA= -github.com/go-vela/server v0.19.3-0.20230510141710-f99a894333d4/go.mod h1:N6ej04/c6kc/0sJK15jCLr3Au9L9Zs9TjcMtkJoyJx8= -github.com/go-vela/types v0.19.2 h1:xU61CX2jdMuBCtLOg8a7Z2aEWYM1zZt37Ygx1oHGbjM= -github.com/go-vela/types v0.19.2/go.mod h1:ZvDjYCKU36yJS3sLxPLCny/HLF1U6YtlOienzv/cXB4= +github.com/go-vela/sdk-go v0.19.3-0.20230525152744-4c6fc715258c h1:2GV5t+AOyMYIoyFvzOFCT1dt08KROzaUozK1oW6Q4p0= +github.com/go-vela/sdk-go v0.19.3-0.20230525152744-4c6fc715258c/go.mod h1:0XTE3DHG5B872POEhKJ3kZzL8ZvR1jAJSeakeJgjfCM= +github.com/go-vela/server v0.19.3-0.20230523191014-5a9a92d279e0 h1:jyNJ1YdYj0YBRcwzyab8yZWBjGt543ATp/eftgPZBoA= +github.com/go-vela/server v0.19.3-0.20230523191014-5a9a92d279e0/go.mod h1:sjfqYOyuXG7uqgeQF03URz7GGNjVE+JNIhkGK00eT9o= +github.com/go-vela/types v0.19.3-0.20230523200921-35a0d5fc088c h1:eAApIK5e5MxFF8RzZAFsvTSdwq/AzdUrdhJHOGQ0ILc= +github.com/go-vela/types v0.19.3-0.20230523200921-35a0d5fc088c/go.mod h1:0lsuPfGyVyTWJSi2h3NS6uaEW6DgnFvIzaZu1sXYKrs= github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA= github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= diff --git a/internal/step/skip.go b/internal/step/skip.go index a341ae0f..4ee0e3e1 100644 --- a/internal/step/skip.go +++ b/internal/step/skip.go @@ -57,6 +57,12 @@ func Skip(c *pipeline.Container, b *library.Build, r *library.Repo) bool { ruledata.Target = b.GetDeploy() } + // check if the build event is schedule + if strings.EqualFold(b.GetEvent(), constants.EventSchedule) { + // add schedule target information to ruledata + ruledata.Target = b.GetDeploy() + } + // return the inverse of container execute // // https://pkg.go.dev/github.com/go-vela/types/pipeline#Container.Execute diff --git a/internal/step/skip_test.go b/internal/step/skip_test.go index 829bbd5c..16a023c6 100644 --- a/internal/step/skip_test.go +++ b/internal/step/skip_test.go @@ -126,6 +126,34 @@ func TestStep_Skip(t *testing.T) { Distribution: vela.String("linux"), } + _schedule := &library.Build{ + ID: vela.Int64(1), + Number: vela.Int(1), + Parent: vela.Int(1), + Event: vela.String("schedule"), + EventAction: vela.String(""), + Status: vela.String("success"), + Error: vela.String(""), + Enqueued: vela.Int64(1563474077), + Created: vela.Int64(1563474076), + Started: vela.Int64(1563474077), + Finished: vela.Int64(0), + Deploy: vela.String(""), + Clone: vela.String("https://github.com/github/octocat.git"), + Source: vela.String("https://github.com/github/octocat/abcdefghi123456789"), + Title: vela.String("push received from https://github.com/github/octocat"), + Message: vela.String("First commit..."), + Commit: vela.String("48afb5bdc41ad69bf22588491333f7cf71135163"), + Sender: vela.String("OctoKitty"), + Author: vela.String("OctoKitty"), + Branch: vela.String("master"), + Ref: vela.String("refs/heads/master"), + BaseRef: vela.String(""), + Host: vela.String("example.company.com"), + Runtime: vela.String("docker"), + Distribution: vela.String("linux"), + } + _tag := &library.Build{ ID: vela.Int64(1), Number: vela.Int(1), @@ -218,6 +246,13 @@ func TestStep_Skip(t *testing.T) { repo: _repo, want: false, }, + { + name: "schedule", + build: _schedule, + container: _container, + repo: _repo, + want: false, + }, { name: "tag", build: _tag,