diff --git a/bitrise.yml b/bitrise.yml index d5a9d34..ae17c37 100644 --- a/bitrise.yml +++ b/bitrise.yml @@ -12,6 +12,11 @@ app: workflows: test: + envs: + - ENV_1: "1" + - ENV_2: "2" + - ENV_3: "3" + - ENV_4: "4" before_run: - audit-this-step steps: @@ -24,6 +29,23 @@ workflows: - workflows: $TEST_WORKFLOWS - access_token: $ACCESS_TOKEN - wait_for_builds: "true" + - environment_key_list: "ENV_1\nENV_2\n$ENV_3\n$ENV_4\n" + + test_shared_envs: + steps: + - script@1.1.5: + inputs: + - content: |- + #!/usr/bin/env bash + # fail if any commands fails + set -e + + echo "ENV_1 = $ENV_1 ENV_2 = $ENV_2 ENV_3 = $ENV_3 ENV_4 = $ENV_4" + if [[ "$ENV_1" != "1" ]] || [[ "$ENV_2" != "2" ]] || [[ "$ENV_3" != "3" ]] || [[ "$ENV_4" != "4" ]] ; then + exit 1; + else + echo "got all of the exported environments" + fi # ---------------------------------------------------------------- # --- workflows to Share this step into a Step Library diff --git a/bitrise/bitrise.go b/bitrise/bitrise.go index 8cb7520..132db69 100644 --- a/bitrise/bitrise.go +++ b/bitrise/bitrise.go @@ -42,6 +42,12 @@ type StartResponse struct { TriggeredWorkflow string `json:"triggered_workflow"` } +// Environment ... +type Environment struct { + MappedTo string `json:"mapped_to"` + Value string `json:"value"` +} + // App ... type App struct { Slug, AccessToken string @@ -78,7 +84,7 @@ func (app App) GetBuild(buildSlug string) (Build, error) { } // StartBuild ... -func (app App) StartBuild(workflow string, buildParams json.RawMessage, buildNumber string) (StartResponse, error) { +func (app App) StartBuild(workflow string, buildParams json.RawMessage, buildNumber string, environments []Environment) (StartResponse, error) { var params map[string]interface{} if err := json.Unmarshal(buildParams, ¶ms); err != nil { return StartResponse{}, err @@ -86,17 +92,13 @@ func (app App) StartBuild(workflow string, buildParams json.RawMessage, buildNum params["workflow_id"] = workflow params["skip_git_status_report"] = true - sourceBuildNumber := map[string]interface{}{ - "is_expand": true, - "mapped_to": "SOURCE_BITRISE_BUILD_NUMBER", - "value": buildNumber, + sourceBuildNumber := Environment{ + MappedTo: "SOURCE_BITRISE_BUILD_NUMBER", + Value: buildNumber, } - if envs, ok := params["environments"].([]interface{}); ok { - params["environments"] = append(envs, sourceBuildNumber) - } else { - params["environments"] = []interface{}{sourceBuildNumber} - } + envs := []Environment{sourceBuildNumber} + params["environments"] = append(envs, environments...) b, err := json.Marshal(params) if err != nil { diff --git a/main.go b/main.go index 27c50a1..f557e41 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ type Config struct { AccessToken stepconf.Secret `env:"access_token,required"` WaitForBuilds string `env:"wait_for_builds"` Workflows string `env:"workflows,required"` + Environments string `env:"environment_key_list"` } func failf(s string, a ...interface{}) { @@ -50,8 +51,9 @@ func main() { log.Infof("Starting builds:") var buildSlugs []string + environments := createEnvs(cfg.Environments) for _, wf := range strings.Split(cfg.Workflows, "\n") { - startedBuild, err := app.StartBuild(wf, build.OriginalBuildParams, cfg.BuildNumber) + startedBuild, err := app.StartBuild(wf, build.OriginalBuildParams, cfg.BuildNumber, environments) if err != nil { failf("Failed to start build, error: %s", err) } @@ -87,3 +89,22 @@ func main() { failf("An error occoured: %s", err) } } + +func createEnvs(environmentKeys string) []bitrise.Environment { + environmentKeys = strings.Replace(environmentKeys, "$", "", -1) + environmentsKeyList := strings.Split(environmentKeys, "\n") + + var environments []bitrise.Environment + for _, key := range environmentsKeyList { + if key == "" { + continue + } + + env := bitrise.Environment{ + MappedTo: key, + Value: os.Getenv(key), + } + environments = append(environments, env) + } + return environments +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..f0673c0 --- /dev/null +++ b/main_test.go @@ -0,0 +1,78 @@ +package main + +import ( + "reflect" + "testing" + + "github.com/bitrise-steplib/bitrise-step-build-router-start/bitrise" +) + +func Test_createEnvs(t *testing.T) { + tests := []struct { + name string + environmentKeys string + want []bitrise.Environment + }{ + { + name: "empty", + environmentKeys: "", + want: nil, + }, + { + name: "one env", + environmentKeys: "ENV_1", + want: []bitrise.Environment{bitrise.Environment{MappedTo: "ENV_1", Value: "1"}}, + }, + { + name: "multiple env", + environmentKeys: "ENV_1\nENV_2\nENV_3\nENV_4", + want: []bitrise.Environment{ + bitrise.Environment{ + MappedTo: "ENV_1", + Value: "1", + }, + bitrise.Environment{ + MappedTo: "ENV_2", + Value: "2", + }, + bitrise.Environment{ + MappedTo: "ENV_3", + Value: "3", + }, + bitrise.Environment{ + MappedTo: "ENV_4", + Value: "4", + }, + }, + }, + { + name: "multiple env with $", + environmentKeys: "ENV_1\n$ENV_2\nENV_3\n$ENV_4", + want: []bitrise.Environment{ + bitrise.Environment{ + MappedTo: "ENV_1", + Value: "1", + }, + bitrise.Environment{ + MappedTo: "ENV_2", + Value: "2", + }, + bitrise.Environment{ + MappedTo: "ENV_3", + Value: "3", + }, + bitrise.Environment{ + MappedTo: "ENV_4", + Value: "4", + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := createEnvs(tt.environmentKeys); !reflect.DeepEqual(got, tt.want) { + t.Errorf("createEnvs() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/step.yml b/step.yml index 826320c..1f19da6 100644 --- a/step.yml +++ b/step.yml @@ -34,6 +34,20 @@ inputs: summary: The workflow(s) to start. One workflow per line. description: The workflow(s) to start. One workflow per line. is_required: true + - environment_key_list: + opts: + title: Environments to share + summary: The keys of the envs which will be shared with the triggered workflows. + description: |- + The keys of the envs which will be shared with the triggered workflows + + + **FORMAT** Seperate the keys `\n` character. E.g: + `ENV_1 + ENV_2 + ENV_3` + is_expand: false + is_required: false - wait_for_builds: "false" opts: title: Wait for builds