Skip to content

Commit

Permalink
Generate cache upload commands for docker_builder tasks too (#231)
Browse files Browse the repository at this point in the history
Also move upload command generation into a separate function.
  • Loading branch information
edigaryev authored Jan 14, 2021
1 parent cf0973e commit 75b0df0
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 19 deletions.
24 changes: 24 additions & 0 deletions pkg/parser/task/command/cache.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions pkg/parser/task/dockerbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
23 changes: 4 additions & 19 deletions pkg/parser/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
docker_builder:
gradle_cache:
folder: ~/.gradle/caches
build_script: ./gradlew build

0 comments on commit 75b0df0

Please sign in to comment.