Before starting implementing commands, we need to find a way to associate the current vcs repository the project is hosted with a corresponding Jenkins Job configuration. This is vital, no command can be done without it.
Branch association with Jenkins jobs
Note that this part is tricky, because it's not always true that a git branch name corresponds to a Jenkins job. In particular, a Job can be a simple pipeline for a desired branch, while another branch can be configured with another Job. Finally, a multipipeline project can wrap up all the repository and have Job per-branch associated.
So, since this association is not straight forward, I'd suggest impementing a sort of "linking" procedure between the git branches and the Jenkins jobs. First of all, I'd introduce a new directory, called .jkk, where all the jkk configurations are stored for that project.
In that folder, I'd create a TOML file (something like config) with the following structure:
a job element tells jkk that all the matching branches under it have to be mapped to the following job
a branch element performs the association between the local git branch name and the remote "branch" in Jenkins (if available, here I'm talking about multibranch pipeline job and simple pipelines - have to check for simple jobs), that lives in the beforehand mentioned job section
fallback at job level identifies the policy to use for all the branches that are not listed in the configuration file. In this case, exact means an exact match is performed, while abort will make the CLI exit if no configuration is found. This field is optional, with a default set to simple
folder is an optional field too, that tells jkk where the Job lives in Jenkins. If no value is provided, the default is to look in the Jenkins project root
the name field is the only one mandatory, that has to be a valid Job name in Jenkins.
Note that this configuration has to exist, since at least one job needs to be specified.
The command itself I think its pretty simple. The command will give the possibility for the user to create a global association between the vcs and the Jenkins Job. Something like:
jkk config [--global, --remove] [ARGS]...
Where
global flag can be typed as global or g. This flag will write the configuration not in the following vcs folder, if present, but in the user ${HOME} instead.
remove flag can be typed as remove or rm. This flag will remove the entry selected (passed valued will be ignored)
This will setup the config file and create the necessary folder (if it doesn't exist).
ARGS here identifies the TOML path to create/edit. For example, if I want to create something like this:
New invocation of this command with different argument will update the existing one. If a remove (or rm) flag is passed, the values is removed instead i.e.:
jkk config --rm association.job0 # Remove all the Job
jkk config --rm association.job0.fallback # Remove the fallback strategy
jkk config --rm association.job0.branch0 # Remove the branch 0 local/remote association for the job 0
Before starting implementing commands, we need to find a way to associate the current
vcs
repository the project is hosted with a corresponding Jenkins Job configuration. This is vital, no command can be done without it.Branch association with Jenkins jobs
Note that this part is tricky, because it's not always true that a git branch name corresponds to a Jenkins job. In particular, a Job can be a simple pipeline for a desired branch, while another branch can be configured with another Job. Finally, a multipipeline project can wrap up all the repository and have Job per-branch associated.
So, since this association is not straight forward, I'd suggest impementing a sort of "linking" procedure between the git branches and the Jenkins jobs. First of all, I'd introduce a new directory, called
.jkk
, where all thejkk
configurations are stored for that project. In that folder, I'd create a TOML file (something likeconfig
) with the following structure:Where:
job
element tellsjkk
that all the matching branches under it have to be mapped to the following jobbranch
element performs the association between the local git branch name and the remote "branch" in Jenkins (if available, here I'm talking about multibranch pipeline job and simple pipelines - have to check for simple jobs), that lives in the beforehand mentionedjob
sectionfallback
at job level identifies the policy to use for all the branches that are not listed in the configuration file. In this case,exact
means an exact match is performed, whileabort
will make the CLI exit if no configuration is found. This field is optional, with a default set tosimple
folder
is an optional field too, that tellsjkk
where the Job lives in Jenkins. If no value is provided, the default is to look in the Jenkins project rootname
field is the only one mandatory, that has to be a valid Job name in Jenkins.Note that this configuration has to exist, since at least one job needs to be specified.
A TOML specification with examples can be found here.
Components to use
Fortunately, Konf works with TOML, and it's already used to parse the login configuration. This means that we'll have to create the relative Objects like for the Auth one: https://github.com/Polpetta/jkk/blob/598084f1360a6d2e8d13022868921999c5a70bcc/src/main/kotlin/it/polpetta/config/Auth.kt#L23-L27
The CLI command
The command itself I think its pretty simple. The command will give the possibility for the user to create a global association between the
vcs
and the Jenkins Job. Something like:Where
global
flag can be typed asglobal
org
. This flag will write the configuration not in the following vcs folder, if present, but in the user${HOME}
instead.remove
flag can be typed asremove
orrm
. This flag will remove the entry selected (passed valued will be ignored) This will setup theconfig
file and create the necessary folder (if it doesn't exist).ARGS
here identifies the TOML path to create/edit. For example, if I want to create something like this:I would need to type the following commands:
New invocation of this command with different argument will update the existing one. If a
remove
(orrm
) flag is passed, the values is removed instead i.e.: