This commit is contained in:
“xHuPo” 2025-05-22 11:43:23 +08:00
parent 16c053bacb
commit 55f3e2b3c6
9 changed files with 580 additions and 0 deletions

View file

@ -2,6 +2,11 @@
jenkins-cron项目访问到jenkins使用gradle编译项目的缓存目录删除缓存目录中的文件达到清理缓存的目的。
触发jenkins中相应job编译刷新缓存。
## 版本说明
v1: 满足基本要求,清理缓存效率不高
v2: 优化,缓存清理效率提高;多次触发编译防止刷新缓存失败
v3特化新增特定参数即如果存在特定参数则这些特定参数需要分别执行一次构建如果不存在则只使用最近一次构建的参数执行构建
## 使用方法
* 前提 *
* 1. jenkins中对应的job需要配置布尔类型的only_build参数当该参数为true时仅编译不进行发布。 *

4
v3/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

178
v3/clean.go Normal file
View file

@ -0,0 +1,178 @@
package main
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"sync"
"sync/atomic"
"time"
)
type RemoveStats struct {
Total int64
Success int64
Failed int64
Errors []error
}
type RemoveOptions struct {
Workers int // 并发 worker 数
Logger func(format string, args ...any)
Retries int // 删除失败重试次数
Stats *RemoveStats // 删除统计(可选)
Progress func(current int64) // 删除进度回调(可选)
}
type removeResult struct {
Err error
}
func Remove(ctx context.Context, path string, opts RemoveOptions) error {
if opts.Workers <= 0 {
opts.Workers = runtime.NumCPU()
}
if opts.Logger == nil {
opts.Logger = log.Printf
}
if opts.Retries < 0 {
opts.Retries = 0
}
if opts.Stats == nil {
opts.Stats = &RemoveStats{}
}
// 检查路径是否存在
if _, err := os.Stat(path); os.IsNotExist(err) {
opts.Logger("[clean] Path does not exist, skipping: %s", path)
return nil
}
workChan := make(chan string, opts.Workers*2)
resultChan := make(chan removeResult, opts.Workers*2)
var wg sync.WaitGroup
for i := 0; i < opts.Workers; i++ {
wg.Add(1)
go removeWorker(ctx, &wg, workChan, resultChan, opts)
}
// 目录遍历
go func() {
defer close(workChan)
_ = walkDir(ctx, path, workChan, opts)
}()
// 收集结果
go func() {
wg.Wait()
close(resultChan)
}()
var (
firstErr error
errorsMu sync.Mutex
)
for res := range resultChan {
atomic.AddInt64(&opts.Stats.Total, 1)
if res.Err != nil {
atomic.AddInt64(&opts.Stats.Failed, 1)
errorsMu.Lock()
opts.Stats.Errors = append(opts.Stats.Errors, res.Err)
errorsMu.Unlock()
if firstErr == nil {
firstErr = res.Err
}
} else {
atomic.AddInt64(&opts.Stats.Success, 1)
}
if opts.Progress != nil {
opts.Progress(atomic.LoadInt64(&opts.Stats.Total))
}
}
// 尝试删除根目录
if err := tryRemoveRoot(ctx, path, opts); err != nil && firstErr == nil {
firstErr = err
opts.Stats.Failed++
opts.Stats.Errors = append(opts.Stats.Errors, err)
}
return firstErr
}
// 遍历目录,逐一发送子路径(不含根目录)到 workChan深度优先、逆序删除
func walkDir(ctx context.Context, root string, workChan chan<- string, opts RemoveOptions) error {
var paths []string
err := filepath.WalkDir(root, func(subPath string, d os.DirEntry, err error) error {
if ctx.Err() != nil {
return ctx.Err()
}
if err != nil {
opts.Logger("[clean] Walk error on %s: %v", subPath, err)
return nil // 跳过错误继续
}
if subPath != root {
paths = append(paths, subPath)
}
return nil
})
// 逆序删除(先深后浅)
for i := len(paths) - 1; i >= 0; i-- {
select {
case <-ctx.Done():
return ctx.Err()
case workChan <- paths[i]:
}
}
return err
}
// Worker 执行删除任务,支持重试
func removeWorker(ctx context.Context, wg *sync.WaitGroup, workChan <-chan string, resultChan chan<- removeResult, opts RemoveOptions) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case subPath, ok := <-workChan:
if !ok {
return
}
var err error
for i := 0; i <= opts.Retries; i++ {
err = os.RemoveAll(subPath)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
if err != nil {
opts.Logger("[clean] Failed to remove %s: %v", subPath, err)
resultChan <- removeResult{Err: fmt.Errorf("remove %q: %w", subPath, err)}
} else {
opts.Logger("[clean] Removed: %s", subPath)
resultChan <- removeResult{}
}
}
}
}
// 删除根目录
func tryRemoveRoot(ctx context.Context, path string, opts RemoveOptions) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if err := os.Remove(path); err != nil {
opts.Logger("[clean] Failed to remove root %s: %v", path, err)
return fmt.Errorf("remove root %q: %w", path, err)
}
opts.Logger("[clean] Removed root directory: %s", path)
return nil
}

48
v3/config.go Normal file
View file

@ -0,0 +1,48 @@
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"`
SpecialParameters []map[string]interface{} `mapstructure:"special_parameters"`
}
type Config struct {
Gradle Gradle
Jenkins Jenkins
Jobs []string `mapstructure:"jobs"`
Retries int `mapstructure:"retries"`
}
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
}

17
v3/config.yaml Normal file
View file

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

21
v3/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
v3/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=

200
v3/jenkins.go Normal file
View file

@ -0,0 +1,200 @@
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"path"
"strings"
"time"
)
type Build struct {
Actions []Action `json:"actions"`
Number int `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"`
}
type JenkinsClient struct {
Client *http.Client
Config *Jenkins
}
type JenkinsService interface {
FetchBuild(job string) (*Build, error)
TriggerBuild(build *Build, params map[string]interface{}) error
}
func NewJenkinsClient(cfg *Jenkins) *JenkinsClient {
return &JenkinsClient{
Client: &http.Client{Timeout: 10 * time.Second},
Config: cfg,
}
}
func (c *JenkinsClient) buildAPIURL(job string) string {
return fmt.Sprintf("%s://%s/job/%s/%s/api/json",
c.Config.Schema,
c.Config.URL,
job,
c.Config.Number,
)
}
func (c *JenkinsClient) FetchBuild(job string) (*Build, error) {
api := c.buildAPIURL(job)
req, err := http.NewRequest("GET", api, nil)
if err != nil {
log.Printf("[jenkins] Failed to create GET request %s: %v", job, err)
return nil, err
}
req.SetBasicAuth(c.Config.User, c.Config.Token)
resp, err := c.Client.Do(req)
if err != nil {
log.Printf("[jenkins] Failed to fetch build %s: %v", job, err)
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("[jenkins] Failed to fetch build %s: %s", job, resp.Status)
return nil, fmt.Errorf("unexpected status: %s", resp.Status)
}
// io限制1m
body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
if err != nil {
log.Printf("[jenkins] Failed to read response body: %v", err)
return nil, fmt.Errorf("failed to read response body: %w", err)
}
var build Build
if err := json.Unmarshal(body, &build); err != nil {
log.Printf("[jenkins] Failed to unmarshal build %s: %v", job, err)
return nil, fmt.Errorf("failed to unmarshal response body: %w", err)
}
return &build, nil
}
// 提取构建参数为 map[string]string
func ExtractBuildParams(build *Build) map[string]string {
params := make(map[string]string)
for _, action := range build.Actions {
if action.Class == "hudson.model.ParametersAction" {
for _, param := range action.Parameters {
params[param.Name] = fmt.Sprintf("%v", param.Value)
}
}
}
return params
}
// 合并两个 map[string]string 和 []map[string]interface{}
func MergeParams(base map[string]string, defaults []map[string]interface{}) map[string]string {
result := make(map[string]string)
for k, v := range base {
result[k] = v
}
for _, param := range defaults {
for k, v := range param {
result[k] = fmt.Sprintf("%v", v)
}
}
return result
}
// string map 转 interface map
func StringMapToInterfaceMap(m map[string]string) map[string]interface{} {
res := make(map[string]interface{})
for k, v := range m {
res[k] = v
}
return res
}
// 判断 b 是否为 a 的子集
func IsSubset(a map[string]string, b map[string]interface{}) bool {
for k, v := range b {
if a[k] != fmt.Sprintf("%v", v) {
return false
}
}
return true
}
// 构建构建触发 URL带参数
func (c *JenkinsClient) generateBuildURL(baseURL string, mergedParams map[string]string) string {
values := url.Values{}
for k, v := range mergedParams {
values.Set(k, v)
}
// 构建新的构建路径
u, err := url.Parse(baseURL)
if err != nil {
log.Printf("[jenkins] Failed to parse build URL: %v", err)
return ""
}
u.Path = getBuildWithParametersPath(u.Path)
u.RawQuery = values.Encode()
return u.String()
}
// 替换为 buildWithParameters 路径
func getBuildWithParametersPath(p string) string {
parts := strings.Split(strings.TrimSuffix(p, "/"), "/")
if len(parts) > 0 {
return path.Join(parts[:len(parts)-1]...) + "/buildWithParameters"
}
return p
}
// 触发构建
func (c *JenkinsClient) TriggerBuild(build *Build, params map[string]interface{}) error {
merged := make(map[string]string)
for k, v := range params {
merged[k] = fmt.Sprintf("%v", v)
}
triggerURL := c.generateBuildURL(build.URL, merged)
if triggerURL == "" {
log.Printf("[jenkins] Failed to generate trigger URL")
return fmt.Errorf("failed to generate trigger URL")
}
req, err := http.NewRequest("POST", triggerURL, nil)
if err != nil {
log.Printf("[jenkins] Failed to create POST request: %v", err)
return fmt.Errorf("created trigger request failed: %w", err)
}
req.SetBasicAuth(c.Config.User, c.Config.Token)
resp, err := c.Client.Do(req)
if err != nil {
log.Printf("[jenkins] Failed to trigger build %s: %v", triggerURL, err)
return fmt.Errorf("trigger request failed: %w", err)
}
defer resp.Body.Close()
log.Printf("[jenkins] Triggered build %s: %s", triggerURL, resp.Status)
return nil
}

71
v3/main.go Normal file
View file

@ -0,0 +1,71 @@
package main
import (
"context"
"log"
)
func main() {
cfg, _ := LoadConfig("/app")
if len(cfg.Jobs) == 0 {
log.Printf("[main] No jobs configured")
return
}
if cfg.Jenkins.URL == "" {
log.Printf("[main] No Jenkins URL configured")
return
}
ctx := context.Background()
stats := &RemoveStats{}
for _, cache := range cfg.Gradle.Caches {
err := Remove(ctx, cache, RemoveOptions{
Workers: 4,
Retries: 3,
Stats: stats,
Logger: log.Printf,
Progress: func(current int64) { log.Printf("[main] Removed %d files", current) },
})
if err != nil {
log.Printf("[main] Error removing cache %s: %s", cache, err)
}
log.Printf("Summary: Total=%d Success=%d Failed=%d", stats.Total, stats.Success, stats.Failed)
}
var jenkinsSvc JenkinsService = NewJenkinsClient(&cfg.Jenkins)
triggerJobs(jenkinsSvc, cfg)
}
func triggerJobs(js JenkinsService, cfg *Config) {
for _, job := range cfg.Jobs {
log.Printf("[main] Triggering build for job %s", job)
build, err := js.FetchBuild(job)
if err != nil {
log.Printf("[main] Error fetching build for job %s: %s", job, err)
return
}
for i := 0; i < cfg.Retries; i++ {
latestParams := ExtractBuildParams(build)
merged := MergeParams(latestParams, cfg.Jenkins.DefaultParameters)
if err := js.TriggerBuild(build, StringMapToInterfaceMap(merged)); err != nil {
log.Printf("[main] Error triggering build for job %s: %s", job, err)
}
for _, special := range cfg.Jenkins.SpecialParameters {
if IsSubset(latestParams, special) {
log.Printf("[main] Skipping special parameters build (subset of latest) for %s: %+v", job, special)
continue
}
log.Printf("[main] Triggering special parameters build for %s: %+v", job, special)
if err := js.TriggerBuild(build, special); err != nil {
log.Printf("[main] Error triggering special parameters build for job %s: %s", job, err)
}
}
}
}
}