mirror of
https://github.com/appleboy/drone-jenkins.git
synced 2026-06-04 18:23:57 +08:00
3dd86f956c
- Add support for passing a remote trigger token to Jenkins jobs - Update the Jenkins constructor to accept a token parameter - Ensure the token is included as a query parameter when triggering jobs - Improve error reporting by including response body in error messages - Remove unnecessary logging and refactor build parameter logic - Update tests to use the new Jenkins constructor and token handling - Add CLI option for specifying a remote trigger token - Extend plugin configuration to support remote token injection Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
73 lines
1.2 KiB
Go
73 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"log"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
type (
|
|
// Plugin values.
|
|
Plugin struct {
|
|
BaseURL string
|
|
Username string
|
|
Token string
|
|
RemoteToken string
|
|
Job []string
|
|
Insecure bool
|
|
Parameters []string
|
|
}
|
|
)
|
|
|
|
func trimElement(keys []string) []string {
|
|
newKeys := []string{}
|
|
|
|
for _, value := range keys {
|
|
value = strings.Trim(value, " ")
|
|
if len(value) == 0 {
|
|
continue
|
|
}
|
|
newKeys = append(newKeys, value)
|
|
}
|
|
|
|
return newKeys
|
|
}
|
|
|
|
// Exec executes the plugin.
|
|
func (p Plugin) Exec() error {
|
|
if len(p.BaseURL) == 0 || len(p.Username) == 0 || len(p.Token) == 0 {
|
|
return errors.New("missing jenkins config")
|
|
}
|
|
|
|
jobs := trimElement(p.Job)
|
|
|
|
if len(jobs) == 0 {
|
|
return errors.New("missing jenkins job")
|
|
}
|
|
|
|
auth := &Auth{
|
|
Username: p.Username,
|
|
Token: p.Token,
|
|
}
|
|
|
|
jenkins := NewJenkins(auth, p.BaseURL, p.RemoteToken, p.Insecure)
|
|
|
|
params := url.Values{}
|
|
for _, v := range p.Parameters {
|
|
kv := strings.Split(v, "=")
|
|
if len(kv) == 2 {
|
|
params.Add(kv[0], kv[1])
|
|
}
|
|
}
|
|
|
|
for _, v := range jobs {
|
|
if err := jenkins.trigger(v, params); err != nil {
|
|
return err
|
|
}
|
|
log.Printf("trigger job %s success", v)
|
|
}
|
|
|
|
return nil
|
|
}
|