Skip to content

Commit

Permalink
Set go version in actions with patch version to force setup-go to pic…
Browse files Browse the repository at this point in the history
…k up the latest version
  • Loading branch information
SuperSandro2000 committed Apr 4, 2024
1 parent 5bc5483 commit f0a1c35
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 49 deletions.
6 changes: 2 additions & 4 deletions internal/core/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
package core

const (
DefaultAlpineImage = "3.19"
DefaultGolangImagePrefix = "1.22.2-alpine"

DefaultGoVersion = "1.22"
DefaultAlpineImage = "3.19"
DefaultGoVersion = "1.22.2"
DefaultPostgresVersion = "16"
DefaultLinkerdAwaitVersion = "0.2.7"
DefaultGitHubComRunnerType = "ubuntu-latest"
Expand Down
4 changes: 2 additions & 2 deletions internal/dockerfile/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ RUN %s --version 2>/dev/null`, binary.Name)
runCommands := strings.Join(commands, " \\\n && ")

dockerfile := fmt.Sprintf(
`FROM golang:%[1]s%[2]s as builder
`FROM golang:%[1]s-alpine%[2]s as builder
RUN apk add --no-cache --no-progress ca-certificates gcc git make musl-dev
Expand Down Expand Up @@ -132,7 +132,7 @@ LABEL source_repository="%[7]s" \
%[8]s%[9]sWORKDIR %[10]s
ENTRYPOINT [ %[11]s ]
`, core.DefaultGolangImagePrefix, core.DefaultAlpineImage, goBuildflags, addUserGroup, runCommands, runVersionArg, cfg.Metadata.URL, extraDirectives, userCommand, workingDir, entrypoint)
`, core.DefaultGoVersion, core.DefaultAlpineImage, goBuildflags, addUserGroup, runCommands, runVersionArg, cfg.Metadata.URL, extraDirectives, userCommand, workingDir, entrypoint)

must.Succeed(os.WriteFile("Dockerfile", []byte(dockerfile), 0666))

Expand Down
11 changes: 5 additions & 6 deletions internal/ghworkflow/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
const workflowDir = ".github/workflows"

// Render renders GitHub workflows.
func Render(cfg *core.Configuration) {
func Render(cfg core.Configuration) {
ghwCfg := cfg.GitHubWorkflow

must.Succeed(os.MkdirAll(workflowDir, 0o755))
Expand All @@ -39,12 +39,11 @@ func Render(cfg *core.Configuration) {
must.Succeed(os.RemoveAll(filepath.Join(workflowDir, "license.yaml")))
must.Succeed(os.RemoveAll(filepath.Join(workflowDir, "spell.yaml")))

checksWorkflow(ghwCfg, cfg.SpellCheck.IgnoreWords)

ciWorkflow(ghwCfg, len(cfg.Binaries) > 0)
checksWorkflow(cfg)
ciWorkflow(cfg)
ghcrWorkflow(ghwCfg)
releaseWorkflow(ghwCfg)
codeQLWorkflow(ghwCfg)
releaseWorkflow(cfg)
codeQLWorkflow(cfg)
}

func writeWorkflowToFile(w *workflow) {
Expand Down
8 changes: 6 additions & 2 deletions internal/ghworkflow/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ func baseJob(name string, isSelfHostedRunner bool) job {
}
}

func baseJobWithGo(name string, isSelfHostedRunner bool, goVersion string) job {
j := baseJob(name, isSelfHostedRunner)
func baseJobWithGo(name string, cfg core.Configuration) job {
j := baseJob(name, cfg.GitHubWorkflow.IsSelfHostedRunner)
goVersion := cfg.GitHubWorkflow.Global.GoVersion
if cfg.Golang.SetGoModVersion {
goVersion = core.DefaultGoVersion
}
step := jobStep{
Name: "Set up Go",
Uses: core.SetupGoAction,
Expand Down
14 changes: 8 additions & 6 deletions internal/ghworkflow/workflow_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
)

// basically a collection of other linters and checks which run fast to reduce the amount of created githbu action workflows
func checksWorkflow(cfg *core.GithubWorkflowConfiguration, ignoreWords []string) {
w := newWorkflow("Checks", cfg.Global.DefaultBranch, nil)
j := baseJobWithGo("Checks", cfg.IsSelfHostedRunner, cfg.Global.GoVersion)
func checksWorkflow(cfg core.Configuration) {
ghwCfg := cfg.GitHubWorkflow
w := newWorkflow("Checks", ghwCfg.Global.DefaultBranch, nil)
j := baseJobWithGo("Checks", cfg)

if cfg.SecurityChecks.Enabled {
if ghwCfg.SecurityChecks.Enabled {
j.addStep(jobStep{
Name: "Dependency Licenses Review",
Run: "make check-dependency-licenses",
Expand All @@ -37,14 +38,15 @@ func checksWorkflow(cfg *core.GithubWorkflowConfiguration, ignoreWords []string)
})
}

if !cfg.IsSelfHostedRunner {
if !ghwCfg.IsSelfHostedRunner {
with := map[string]any{
"exclude": "./vendor/*",
"reporter": "github-check",
"fail_on_error": true,
"github_token": "${{ secrets.GITHUB_TOKEN }}",
"ignore": "importas", //nolint:misspell //importas is a valid linter name, so we always ignore it
}
ignoreWords := cfg.SpellCheck.IgnoreWords
if len(ignoreWords) > 0 {
with["ignore"] = fmt.Sprintf("%s,%s", with["ignore"], strings.Join(ignoreWords, ","))
}
Expand All @@ -57,7 +59,7 @@ func checksWorkflow(cfg *core.GithubWorkflowConfiguration, ignoreWords []string)
})
}

if cfg.License.Enabled {
if ghwCfg.License.Enabled {
j.addStep(jobStep{
Name: "Check if source code files have license header",
Run: "make check-license-headers",
Expand Down
36 changes: 19 additions & 17 deletions internal/ghworkflow/workflow_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ import (
"github.com/sapcc/go-makefile-maker/internal/core"
)

func ciWorkflow(cfg *core.GithubWorkflowConfiguration, hasBinaries bool) {
w := newWorkflow("CI", cfg.Global.DefaultBranch, cfg.CI.IgnorePaths)
func ciWorkflow(cfg core.Configuration) {
ghwCfg := cfg.GitHubWorkflow
w := newWorkflow("CI", ghwCfg.Global.DefaultBranch, ghwCfg.CI.IgnorePaths)

if w.deleteIf(cfg.CI.Enabled) {
if w.deleteIf(ghwCfg.CI.Enabled) {
return
}

w.Jobs = make(map[string]job)
buildAndLintJob := baseJobWithGo("Build & Lint", cfg.IsSelfHostedRunner, cfg.Global.GoVersion)
if hasBinaries {
buildAndLintJob := baseJobWithGo("Build & Lint", cfg)
if len(cfg.Binaries) > 0 {
buildAndLintJob.addStep(jobStep{
Name: "Build all binaries",
Run: "make build-all",
Expand All @@ -46,12 +47,12 @@ func ciWorkflow(cfg *core.GithubWorkflowConfiguration, hasBinaries bool) {

w.Jobs["buildAndLint"] = buildAndLintJob

testJob := buildOrTestBaseJob("Test", cfg.IsSelfHostedRunner, cfg.CI.RunnerType, cfg.Global.GoVersion)
testJob := buildOrTestBaseJob("Test", cfg)
testJob.Needs = []string{"buildAndLint"}
if cfg.CI.Postgres.Enabled {
if ghwCfg.CI.Postgres.Enabled {
version := core.DefaultPostgresVersion
if cfg.CI.Postgres.Version != "" {
version = cfg.CI.Postgres.Version
if ghwCfg.CI.Postgres.Version != "" {
version = ghwCfg.CI.Postgres.Version
}
testJob.Services = map[string]jobService{"postgres": {
Image: "postgres:" + version,
Expand All @@ -70,8 +71,8 @@ func ciWorkflow(cfg *core.GithubWorkflowConfiguration, hasBinaries bool) {
Name: "Run tests and generate coverage report",
Run: "make build/cover.out",
})
if cfg.CI.Coveralls && !cfg.IsSelfHostedRunner {
multipleOS := len(cfg.CI.RunnerType) > 1
if ghwCfg.CI.Coveralls && !ghwCfg.IsSelfHostedRunner {
multipleOS := len(ghwCfg.CI.RunnerType) > 1
env := map[string]string{
"GIT_BRANCH": "${{ github.head_ref }}",
"COVERALLS_TOKEN": "${{ secrets.GITHUB_TOKEN }}",
Expand All @@ -89,7 +90,7 @@ func ciWorkflow(cfg *core.GithubWorkflowConfiguration, hasBinaries bool) {

if multipleOS {
// 04. Tell Coveralls to merge coverage results.
finishJob := baseJobWithGo("Finish", cfg.IsSelfHostedRunner, cfg.Global.GoVersion)
finishJob := baseJobWithGo("Finish", cfg)
finishJob.Needs = []string{"test"} // this is the <job_id> for the test job
finishJob.addStep(jobStep{
Name: "Coveralls post build webhook",
Expand All @@ -104,16 +105,17 @@ func ciWorkflow(cfg *core.GithubWorkflowConfiguration, hasBinaries bool) {
writeWorkflowToFile(w)
}

func buildOrTestBaseJob(name string, isSelfHostedRunner bool, runsOnList []string, goVersion string) job {
j := baseJobWithGo(name, isSelfHostedRunner, goVersion)
switch len(runsOnList) {
func buildOrTestBaseJob(name string, cfg core.Configuration) job {
ghwCfg := cfg.GitHubWorkflow
j := baseJobWithGo(name, cfg)
switch len(ghwCfg.CI.RunnerType) {
case 0:
// baseJobWithGo() will set j.RunsOn to DefaultGitHubComRunnerType.
case 1:
j.RunsOn = runsOnList[0]
j.RunsOn = ghwCfg.CI.RunnerType[0]
default:
j.RunsOn = "${{ matrix.os }}"
j.Strategy.Matrix.OS = runsOnList
j.Strategy.Matrix.OS = ghwCfg.CI.RunnerType
}
return j
}
11 changes: 6 additions & 5 deletions internal/ghworkflow/workflow_codeql.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import (
"github.com/sapcc/go-makefile-maker/internal/core"
)

func codeQLWorkflow(cfg *core.GithubWorkflowConfiguration) {
w := newWorkflow("CodeQL", cfg.Global.DefaultBranch, nil)
func codeQLWorkflow(cfg core.Configuration) {
ghwCfg := cfg.GitHubWorkflow
w := newWorkflow("CodeQL", ghwCfg.Global.DefaultBranch, nil)

if w.deleteIf(cfg.SecurityChecks.Enabled && !cfg.IsSelfHostedRunner) {
if w.deleteIf(ghwCfg.SecurityChecks.Enabled && !ghwCfg.IsSelfHostedRunner) {
return
}

Expand All @@ -29,10 +30,10 @@ func codeQLWorkflow(cfg *core.GithubWorkflowConfiguration) {

// Overwrite because CodeQL expects the pull_request.branches to be a subset of
// push.branches.
w.On.PullRequest.Branches = []string{cfg.Global.DefaultBranch}
w.On.PullRequest.Branches = []string{ghwCfg.Global.DefaultBranch}
w.On.Schedule = []cronExpr{{Cron: "00 07 * * 1"}} // every Monday at 07:00 AM

j := baseJobWithGo("Analyze", cfg.IsSelfHostedRunner, cfg.Global.GoVersion)
j := baseJobWithGo("Analyze", cfg)
j.addStep(jobStep{
Name: "Initialize CodeQL",
Uses: core.CodeqlInitAction,
Expand Down
9 changes: 5 additions & 4 deletions internal/ghworkflow/workflow_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ package ghworkflow

import "github.com/sapcc/go-makefile-maker/internal/core"

func releaseWorkflow(cfg *core.GithubWorkflowConfiguration) {
func releaseWorkflow(cfg core.Configuration) {
// https://docs.github.com/en/packages/managing-github-packages-using-github-actions-workflows/publishing-and-installing-a-package-with-github-actions#publishing-a-package-using-an-action
w := newWorkflow("goreleaser", cfg.Global.DefaultBranch, nil)
ghwCfg := cfg.GitHubWorkflow
w := newWorkflow("goreleaser", ghwCfg.Global.DefaultBranch, nil)

if w.deleteIf(cfg.Release.Enabled) {
if w.deleteIf(ghwCfg.Release.Enabled) {
return
}

Expand All @@ -30,7 +31,7 @@ func releaseWorkflow(cfg *core.GithubWorkflowConfiguration) {
w.On.PullRequest.Branches = nil
w.On.Push.Tags = []string{"*"} // goreleaser uses semver to decide if this is a prerelease or not

j := baseJobWithGo("goreleaser", cfg.IsSelfHostedRunner, cfg.Global.GoVersion)
j := baseJobWithGo("goreleaser", cfg)
// This is needed because: https://goreleaser.com/ci/actions/#fetch-depthness
j.Steps[0].With = map[string]any{
"fetch-depth": 0,
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ func main() {

if cfg.Golang.SetGoModVersion {
modFileBytes := must.Return(os.ReadFile(core.ModFilename))
rgx := regexp.MustCompile(`go \d\.\d\d`)
modFileBytesReplaced := rgx.ReplaceAll(modFileBytes, []byte("go "+core.DefaultGoVersion))
rgx := regexp.MustCompile(`go \d\.\d+(\.\d+)?`)
goVersionSlice := strings.Split(core.DefaultGoVersion, ".")
modFileBytesReplaced := rgx.ReplaceAll(modFileBytes, []byte("go "+strings.Join(goVersionSlice[:len(goVersionSlice)-1], ".")))
must.Succeed(os.WriteFile(core.ModFilename, modFileBytesReplaced, 0o666))
}

Expand Down Expand Up @@ -89,7 +90,7 @@ func main() {
}
cfg.GitHubWorkflow.Global.GoVersion = sr.GoVersion
}
ghworkflow.Render(&cfg)
ghworkflow.Render(cfg)
}

// Render Renovate config
Expand Down

0 comments on commit f0a1c35

Please sign in to comment.