Skip to content

Commit

Permalink
Share envs between the triggered workflows and the parent build (#6)
Browse files Browse the repository at this point in the history
* feat: add an option to share the provided envs with the triggered workflows

- step.yml: add the environment_key_list input. With that you can specify which envs do you want to share with the triggered worklflows.
- bitrise/bitrise.go: add the Environment struct.
- test added to test the environment_key_list input parsing

* test: test fix

* sourceBuildNumber param converted to the new Environment struct

* test: add test_shared_envs workfows to the bitrise.yml

* fix: chage the break command to continue in the loop which creates the shared envs for the request

* docs: remove the note from the `environment_key_list` input in the step.yml

* fix: typo fix
  • Loading branch information
BirmacherAkos authored Sep 12, 2018
1 parent 55c0415 commit 4b4f74b
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 11 deletions.
22 changes: 22 additions & 0 deletions bitrise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
- [email protected]:
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
Expand Down
22 changes: 12 additions & 10 deletions bitrise/bitrise.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -78,25 +84,21 @@ 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, &params); err != nil {
return StartResponse{}, err
}
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 {
Expand Down
23 changes: 22 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
78 changes: 78 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
14 changes: 14 additions & 0 deletions step.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4b4f74b

Please sign in to comment.