From 540c37edd740820d72666b8fdc3a3e48845fb316 Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Thu, 14 Jan 2021 16:54:33 +0300 Subject: [PATCH] Generate cache upload commands for docker_builder tasks too Also move upload command generation into a separate function. --- pkg/parser/task/command/cache.go | 24 ++++++++++ pkg/parser/task/dockerbuilder.go | 5 ++ pkg/parser/task/task.go | 23 ++------- .../new-docker-builder-cache-is-uploaded.json | 47 +++++++++++++++++++ .../new-docker-builder-cache-is-uploaded.yml | 4 ++ 5 files changed, 84 insertions(+), 19 deletions(-) create mode 100644 pkg/parser/testdata/via-rpc/new-docker-builder-cache-is-uploaded.json create mode 100644 pkg/parser/testdata/via-rpc/new-docker-builder-cache-is-uploaded.yml diff --git a/pkg/parser/task/command/cache.go b/pkg/parser/task/command/cache.go index 5d57c4c7..d9e2d422 100644 --- a/pkg/parser/task/command/cache.go +++ b/pkg/parser/task/command/cache.go @@ -1,6 +1,7 @@ package command import ( + "fmt" "github.com/cirruslabs/cirrus-ci-agent/api" "github.com/cirruslabs/cirrus-cli/pkg/parser/boolevator" "github.com/cirruslabs/cirrus-cli/pkg/parser/nameable" @@ -129,3 +130,26 @@ func (cache *CacheCommand) Schema() *jsschema.Schema { return modifiedSchema } + +func GenUploadCacheCmds(commands []*api.Command) (result []*api.Command) { + for _, command := range commands { + _, ok := command.Instruction.(*api.Command_CacheInstruction) + if !ok { + continue + } + + uploadCommand := &api.Command{ + Name: fmt.Sprintf("Upload '%s' cache", command.Name), + Instruction: &api.Command_UploadCacheInstruction{ + UploadCacheInstruction: &api.UploadCacheInstruction{ + CacheName: command.Name, + }, + }, + ExecutionBehaviour: command.ExecutionBehaviour, + } + + result = append(result, uploadCommand) + } + + return +} diff --git a/pkg/parser/task/dockerbuilder.go b/pkg/parser/task/dockerbuilder.go index d63dbf8a..668f0d2b 100644 --- a/pkg/parser/task/dockerbuilder.go +++ b/pkg/parser/task/dockerbuilder.go @@ -10,6 +10,7 @@ import ( "github.com/cirruslabs/cirrus-cli/pkg/parser/parseable" "github.com/cirruslabs/cirrus-cli/pkg/parser/parsererror" "github.com/cirruslabs/cirrus-cli/pkg/parser/schema" + "github.com/cirruslabs/cirrus-cli/pkg/parser/task/command" "github.com/golang/protobuf/ptypes" jsschema "github.com/lestrrat-go/jsschema" "strconv" @@ -121,6 +122,10 @@ func (dbuilder *DockerBuilder) Parse(node *node.Node) error { dbuilder.proto.Instance = anyInstance + // Since the parsing is almost done and other commands are expected, + // we can safely append cache upload commands, if applicable + dbuilder.proto.Commands = append(dbuilder.proto.Commands, command.GenUploadCacheCmds(dbuilder.proto.Commands)...) + return nil } diff --git a/pkg/parser/task/task.go b/pkg/parser/task/task.go index ca134e4d..a53f680b 100644 --- a/pkg/parser/task/task.go +++ b/pkg/parser/task/task.go @@ -11,6 +11,7 @@ import ( "github.com/cirruslabs/cirrus-cli/pkg/parser/parseable" "github.com/cirruslabs/cirrus-cli/pkg/parser/parsererror" "github.com/cirruslabs/cirrus-cli/pkg/parser/schema" + "github.com/cirruslabs/cirrus-cli/pkg/parser/task/command" "github.com/golang/protobuf/ptypes" jsschema "github.com/lestrrat-go/jsschema" "google.golang.org/protobuf/reflect/protoreflect" @@ -182,25 +183,9 @@ func (task *Task) Parse(node *node.Node) error { return fmt.Errorf("%w: task %s has no instance attached", parsererror.ErrParsing, task.Name()) } - // Generate cache upload instructions - for _, command := range task.proto.Commands { - _, ok := command.Instruction.(*api.Command_CacheInstruction) - if !ok { - continue - } - - uploadCommand := &api.Command{ - Name: fmt.Sprintf("Upload '%s' cache", command.Name), - Instruction: &api.Command_UploadCacheInstruction{ - UploadCacheInstruction: &api.UploadCacheInstruction{ - CacheName: command.Name, - }, - }, - ExecutionBehaviour: command.ExecutionBehaviour, - } - - task.proto.Commands = append(task.proto.Commands, uploadCommand) - } + // Since the parsing is almost done and other commands are expected, + // we can safely append cache upload commands, if applicable + task.proto.Commands = append(task.proto.Commands, command.GenUploadCacheCmds(task.proto.Commands)...) return nil } diff --git a/pkg/parser/testdata/via-rpc/new-docker-builder-cache-is-uploaded.json b/pkg/parser/testdata/via-rpc/new-docker-builder-cache-is-uploaded.json new file mode 100644 index 00000000..bca7235e --- /dev/null +++ b/pkg/parser/testdata/via-rpc/new-docker-builder-cache-is-uploaded.json @@ -0,0 +1,47 @@ +[ + { + "commands": [ + { + "cloneInstruction": {}, + "name": "clone" + }, + { + "cacheInstruction": { + "folder": "$HOME/.gradle/caches", + "reuploadOnChanges": true + }, + "name": "gradle" + }, + { + "name": "build", + "scriptInstruction": { + "scripts": [ + "./gradlew build" + ] + } + }, + { + "name": "Upload 'gradle' cache", + "uploadCacheInstruction": { + "cacheName": "gradle" + } + } + ], + "environment": { + "CIRRUS_OS": "linux" + }, + "instance": { + "@type": "type.googleapis.com/org.cirruslabs.ci.services.cirruscigrpc.DockerBuilder" + }, + "metadata": { + "properties": { + "allow_failures": "false", + "experimental": "false", + "indexWithinBuild": "0", + "timeout_in": "3600", + "trigger_type": "AUTOMATIC" + } + }, + "name": "main" + } +] diff --git a/pkg/parser/testdata/via-rpc/new-docker-builder-cache-is-uploaded.yml b/pkg/parser/testdata/via-rpc/new-docker-builder-cache-is-uploaded.yml new file mode 100644 index 00000000..ba2df914 --- /dev/null +++ b/pkg/parser/testdata/via-rpc/new-docker-builder-cache-is-uploaded.yml @@ -0,0 +1,4 @@ +docker_builder: + gradle_cache: + folder: ~/.gradle/caches + build_script: ./gradlew build