indigo-dc / jenkins-pipeline-library

Jenkins pipeline library with common functionalities for CI/CD environments, mainly targeted for the implementation of the SQA baseline requirements from https://indigo-dc.github.io/sqa-baseline/
Apache License 2.0
11 stars 6 forks source link

Support variables in parameters values that are parsed in JVM side #133

Open samuelbernardolip opened 3 years ago

samuelbernardolip commented 3 years ago

Credentials parameters supporting variables

Motivation

Allow to have config.yml as a template file for multiple users configuration.

Use case

Get real git committer from the git repository, e.g. GIT_REAL_COMMITTER.

config.yml of JePL has the following configuration:

credentials:
    - id: o3as-dockerhub-$GIT_REAL_COMMITER
      username_var: JPL_DOCKERUSER
      password_var: JPL_DOCKERPASS

Then every user ($GIT_REAL_COMMITER) manually configures his/her access to DockerHub via creating credentials in Jenkins, named o3as-dockerhub-$GIT_REAL_COMMITER.

In o3as-dockerhub-$GIT_REAL_COMMITER the DockerHub account can be anything, not necessarily "$GIT_REAL_COMMITER" (i.e. my git account does not need to match DockerHub account).

Using environment variables to retrieve git details

Jenkins provides a set of environments for git configurations that are available only with job system configuration[1].

To get those values is possible to use:

GIT_COMMITER_NAME=$(git log -n 1 --pretty=format:'%an')

GIT_COMMITER_EMAIL_USER=$(git log -n 1 --pretty=format:'%al')


More details about git pretty formats in [2].

### `config.yml` feature support

`config.yml` is parsed in server side where there is already the triggered branch cloned. But the problem is that those identifiers are processed in JVM side and not in shell environment. So doing something as `"o3as-dockerhub-$GIT_REAL_COMMITER"` requires library support over string parsing at shell side, since it requires to run git command. That would also needs an update in `config.yml` validator to support that syntax.

So I would say for the time being is out of scope to implement this enhancement, but useful for a later JCasC adoption as mentioned in issue #97.

### Dynamically generate `config.yml` using a script

Instead of config.yml being already in place in the repository, it is possible to have a script in the repository that generates the `config.yml` file and do in-place substitutions from environment variables.

Something like

cat << EOF > config.yml

...

credentials:

...

EOF



This can be configured in a Jenkinsfile previous sh step or stage.

Another option, when using collaborative version control services, is to run the operations to prepare a branch for JePL submission with the required configurations. In Github is possible to use actions and create workflows. Gitlab have same feature with Gitlab CI.

## References

[1] https://plugins.jenkins.io/git/#system-configuration-variables

[2] https://git-scm.com/docs/pretty-formats