plugin-buildspec-go Outdated/Broken Job suggestion for Go projects #2975
Corentin B opened 1 day ago

The suggested Job for Go projects is completely out of date and no longer works on recent version of Go (Tested in 1.26.5)
I wanted to create a PR to provide the update I wrote for a personal project, but I do not seem to have the permission to do such a thing. Oh well
Anyway here it is: example of a Working .onedev-buildspec.yaml

version: 52
jobs:
- name: go ci
  steps:
  - type: CheckoutStep
    name: checkout code
    cloneCredential:
      type: DefaultCredential
    withLfs: false
    withSubmodules: false
    condition: SUCCESSFUL
    optional: false
  - type: SetupCacheStep
    name: set up dependency cache
    key: go_cache
    checksumFiles: '**/go.mod'
    entries:
    - path: /root/.cache/go_build
    - path: /root/.cache/golangci-lint
    - path: /go/pkg/mod
    uploadStrategy: UPLOAD_IF_NOT_EXACT_MATCH
    condition: SUCCESSFUL
    optional: false
  - type: CommandStep
    name: build and test
    runInContainer: true
    image: golang:1.26-trixie
    interpreter:
      type: PosixInterpreter
      shell: sh
      commands: |
        set -e
        go install github.com/boumenot/gocover-cobertura@@latest
        go test -test.coverprofile=coverage.out
        gocover-cobertura < coverage.out > coverage.xml
        go install github.com/jstemmer/go-junit-report/v2@@latest
        go test -v 2>&1 ./... | go-junit-report -set-exit-code > test-result.xml
    runAs: 0:0
    useTTY: true
    condition: SUCCESSFUL
    optional: false
  - type: CommandStep
    name: check and lint
    runInContainer: true
    image: golangci/golangci-lint
    interpreter:
      type: PosixInterpreter
      shell: sh
      commands: golangci-lint run --timeout=10m --issues-exit-code=0 --output.checkstyle.path lint-result.xml
    runAs: 0:0
    useTTY: true
    condition: ALWAYS
    optional: false
  - type: PublishCoberturaReportStep
    name: publish coverage report
    reportName: Coverage
    filePatterns: coverage.xml
    condition: ALWAYS
    optional: false
  - type: PublishJUnitReportStep
    name: publish unit test report
    reportName: Unit Test
    filePatterns: test-result.xml
    condition: ALWAYS
    optional: false
  - type: PublishCheckstyleReportStep
    name: publish lint report
    reportName: Lint
    filePatterns: lint-result.xml
    failThreshold: HIGH
    condition: ALWAYS
    optional: true
  triggers:
  - type: BranchUpdateTrigger
    userMatch: anyone
  - type: PullRequestUpdateTrigger
  retryCondition: never
  maxRetries: 3
  retryDelay: 30
  timeout: 14400

The suggestion is stored here: https://code.onedev.io/onedev/server/~files/main/server-plugin/server-plugin-buildspec-golang/src/main/java/io/onedev/server/plugin/buildspec/golang/GolangJobSuggestion.java
Here is the modified suggestJobs function:

@Override
	public Collection<Job> suggestJobs(Project project, ObjectId commitId) {
		List<Job> jobs = new ArrayList<>();
		if (project.getBlob(new BlobIdent(commitId.name(), "go.mod", FileMode.TYPE_FILE), false) != null) {
			Job job = newJob();
			var setupCache = new SetupCacheStep();
			setupCache.setName("set up dependency cache");
			setupCache.setKey("go_cache");

			setupCache.setChecksumFiles("**/go.mod");
			setupCache.setPaths(Lists.newArrayList(
				"/root/.cache/go_build", 
				"/root/.cache/golangci-lint", 
				"/go/pkg/mod"));
			job.getSteps().add(setupCache);

			CommandStep buildAndTest = new CommandStep();
			buildAndTest.setName("build and test");
			
			buildAndTest.setImage("golang:@" + JobVariableInterpolator.PREFIX_SCRIPT + GroovyScript.BUILTIN_PREFIX + DETERMINE_GO_VERSION + "@");
			buildAndTest.getInterpreter().setCommands("" +
					"set -e\n" +
					"# Use double at to avoid being interpreted as OneDev variable substitution\n" +
					"go install github.com/boumenot/gocover-cobertura@@latest\n" + 
					"go test -test.coverprofile=coverage.out\n" +
					"go install github.com/jstemmer/go-junit-report/v2@@latest\n" +
					"gocover-cobertura < coverage.out > coverage.xml\n" +
					"go install github.com/jstemmer/go-junit-report/v2@@latest\n" +
					"go test -v 2>&1 ./... | go-junit-report -set-exit-code > test-result.xml\n";
			job.getSteps().add(buildAndTest);
			
			var checkAndLint = new CommandStep();
			checkAndLint.setName("check and lint");
			checkAndLint.setImage("golangci/golangci-lint");
			checkAndLint.setCondition(ExecuteCondition.NEVER);
			checkAndLint.getInterpreter().setCommands("golangci-lint run --timeout=10m --issues-exit-code=0 --output.checkstyle.path lint-result.xml");
			job.getSteps().add(checkAndLint);
			
			addCommonJobsAndTriggers(job);
			jobs.add(job);
		}
		return jobs;
	}

The only change is the use of more up-to-date packages.

I hope that this will save some time to someone somewhere. Wish you a great day and keep up the good work!

1/1
Type
Improvement
Priority
Minor
Assignee
Labels
No labels
Issue Votes (0)
Watchers (3)
Reference
OD-2975
Please wait...
Connection lost or session expired, reload to recover
Page is in error, reload to recover