diff --git a/Dockerfile b/Dockerfile index 233338b..82ce4b8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,13 @@ FROM alpine:3 +# Labels. +LABEL org.opencontainers.image.authors='peter.zmilczak@gmail.com' \ + 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 \ diff --git a/README.md b/README.md index 0784c3e..0a33983 100644 --- a/README.md +++ b/README.md @@ -40,10 +40,10 @@ commit_before_release: # define actions which have to happen before release and username: Test1234 # git config username email: test-valhalla@logchange.dev # 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: @@ -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 @@ -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 @@ -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 ### diff --git a/test/extends/resolver_test.py b/test/extends/resolver_test.py new file mode 100644 index 0000000..72430dd --- /dev/null +++ b/test/extends/resolver_test.py @@ -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) diff --git a/valhalla/common/resolver.py b/valhalla/common/resolver.py index a11bed7..84fd310 100644 --- a/valhalla/common/resolver.py +++ b/valhalla/common/resolver.py @@ -1,3 +1,5 @@ +import os + from valhalla.common.logger import info, error VERSION = "not_set" @@ -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