Compare commits

..

3 Commits

Author SHA1 Message Date
appleboy c213337305 ci: enhance CI/CD pipeline with GoReleaser integration
- Replace `go-version` with `go-version-file` and add `check-latest` in GitHub Actions workflow
- Add `.goreleaser.yaml` configuration file with build, archive, checksum, snapshot, release, and changelog settings

Signed-off-by: appleboy <appleboy.tw@gmail.com>
2024-10-05 22:26:32 +08:00
Bo-Yi Wu a7c6b81621 feat: support parameter handling for dynamic URL paths (#35)
- Comment out unused form data encoding in `post` method
- Remove redundant error logging in `post` method
- Add conditional URL path selection in `trigger` method based on parameters
- Add `parameter` flag to CLI options in `main.go`
- Include `parameter` in the `run` function configuration
- Import `net/url` package in `plugin.go`
- Add `Parameter` field to `plugin.go` struct
- Parse and add parameters to URL values in `Exec` method
- Pass parsed parameters to `trigger` method in `Exec` function

Signed-off-by: appleboy <appleboy.tw@gmail.com>
2024-10-05 20:25:01 +08:00
appleboy bb1e9fe4e0 feat: improve logging and error handling in Jenkins plugin
- Add error handling for unexpected response codes in `post` method of `jenkins.go`
- Import `log` package in `plugin.go`
- Add logging for successful job trigger in `Exec` method of `plugin.go`

Signed-off-by: appleboy <appleboy.tw@gmail.com>
2024-10-05 13:28:14 +08:00
5 changed files with 166 additions and 16 deletions
+2 -1
View File
@@ -19,7 +19,8 @@ jobs:
- name: Setup go
uses: actions/setup-go@v5
with:
go-version: "^1"
go-version-file: go.mod
check-latest: true
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v6
+123
View File
@@ -0,0 +1,123 @@
before:
hooks:
- go mod tidy
builds:
- env:
- CGO_ENABLED=0
goos:
- darwin
- linux
- windows
- freebsd
goarch:
- amd64
- arm
- arm64
goarm:
- "5"
- "6"
- "7"
ignore:
- goos: darwin
goarch: arm
- goos: darwin
goarch: ppc64le
- goos: darwin
goarch: s390x
- goos: windows
goarch: ppc64le
- goos: windows
goarch: s390x
- goos: windows
goarch: arm
goarm: "5"
- goos: windows
goarch: arm
goarm: "6"
- goos: windows
goarch: arm
goarm: "7"
- goos: windows
goarch: arm64
- goos: freebsd
goarch: ppc64le
- goos: freebsd
goarch: s390x
- goos: freebsd
goarch: arm
goarm: "5"
- goos: freebsd
goarch: arm
goarm: "6"
- goos: freebsd
goarch: arm
goarm: "7"
- goos: freebsd
goarch: arm64
flags:
- -trimpath
ldflags:
- -s -w
- -X main.Version={{.Version}}
binary: >-
{{ .ProjectName }}-
{{- if .IsSnapshot }}{{ .Branch }}-
{{- else }}{{- .Version }}-{{ end }}
{{- .Os }}-
{{- if eq .Arch "amd64" }}amd64
{{- else if eq .Arch "amd64_v1" }}amd64
{{- else if eq .Arch "386" }}386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}-{{ .Arm }}{{ end }}
no_unique_dist_dir: true
hooks:
post:
- cmd: xz -k -9 {{ .Path }}
dir: ./dist/
archives:
- format: binary
name_template: "{{ .Binary }}"
allow_different_binary_count: true
checksum:
name_template: "checksums.txt"
extra_files:
- glob: ./**.xz
snapshot:
name_template: "{{ incpatch .Version }}"
release:
# You can add extra pre-existing files to the release.
# The filename on the release will be the last part of the path (base).
# If another file with the same name exists, the last one found will be used.
#
# Templates: allowed
extra_files:
- glob: ./**.xz
changelog:
use: github
groups:
- title: Features
regexp: "^.*feat[(\\w)]*:+.*$"
order: 0
- title: "Bug fixes"
regexp: "^.*fix[(\\w)]*:+.*$"
order: 1
- title: "Enhancements"
regexp: "^.*chore[(\\w)]*:+.*$"
order: 2
- title: "Refactor"
regexp: "^.*refactor[(\\w)]*:+.*$"
order: 3
- title: "Build process updates"
regexp: ^.*?(build|ci)(\(.+\))??!?:.+$
order: 4
- title: "Documentation updates"
regexp: ^.*?docs?(\(.+\))??!?:.+$
order: 4
- title: Others
order: 999
+12 -4
View File
@@ -81,18 +81,21 @@ func (jenkins *Jenkins) parseResponse(resp *http.Response, body interface{}) (er
func (jenkins *Jenkins) post(path string, params url.Values, body interface{}) (err error) {
requestURL := jenkins.buildURL(path, params)
// formData := params.Encode()
req, err := http.NewRequest("POST", requestURL, nil)
if err != nil {
fmt.Println(err)
return
}
resp, err := jenkins.sendRequest(req)
if err != nil {
fmt.Println(err)
return
}
if resp.StatusCode != http.StatusCreated {
return fmt.Errorf("unexpected response code: %d", resp.StatusCode)
}
return jenkins.parseResponse(resp, body)
}
@@ -114,7 +117,12 @@ func (jenkins *Jenkins) parseJobPath(job string) string {
}
func (jenkins *Jenkins) trigger(job string, params url.Values) error {
path := jenkins.parseJobPath(job) + "/build"
var urlPath string
if len(params) == 0 {
urlPath = jenkins.parseJobPath(job) + "/build"
} else {
urlPath = jenkins.parseJobPath(job) + "/buildWithParameters"
}
return jenkins.post(path, params, nil)
return jenkins.post(urlPath, params, nil)
}
+11 -5
View File
@@ -59,6 +59,11 @@ func main() {
Usage: "allow insecure server connections when using SSL",
EnvVar: "PLUGIN_INSECURE,JENKINS_INSECURE,INPUT_INSECURE",
},
cli.StringSliceFlag{
Name: "parameter,p",
Usage: "jenkins build parameter",
EnvVar: "PLUGIN_PARAMETER,JENKINS_PARAMETER,INPUT_PARAMETER",
},
}
// Override a template
@@ -101,11 +106,12 @@ REPOSITORY:
func run(c *cli.Context) error {
plugin := Plugin{
BaseURL: c.String("host"),
Username: c.String("user"),
Token: c.String("token"),
Job: c.StringSlice("job"),
Insecure: c.Bool("insecure"),
BaseURL: c.String("host"),
Username: c.String("user"),
Token: c.String("token"),
Job: c.StringSlice("job"),
Insecure: c.Bool("insecure"),
Parameter: c.StringSlice("parameter"),
}
return plugin.Exec()
+18 -6
View File
@@ -2,17 +2,20 @@ package main
import (
"errors"
"log"
"net/url"
"strings"
)
type (
// Plugin values.
Plugin struct {
BaseURL string
Username string
Token string
Job []string
Insecure bool
BaseURL string
Username string
Token string
Job []string
Insecure bool
Parameter []string
}
)
@@ -49,10 +52,19 @@ func (p Plugin) Exec() error {
jenkins := NewJenkins(auth, p.BaseURL, p.Insecure)
params := url.Values{}
for _, v := range p.Parameter {
kv := strings.Split(v, "=")
if len(kv) == 2 {
params.Add(kv[0], kv[1])
}
}
for _, v := range jobs {
if err := jenkins.trigger(v, nil); err != nil {
if err := jenkins.trigger(v, params); err != nil {
return err
}
log.Printf("trigger job %s success", v)
}
return nil