Skip to content

Commit

Permalink
Merge pull request #6 from logchange/5-system-env-variables
Browse files Browse the repository at this point in the history
Allow using system environment variables creating valhalla.yml
  • Loading branch information
marwin1991 authored Mar 23, 2024
2 parents 666972f + 353716b commit b0fb6d2
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 9 deletions.
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
FROM alpine:3

# Labels.
LABEL org.opencontainers.image.authors='[email protected]' \
org.opencontainers.image.url='https://github.com/logchange/valhalla' \
org.opencontainers.image.documentation='https://github.com/logchange/valhalla/blob/master/README.md' \
org.opencontainers.image.source='https://github.com/logchange/valhalla' \
org.opencontainers.image.vendor='The logchange Community' \
org.opencontainers.image.licenses='Apache-2.0'

RUN apk --update --no-cache add \
bash \
git \
Expand Down
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ commit_before_release: # define actions which have to happen before release and
username: Test1234 # git config username
email: [email protected] # git config email
msg: Releasing version {VERSION} # commit message, you can use string predefined variables
before: # list of bash commands, you can use string predefined variables
before: # list of bash commands, you can use string predefined variables, custom variables or system environment variables!
- echo "test" > some_file4.md
- mkdir -p changelog/v{VERSION}
- echo "Super release description for tests" > changelog/v{VERSION}/version_summary.md
- echo "Super release description for tests generated at {CI_COMMIT_TIMESTAMP}" > changelog/v{VERSION}/version_summary.md
# definition of release which will be created
release:
description:
Expand Down Expand Up @@ -128,6 +128,14 @@ which will be overriden in child `valhalla.yml`.**
| `VERSION` | value extracted from branch name, for `release-1.2.14` it will be `1.2.14` |
| `VALHALLA_TOKEN` | token passed to CI runner which execute this job |

### 🐛 Environment variables

Valhalla allows you to use any variable defined in your environment system, it is useful f.e when you
are using GitLab CI/CD and you want to use [GitLab CI/CD predefined variables](https://docs.gitlab.com/ee/ci/variables/predefined_variables.html)
in your `valhalla.yml`.

**Use `{}` to evaluate variable to value f.e. `{HOME}`**

### 🦊 .gitlab-ci.yml

- if using GitLab workflows
Expand All @@ -136,16 +144,23 @@ which will be overriden in child `valhalla.yml`.**

```yml
# Modify your workflow rules to include rule to start pipeline for branch starting with name
# release- and exclude commits with VALHALLA SKIP (when valhalla commits files we don't want to start pipeline again)
workflows:
# .... your standard workflows here
- if: $CI_COMMIT_BRANCH =~ /^release-*/ && $CI_COMMIT_TITLE !~ /.*VALHALLA SKIP.*/
# add new stage
stages:
# .... your standard stages here
- release
valhalla_release:
stage: release
image: logchange/valhalla:1.2.1
image: logchange/valhalla:1.3.0
# Prevent from fetching artifacts because it is a problem during committing all files (git add .)
# https://docs.gitlab.com/ee/ci/jobs/job_artifacts.html#prevent-a-job-from-fetching-artifacts
dependencies: []
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
when: never
Expand All @@ -158,7 +173,12 @@ valhalla_release:
```
### 🚧 .gitignore

Add to your `.gitignore` following rules, please create issue if valhalla commits to much files!
Add to your `.gitignore` following rules, please create issue if valhalla commits to many files!
(For GitLab CI/CD add `dependencies: []` which will prevent from committing generated files)

It is important to modify your `.gitignore` when valhalla during `commit_before_release` or
`commit_after_release` phase generates files which you don't want to commit (f.e. maven release plugin or
maven version plugin generates `pom.xml` backup - for mvn version you can also use `-DgenerateBackupPoms=false`)

```
### Valhalla ###
Expand Down
42 changes: 42 additions & 0 deletions test/extends/resolver_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os
import unittest
from unittest.mock import patch

from valhalla.common.resolver import resolve, init_str_resolver, init_str_resolver_custom_variables


class TestStringResolver(unittest.TestCase):

def test_resolve_predefined(self):
# given:
init_str_resolver("1.0", "token123")
init_str_resolver_custom_variables({"CUSTOM_VAR": "value123"})

# when:
resolved_string = resolve("Testing {VERSION}")

# then:
self.assertEqual("Testing 1.0", resolved_string)

def test_resolve_custom_variables(self):
# given:
init_str_resolver("1.0", "token123")
init_str_resolver_custom_variables({"CUSTOM_VAR": "value123"})

# when:
resolved_string = resolve("Testing {CUSTOM_VAR}")

# then:
self.assertEqual("Testing value123", resolved_string)

@patch.dict(os.environ, {"ENV_VAR": "env_value123"})
def test_resolve_from_env(self):
# given:
init_str_resolver("1.0", "token123")
init_str_resolver_custom_variables({"CUSTOM_VAR": "value123"})

# when:
resolved_string = resolve("Testing {ENV_VAR}")

# then:
self.assertEqual("Testing env_value123", resolved_string)
32 changes: 27 additions & 5 deletions valhalla/common/resolver.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from valhalla.common.logger import info, error

VERSION = "not_set"
Expand All @@ -21,20 +23,40 @@ def init_str_resolver_custom_variables(variables: dict):


def resolve(string: str):
global VERSION
global VALHALLA_TOKEN
global CUSTOM_VARIABLES_DICT

if VERSION == "not_set":
error("There was no init_str_resolver(...) call in the code, so resolving strings does not work!")
error("There is bug in valhalla! Please report it here: https://github.com/logchange/valhalla/issues")
exit(1)

string = __resolve_predefined(string)
string = __resolve_custom_variables(string)
string = __resolve_from_env(string)

info("String resolving output: " + string)
return string


def __resolve_predefined(string: str):
global VERSION
global VALHALLA_TOKEN

string = string.replace("{VERSION}", VERSION)
string = string.replace("{VALHALLA_TOKEN}", VALHALLA_TOKEN)

return string


def __resolve_custom_variables(string: str):
global CUSTOM_VARIABLES_DICT

for key, value in CUSTOM_VARIABLES_DICT.items():
string = string.replace("{" + key + "}", value)

info("String resolving output: " + string)
return string


def __resolve_from_env(string: str):
# Iterating over each environment variable
for env_var in os.environ:
string = string.replace('{' + env_var + '}', os.environ.get(env_var, ''))
return string

0 comments on commit b0fb6d2

Please sign in to comment.