Skip to content

Commit c62571a

Browse files
kdobmayergodrei
authored andcommitted
Implement set-env-var step (#1)
* Implement set-env-var step * Fix error message for missing value * Update title, fix is_required fields
1 parent e2a3acc commit c62571a

93 files changed

Lines changed: 7972 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.bitrise*
2+
.gows.user.yml

Gopkg.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[[constraint]]
2+
name = "github.com/bitrise-io/go-utils"
3+
branch = "master"

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 bitrise-steplib
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,93 @@
1-
# steps-set-env-var
1+
# set-env-var
2+
3+
Set environment variable
4+
5+
6+
## How to use this Step
7+
8+
Can be run directly with the [bitrise CLI](https://github.com/bitrise-io/bitrise),
9+
just `git clone` this repository, `cd` into it's folder in your Terminal/Command Line
10+
and call `bitrise run test`.
11+
12+
*Check the `bitrise.yml` file for required inputs which have to be
13+
added to your `.bitrise.secrets.yml` file!*
14+
15+
Step by step:
16+
17+
1. Open up your Terminal / Command Line
18+
2. `git clone` the repository
19+
3. `cd` into the directory of the step (the one you just `git clone`d)
20+
5. Create a `.bitrise.secrets.yml` file in the same directory of `bitrise.yml`
21+
(the `.bitrise.secrets.yml` is a git ignored file, you can store your secrets in it)
22+
6. Check the `bitrise.yml` file for any secret you should set in `.bitrise.secrets.yml`
23+
* Best practice is to mark these options with something like `# define these in your .bitrise.secrets.yml`, in the `app:envs` section.
24+
7. Once you have all the required secret parameters in your `.bitrise.secrets.yml` you can just run this step with the [bitrise CLI](https://github.com/bitrise-io/bitrise): `bitrise run test`
25+
26+
An example `.bitrise.secrets.yml` file:
27+
28+
```
29+
envs:
30+
- A_SECRET_PARAM_ONE: the value for secret one
31+
- A_SECRET_PARAM_TWO: the value for secret two
32+
```
33+
34+
## How to create your own step
35+
36+
1. Create a new git repository for your step (**don't fork** the *step template*, create a *new* repository)
37+
2. Copy the [step template](https://github.com/bitrise-steplib/step-template) files into your repository
38+
3. Fill the `step.sh` with your functionality
39+
4. Wire out your inputs to `step.yml` (`inputs` section)
40+
5. Fill out the other parts of the `step.yml` too
41+
6. Provide test values for the inputs in the `bitrise.yml`
42+
7. Run your step with `bitrise run test` - if it works, you're ready
43+
44+
__For Step development guidelines & best practices__ check this documentation: [https://github.com/bitrise-io/bitrise/blob/master/_docs/step-development-guideline.md](https://github.com/bitrise-io/bitrise/blob/master/_docs/step-development-guideline.md).
45+
46+
**NOTE:**
47+
48+
If you want to use your step in your project's `bitrise.yml`:
49+
50+
1. git push the step into it's repository
51+
2. reference it in your `bitrise.yml` with the `git::PUBLIC-GIT-CLONE-URL@BRANCH` step reference style:
52+
53+
```
54+
- git::https://github.com/user/my-step.git@branch:
55+
title: My step
56+
inputs:
57+
- my_input_1: "my value 1"
58+
- my_input_2: "my value 2"
59+
```
60+
61+
You can find more examples of step reference styles
62+
in the [bitrise CLI repository](https://github.com/bitrise-io/bitrise/blob/master/_examples/tutorials/steps-and-workflows/bitrise.yml#L65).
63+
64+
## How to contribute to this Step
65+
66+
1. Fork this repository
67+
2. `git clone` it
68+
3. Create a branch you'll work on
69+
4. To use/test the step just follow the **How to use this Step** section
70+
5. Do the changes you want to
71+
6. Run/test the step before sending your contribution
72+
* You can also test the step in your `bitrise` project, either on your Mac or on [bitrise.io](https://www.bitrise.io)
73+
* You just have to replace the step ID in your project's `bitrise.yml` with either a relative path, or with a git URL format
74+
* (relative) path format: instead of `- original-step-id:` use `- path::./relative/path/of/script/on/your/Mac:`
75+
* direct git URL format: instead of `- original-step-id:` use `- git::https://github.com/user/step.git@branch:`
76+
* You can find more example of alternative step referencing at: https://github.com/bitrise-io/bitrise/blob/master/_examples/tutorials/steps-and-workflows/bitrise.yml
77+
7. Once you're done just commit your changes & create a Pull Request
78+
79+
80+
## Share your own Step
81+
82+
You can share your Step or step version with the [bitrise CLI](https://github.com/bitrise-io/bitrise). If you use the `bitrise.yml` included in this repository, all you have to do is:
83+
84+
1. In your Terminal / Command Line `cd` into this directory (where the `bitrise.yml` of the step is located)
85+
1. Run: `bitrise run test` to test the step
86+
1. Run: `bitrise run audit-this-step` to audit the `step.yml`
87+
1. Check the `share-this-step` workflow in the `bitrise.yml`, and fill out the
88+
`envs` if you haven't done so already (don't forget to bump the version number if this is an update
89+
of your step!)
90+
1. Then run: `bitrise run share-this-step` to share the step (version) you specified in the `envs`
91+
1. Send the Pull Request, as described in the logs of `bitrise run share-this-step`
92+
93+
That's all ;)

bitrise.yml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
format_version: 4
2+
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
3+
4+
app:
5+
envs:
6+
- BITRISE_STEP_ID: set-env-var
7+
- BITRISE_STEP_VERSION: "0.9.0"
8+
- BITRISE_STEP_GIT_CLONE_URL: https://github.com/bitrise-steplib/steps-set-env-var.git
9+
- MY_STEPLIB_REPO_FORK_GIT_URL: $MY_STEPLIB_REPO_FORK_GIT_URL
10+
- VALUE: source-env-var-value
11+
12+
workflows:
13+
test:
14+
steps:
15+
- change-workdir:
16+
title: Switch working dir to test / _tmp dir
17+
description: |-
18+
To prevent step testing issues, like referencing relative
19+
files with just './some-file' in the step's code, which would
20+
work for testing the step from this directory directly
21+
but would break if the step is included in another `bitrise.yml`.
22+
run_if: true
23+
inputs:
24+
- path: ./_tmp
25+
- is_create_path: true
26+
- path::./:
27+
title: Step Test
28+
run_if: true
29+
inputs:
30+
- value: ${VALUE}
31+
- destination_keys: DEST_ENV_VAR_1 DEST_ENV_VAR_2 DEST_ENV_VAR_3
32+
- script:
33+
inputs:
34+
- content: |-
35+
#!/usr/bin/env bash
36+
set -u
37+
echo "DEST_ENV_VAR_1: $DEST_ENV_VAR_1"
38+
echo "DEST_ENV_VAR_2: $DEST_ENV_VAR_2"
39+
echo "DEST_ENV_VAR_3: $DEST_ENV_VAR_3"
40+
# ----------------------------------------------------------------
41+
# --- workflows to Share this step into a Step Library
42+
audit-this-step:
43+
steps:
44+
- script:
45+
inputs:
46+
- content: |-
47+
#!/bin/bash
48+
set -ex
49+
stepman audit --step-yml ./step.yml
50+
51+
share-this-step:
52+
envs:
53+
# if you want to share this step into a StepLib
54+
- MY_STEPLIB_REPO_FORK_GIT_URL: $MY_STEPLIB_REPO_FORK_GIT_URL
55+
- BITRISE_STEP_ID: $BITRISE_STEP_ID
56+
- BITRISE_STEP_VERSION: $BITRISE_STEP_VERSION
57+
- BITRISE_STEP_GIT_CLONE_URL: $BITRISE_STEP_GIT_CLONE_URL
58+
description: |-
59+
If this is the first time you try to share a Step you should
60+
first call: $ bitrise share
61+
62+
This will print you a guide, and information about how Step sharing
63+
works. Please read it at least once!
64+
65+
As noted in the Step sharing guide you'll have to fork the
66+
StepLib you want to share this step into. Once you're done with forking
67+
the repository you should set your own fork's git clone URL
68+
in the `.bitrise.secrets.yml` file, or here in the `envs` section,
69+
as the value of the `MY_STEPLIB_REPO_FORK_GIT_URL` environment.
70+
71+
You're now ready to share this Step, just make sure that
72+
the `BITRISE_STEP_ID` and `BITRISE_STEP_VERSION`
73+
environments are set to the desired values!
74+
75+
To share this Step into a StepLib you can just run: $ bitrise run share-this-step
76+
77+
Once it finishes the only thing left is to actually create a Pull Request,
78+
the way described in the guide printed at the end of the process.
79+
before_run:
80+
- audit-this-step
81+
steps:
82+
- script:
83+
inputs:
84+
- content: |-
85+
#!/bin/bash
86+
set -ex
87+
bitrise share start -c "${MY_STEPLIB_REPO_FORK_GIT_URL}"
88+
bitrise share create --stepid "${BITRISE_STEP_ID}" --tag "${BITRISE_STEP_VERSION}" --git "${BITRISE_STEP_GIT_CLONE_URL}"
89+
bitrise share finish

main.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
"os/exec"
8+
"strings"
9+
10+
"github.com/bitrise-io/go-utils/log"
11+
)
12+
13+
// ConfigsModel ...
14+
type ConfigsModel struct {
15+
Value string
16+
DestinationKeys string
17+
}
18+
19+
func createConfigsModelFromEnvs() ConfigsModel {
20+
return ConfigsModel{
21+
Value: os.Getenv("value"),
22+
DestinationKeys: os.Getenv("destination_keys"),
23+
}
24+
}
25+
26+
func (configs ConfigsModel) print() {
27+
log.Infof("Configs:")
28+
log.Printf("- Value: %s", configs.Value)
29+
log.Printf("- DestinationKeys: %s", configs.DestinationKeys)
30+
}
31+
32+
func (configs ConfigsModel) validate() error {
33+
if configs.DestinationKeys == "" {
34+
return errors.New("no DestinationKeys parameter specified")
35+
}
36+
return nil
37+
}
38+
39+
func main() {
40+
configs := createConfigsModelFromEnvs()
41+
42+
fmt.Println()
43+
configs.print()
44+
fmt.Println()
45+
46+
if err := configs.validate(); err != nil {
47+
log.Errorf("Issue with input: %s", err)
48+
os.Exit(1)
49+
}
50+
51+
if configs.Value == "" {
52+
log.Warnf("The value is empty")
53+
fmt.Println()
54+
}
55+
56+
log.Infof("Exported environment variables:")
57+
for _, dest := range strings.Split(configs.DestinationKeys, " ") {
58+
cmdLog, err := exec.Command("bitrise", "envman", "add", "--key", dest, "--value", configs.Value).CombinedOutput()
59+
if err != nil {
60+
fmt.Printf("Failed to expose output with envman, error: %#v | output: %s", err, cmdLog)
61+
os.Exit(1)
62+
}
63+
fmt.Printf("- %s: %s\n", dest, configs.Value)
64+
}
65+
}

step.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
title: |-
2+
Set environment variable
3+
summary: |
4+
Set environment variable
5+
description: |
6+
With this step you can copy a value into as many environment variables as you like.
7+
website: https://github.com/bitrise-steplib/steps-set-env-var
8+
source_code_url: https://github.com/bitrise-steplib/steps-set-env-var
9+
support_url: https://github.com/bitrise-steplib/steps-set-env-var/issues
10+
host_os_tags:
11+
- osx-10.10
12+
- ubuntu
13+
type_tags:
14+
- utility
15+
is_requires_admin_user: false
16+
is_always_run: false
17+
is_skippable: false
18+
run_if: ""
19+
20+
toolkit:
21+
go:
22+
package_name: github.com/bitrise-steplib/steps-set-env-var
23+
24+
inputs:
25+
- value:
26+
opts:
27+
title: "Value for copy into Envirhnment variables"
28+
summary: "Value for copy into Environment variables"
29+
description: |
30+
The value will be copied into the destination environment variables.
31+
is_expand: true
32+
is_required: false
33+
value_options: []
34+
- destination_keys:
35+
opts:
36+
title: "Destination Environment variables"
37+
summary: "Destination Environment variables"
38+
description: |
39+
Space separated list of the keys of the destination environment variables.
40+
The value will be copied into these environment variables.
41+
42+
`envman add --key [destination_key] --value [value]`
43+
is_expand: true
44+
is_required: true
45+
value_options: []

vendor/github.com/bitrise-io/go-utils/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/bitrise-io/go-utils/LICENSE

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)