Skip to content

Commit

Permalink
TEST/MAJOR: ci: use go version from go.mod file in gitlab
Browse files Browse the repository at this point in the history
  • Loading branch information
oktalz committed Aug 21, 2023
1 parent 6b04573 commit 5878ad7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 46 deletions.
55 changes: 55 additions & 0 deletions .gitlab-ci.template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
variables:
GO_VERSION: "from_go_mod_file"
stages:
- diff
- lint
- test
diff:
stage: diff
image:
name: $CI_REGISTRY_GO/golang:$GO_VERSION
entrypoint: [""]
tags:
- go
script:
- make generate
- git diff
- test -z "$(git diff 2> /dev/null)" || exit 'Files are not generated or formatted with gofumpt, issue `make generate` and commit the result'
- test -z "$(git ls-files --others --exclude-standard 2> /dev/null)" || exit 'Generation created untracked files, cannot proceed'
rules:
- if: '$CI_PIPELINE_SOURCE == "parent_pipeline"'
lint:
stage: lint
image:
name: $CI_REGISTRY_GO/golang:$GO_VERSION
entrypoint: [""]
tags:
- go
script:
- make lint
rules:
- if: '$CI_PIPELINE_SOURCE == "parent_pipeline"'
tidy:
stage: lint
image:
name: $CI_REGISTRY_GO/golang:$GO_VERSION
entrypoint: [""]
tags:
- go
script:
- go mod tidy
- test -z "$(git diff 2> /dev/null)" || exit 'Go modules not tidied, issue \`go mod tidy\` and commit the result'
rules:
- if: '$CI_PIPELINE_SOURCE == "parent_pipeline"'
test:
stage: test
image:
name: $CI_REGISTRY_GO/golang:$GO_VERSION
entrypoint: [""]
tags:
- go
script:
- go build -v .
- make test
rules:
- if: '$CI_PIPELINE_SOURCE == "parent_pipeline"'
69 changes: 23 additions & 46 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
variables:
GO_VERSION: "1.20"
stages:
- prepare
- diff
- lint
- test
diff:
stage: diff
image:
name: $CI_REGISTRY_GO/golang:$GO_VERSION
entrypoint: [""]
prepare-jobs:
image: $CI_REGISTRY_GO/bash:4.4
stage: prepare
tags:
- go
script:
- make generate
- git diff
- test -z "$(git diff 2> /dev/null)" || exit 'Files are not generated or formatted with gofumpt, issue `make generate` and commit the result'
- test -z "$(git ls-files --others --exclude-standard 2> /dev/null)" || exit 'Generation created untracked files, cannot proceed'
- awk '$1=="go" {print $2}' go.mod
- sed -i "s/from_go_mod_file/$(awk '$1=="go" {print $2}' go.mod)/g" .gitlab-ci.template.yml
- cat .gitlab-ci.template.yml
artifacts:
when: on_success
paths:
- .gitlab-ci.template.yml
expire_in: 1 hour
rules:
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
- if: $CI_PIPELINE_SOURCE == 'push'
lint:
stage: lint
image:
name: $CI_REGISTRY_GO/golang:$GO_VERSION
entrypoint: [""]
tags:
- go
script:
- make lint
generated:
variables:
PARENT_PIPELINE_ID: $CI_PIPELINE_ID
needs:
- "prepare-jobs"
stage: prepare
trigger:
include:
- artifact: .gitlab-ci.template.yml
job: prepare-jobs
strategy: depend
rules:
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
- if: $CI_PIPELINE_SOURCE == 'push'
Expand All @@ -42,29 +45,3 @@ lint-commit-msg:
- /check
rules:
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
tidy:
stage: lint
image:
name: $CI_REGISTRY_GO/golang:$GO_VERSION
entrypoint: [""]
tags:
- go
script:
- go mod tidy
- test -z "$(git diff 2> /dev/null)" || exit 'Go modules not tidied, issue \`go mod tidy\` and commit the result'
rules:
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
- if: $CI_PIPELINE_SOURCE == 'push'
test:
stage: test
image:
name: $CI_REGISTRY_GO/golang:$GO_VERSION
entrypoint: [""]
tags:
- go
script:
- go build -v .
- make test
rules:
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
- if: $CI_PIPELINE_SOURCE == 'push'

0 comments on commit 5878ad7

Please sign in to comment.