Commit 0c21d4a7 authored by Kamil Trzciński's avatar Kamil Trzciński

Allow to define a name of uploaded artifacts archive and a list of dependent builds

parent fa9de637
Pipeline #774435 passed with stage
package helpers
import (
"errors"
"io"
"os"
"path"
"time"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"errors"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers/archives"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers/formatter"
......@@ -20,6 +21,8 @@ type ArtifactsUploaderCommand struct {
fileArchiver
retryHelper
network common.Network
Name string `long:"name" description:"The name of the archive"`
}
func (c *ArtifactsUploaderCommand) createAndUpload() (bool, error) {
......@@ -32,8 +35,10 @@ func (c *ArtifactsUploaderCommand) createAndUpload() (bool, error) {
pw.CloseWithError(err)
}()
artifactsName := path.Base(c.Name) + ".zip"
// Upload the data
switch c.network.UploadRawArtifacts(c.BuildCredentials, pr, "artifacts.zip") {
switch c.network.UploadRawArtifacts(c.BuildCredentials, pr, artifactsName) {
case common.UploadSucceeded:
return false, nil
case common.UploadForbidden:
......@@ -77,5 +82,6 @@ func init() {
Retry: 2,
RetryTime: time.Second,
},
Name: "artifacts",
})
}
......@@ -95,9 +95,9 @@ func (b *Build) CacheKeyForRef(ref string) string {
cacheKey := path.Join(b.Name, ref)
// Get cache:key
if hash, ok := b.Options["cache"].(map[string]interface{}); ok {
if key, ok := hash["key"].(string); ok && key != "" {
cacheKey = key
if key, ok := helpers.GetMapKey(b.Options, "cache", "key"); ok {
if keyValue, ok := key.(string); ok && key != "" {
cacheKey = keyValue
}
}
......
......@@ -48,3 +48,27 @@ func ToConfigMap(list interface{}) (map[string]interface{}, bool) {
return result, true
}
func GetMapKey(value interface{}, keys ...string) (interface{}, bool) {
for _, key := range keys {
stringMap, ok := value.(map[string]interface{})
if ok {
value, ok = stringMap[key]
if !ok {
return nil, false
}
continue
}
interfaceMap, ok := value.(map[interface{}]interface{})
if ok {
value, ok = interfaceMap[key]
if !ok {
return nil, false
}
continue
}
}
return value, true
}
package helpers
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v1"
"reflect"
"testing"
......@@ -75,3 +77,35 @@ test:
t.Error("Result ", cacheConfig, " was not equal to ", expectedCacheConfig)
}
}
func TestGetMapKey(t *testing.T) {
data := `
test:
script:
- make test
cache:
untracked: true
paths:
- vendor/
- foo
`
config1 := make(map[string]interface{})
config2 := make(map[interface{}]interface{})
require.NoError(t, yaml.Unmarshal([]byte(data), config1))
require.NoError(t, yaml.Unmarshal([]byte(data), config2))
value, ok := GetMapKey(config1, "test", "cache", "untracked")
assert.True(t, ok)
assert.Equal(t, true, value)
value, ok = GetMapKey(config2, "test", "cache", "untracked")
assert.True(t, ok)
assert.Equal(t, true, value)
_, ok = GetMapKey(config1, "test", "undefined", "untracked")
assert.False(t, ok)
_, ok = GetMapKey(config2, "test", "undefined", "untracked")
assert.False(t, ok)
}
......@@ -153,6 +153,21 @@ func (b *AbstractShell) downloadArtifacts(w ShellWriter, build *common.BuildInfo
w.Command(info.RunnerCommand, args...)
}
func (b *AbstractShell) isDependentBuild(build *common.Build, name string) bool {
dependencies, ok := build.Options["dependencies"].([]interface{})
if !ok {
// If no dependencies are defined we assume that we depend on all builds
return true
}
for _, dependency := range dependencies {
if value, ok := dependency.(string); ok && name == value {
return true
}
}
return false
}
func (b *AbstractShell) GeneratePreBuild(w ShellWriter, info common.ShellScriptInfo) {
b.writeExports(w, info)
......@@ -181,6 +196,9 @@ func (b *AbstractShell) GeneratePreBuild(w ShellWriter, info common.ShellScriptI
if otherBuild.Artifacts == nil || otherBuild.Artifacts.Filename == "" {
continue
}
if !b.isDependentBuild(info.Build, otherBuild.Name) {
continue
}
b.downloadArtifacts(w, &otherBuild, info)
}
}
......@@ -255,6 +273,13 @@ func (b *AbstractShell) uploadArtifacts(w ShellWriter, list interface{}, info co
}
args = append(args, archiverArgs...)
// Get artifacts:name
if name, ok := helpers.GetMapKey(info.Build.Options["artifacts"], "name"); ok {
if nameValue, ok := name.(string); ok && nameValue != "" {
args = append(args, "--name", nameValue)
}
}
w.Notice("Uploading artifacts...")
w.Command(info.RunnerCommand, args...)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment