first commit

This commit is contained in:
“xHuPo” 2025-05-09 17:21:06 +08:00
commit aaad8b143f
17 changed files with 818 additions and 0 deletions

4
v1/Dockerfile Normal file
View file

@ -0,0 +1,4 @@
FROM swr.cn-east-3.myhuaweicloud.com/turingsyn/alpine:3.21.3
ADD jenkins-cron /app/
RUN chmod +x /app/jenkins-cron
WORKDIR /app

33
v1/clean.go Normal file
View file

@ -0,0 +1,33 @@
package main
import (
"log"
"os"
"path/filepath"
)
func Remove(path string) error {
entries, err := os.ReadDir(path)
if err != nil {
log.Printf("clean: Error reading directory: %v", err)
return err
}
for _, entry := range entries {
if entry.IsDir() {
err := Remove(filepath.Join(path, entry.Name()))
if err != nil {
log.Printf("clean: Error removing directory: %v", err)
return err
}
} else {
err := os.Remove(filepath.Join(path, entry.Name()))
if err != nil {
log.Printf("clean: Error removing file: %v", err)
return err
}
}
}
log.Printf("clean: Removed directory successfully: %s", path)
return nil
}

46
v1/config.go Normal file
View file

@ -0,0 +1,46 @@
package main
import (
"log"
"github.com/spf13/viper"
)
type Gradle struct {
Caches []string `mapstructure:"caches"`
}
type Jenkins struct {
Schema string `mapstructure:"schema"`
URL string `mapstructure:"url"`
User string `mapstructure:"user"`
Token string `mapstructure:"token"`
Number string `mapstructure:"number"`
DefaultParameters []map[string]interface{} `mapstructure:"default_parameters"`
}
type Config struct {
Gradle Gradle
Jenkins Jenkins
Jobs []string `mapstructure:"jobs"`
}
func LoadConfig(path string) (*Config, error) {
v := viper.New()
v.SetConfigName("config")
v.SetConfigType("yaml")
v.AddConfigPath(path)
if err := v.ReadInConfig(); err != nil {
log.Fatal("config: Error reading config file:", err)
return nil, err
}
var config Config
if err := v.Unmarshal(&config); err != nil {
log.Fatal("config: Unable to decode config into struct:", err)
return nil, err
}
return &config, nil
}

16
v1/config.yaml Normal file
View file

@ -0,0 +1,16 @@
gradle:
caches:
- /home/caches
jenkins:
schema: https
url: jenkins-ops.shasoapp.com
user: admin
token: 1234567890
number: lastBuild
default_parameters:
- only_build: true
jobs:
- echo-rework

21
v1/go.mod Normal file
View file

@ -0,0 +1,21 @@
module jenkins-cron
go 1.21.1
require (
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/sagikazarmark/locafero v0.7.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.12.0 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/spf13/viper v1.20.1 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

36
v1/go.sum Normal file
View file

@ -0,0 +1,36 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sagikazarmark/locafero v0.7.0 h1:5MqpDsTGNDhY8sGp0Aowyf0qKsPrhewaLSsFaodPcyo=
github.com/sagikazarmark/locafero v0.7.0/go.mod h1:2za3Cg5rMaTMoG/2Ulr9AwtFaIppKXTRYnozin4aB5k=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs=
github.com/spf13/afero v1.12.0/go.mod h1:ZTlWwG4/ahT8W7T0WQ5uYmjI9duaLQGy3Q2OAl4sk/4=
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.20.1 h1:ZMi+z/lvLyPSCoNtFCpqjy0S4kPbirhpTMwl8BkW9X4=
github.com/spf13/viper v1.20.1/go.mod h1:P9Mdzt1zoHIG8m2eZQinpiBjo6kCmZSKBClNNqjJvu4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

125
v1/jenkins.go Normal file
View file

@ -0,0 +1,125 @@
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"path"
"strings"
"time"
)
type Build struct {
Actions []Action `json:"actions"`
Number interface{} `json:"number"`
URL string `json:"url"`
}
type Action struct {
Class string `json:"_class"`
Parameters []Parameter `json:"parameters,omitempty"`
}
type Parameter struct {
Name string `json:"name"`
Value interface{} `json:"value"`
}
var client = &http.Client{
Timeout: 10 * time.Second,
}
func FetchBuild(cfg *Config, job string) (*Build, error) {
buildID := cfg.Jenkins.Number
api := fmt.Sprintf("%s://%s/job/%s/%s/api/json",
cfg.Jenkins.Schema,
cfg.Jenkins.URL,
job,
buildID,
)
req, err := http.NewRequest("GET", api, nil)
if err != nil {
log.Printf("failed to create GET request %s: %v", job, err)
return nil, err
}
req.SetBasicAuth(cfg.Jenkins.User, cfg.Jenkins.Token)
resp, err := client.Do(req)
if err != nil {
log.Printf("failed to fetch build %s: %v", job, err)
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("failed to fetch build %s: %s", job, resp.Status)
return nil, fmt.Errorf("failed to fetch build: %s", resp.Status)
}
// io限制1m
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
var build Build
if err := json.Unmarshal(body, &build); err != nil {
log.Printf("failed to unmarshal build %s: %v", job, err)
return nil, err
}
return &build, nil
}
func GenerateBuildURL(cfg *Config, build *Build) string {
values := url.Values{}
for _, action := range build.Actions {
if action.Class == "hudson.model.ParametersAction" {
for _, param := range action.Parameters {
values.Set(param.Name, fmt.Sprintf("%v", param.Value))
}
}
}
for _, param := range cfg.Jenkins.DefaultParameters {
for k, v := range param {
values.Set(k, fmt.Sprintf("%v", v))
}
}
u, err := url.Parse(build.URL)
if err != nil {
log.Fatalf("failed to parse build URL: %v", err)
}
parts := strings.Split(strings.TrimSuffix(u.Path, "/"), "/")
if len(parts) > 0 {
u.Path = path.Join(parts[:len(parts)-1]...) + "/buildWithParameters"
}
u.RawQuery = values.Encode()
return u.String()
}
func TriggerBuild(cfg *Config, build *Build) error {
url := GenerateBuildURL(cfg, build)
req, err := http.NewRequest("POST", url, nil)
if err != nil {
log.Printf("failed to create POST request: %v", err)
return err
}
req.SetBasicAuth(cfg.Jenkins.User, cfg.Jenkins.Token)
resp, err := client.Do(req)
if err != nil {
log.Printf("failed to trigger build %s: %v", url, err)
return err
}
defer resp.Body.Close()
log.Printf("triggered build %s: %s", url, resp.Status)
return nil
}

32
v1/main.go Normal file
View file

@ -0,0 +1,32 @@
package main
import "log"
func main() {
cfg, _ := LoadConfig("/app")
if len(cfg.Jobs) == 0 {
log.Printf("main: No jobs configured")
return
}
for _, cache := range cfg.Gradle.Caches {
log.Printf("main: Removing gradle cache %s", cache)
if err := Remove(cache); err != nil {
log.Printf("main: Error removing gradle caches %s: %s", cache, err)
}
}
for _, job := range cfg.Jobs {
log.Printf("main: Triggering build for job %s", job)
build, err := FetchBuild(cfg, job)
if err != nil {
log.Printf("main: Error fetching build for job %s: %s", job, err)
return
}
if err := TriggerBuild(cfg, build); err != nil {
log.Printf("main: Error triggering build for job %s: %s", job, err)
}
}
}