-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 A Cloud Guru | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,36 @@ | ||
# metadata-env-buildkite-plugin | ||
|
||
Buildkite plugin to inject buildkite-agent metadata into environment | ||
|
||
This is particularly useful as **block step** values are saved in a build's **meta-data**. | ||
|
||
https://buildkite.com/docs/pipelines/block-step | ||
|
||
|
||
## Example | ||
|
||
```yml | ||
steps: | ||
- block: "Request Release" | ||
fields: | ||
- select: "Select account" | ||
key: ROLE # saves to buildkite-agent meta-data | ||
options: | ||
- label: "Production" | ||
value: "arn:aws:iam::123456789:role/production-role" | ||
- label: "Staging" | ||
value: "arn:aws:iam::987654321:role/staging-role" | ||
- select: "Select runtime environment" | ||
key: node-env # saves to buildkite-agent meta-data | ||
options: | ||
- label: "Production" | ||
value: "production" | ||
- label: "Development" | ||
value: "development" | ||
- command: echo \$NODE_ENV | ||
plugins: | ||
ACloudGuru/metadata-env: | ||
keys: | ||
- ROLE | ||
- node-env=NODE_ENV #remaps node-env key to NODE_ENV <key>=<alias> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
version: '3' | ||
services: | ||
tests: | ||
image: buildkite/plugin-tester | ||
volumes: | ||
- .:/plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/bin/bash | ||
|
||
set -euo pipefail | ||
|
||
main() { | ||
echo "Reading meta-data ..." | ||
env_list=$(plugin_read_list | evaluate_key_item) | ||
|
||
echo "Exporting ${#env_list} bytes of env" | ||
set -a | ||
eval "$env_list" | ||
set +a | ||
} | ||
|
||
function plugin_read_list() { | ||
local prefix="BUILDKITE_PLUGIN_METADATA_ENV_KEYS" | ||
local parameter="${prefix}_0" | ||
|
||
if [[ -n "${!parameter:-}" ]]; then | ||
local i=0 | ||
local parameter="${prefix}_${i}" | ||
while [[ -n "${!parameter:-}" ]]; do | ||
echo "${!parameter}" | ||
i=$((i+1)) | ||
parameter="${prefix}_${i}" | ||
done | ||
fi | ||
} | ||
|
||
function evaluate_key_item() { | ||
while IFS=$'\n' read -r key_item ; do | ||
read_meta_data "$key_item" | ||
done | ||
} | ||
|
||
function read_meta_data() { | ||
local key_item="$1" | ||
local value | ||
local meta_data_key | ||
local meta_data_key_alias | ||
local mapped_key | ||
|
||
IFS='=' read -ra key_mapped <<< "$key_item" | ||
|
||
meta_data_key=${key_mapped[0]:-} | ||
meta_data_key_alias=${key_mapped[1]:-} | ||
mapped_key=${meta_data_key_alias:-$meta_data_key} | ||
|
||
value=$(buildkite-agent meta-data get "$meta_data_key") | ||
|
||
if [[ -n "$value" ]]; then | ||
echo "${mapped_key}=(${#value} chars)" >&2; | ||
echo "${mapped_key}=${value}" | ||
else | ||
echo "Could not read meta-data: ${meta_data_key}" >&2; | ||
exit 1 | ||
fi | ||
} | ||
|
||
main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "metadata-env", | ||
"description": "Read metadata values and place into environment", | ||
"author": "@ACloudGuru", | ||
"public": true, | ||
"requirements": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env bats | ||
|
||
load '/usr/local/lib/bats/load.bash' | ||
|
||
@test "Exports metadata to env" { | ||
export BUILDKITE_PLUGIN_METADATA_ENV_KEYS_0="FOO" | ||
export BUILDKITE_PLUGIN_METADATA_ENV_KEYS_1="BAR" | ||
|
||
stub buildkite-agent \ | ||
"meta-data get FOO : echo BAR" \ | ||
"meta-data get BAR : echo TENDER" | ||
|
||
run $PWD/hooks/environment | ||
|
||
assert_success | ||
assert_output --partial "FOO=(3 chars)" | ||
assert_output --partial "BAR=(6 chars)" | ||
} | ||
|
||
@test "Exports remapped meta-data to env" { | ||
export BUILDKITE_PLUGIN_METADATA_ENV_KEYS_0="foo-meta=FOO_ENV" | ||
export BUILDKITE_PLUGIN_METADATA_ENV_KEYS_1="BAR" | ||
|
||
stub buildkite-agent \ | ||
"meta-data get foo-meta : echo FIRST" \ | ||
"meta-data get BAR : echo SECOND" \ | ||
|
||
run $PWD/hooks/environment | ||
|
||
assert_success | ||
assert_output --partial "FOO_ENV=(5 chars)" | ||
assert_output --partial "BAR=(6 chars)" | ||
} |