From feea767e177a73099d398fb643b200943f75fad1 Mon Sep 17 00:00:00 2001 From: Ethan Reesor Date: Sun, 10 Sep 2023 11:59:28 -0500 Subject: [PATCH] CI --- .gitlab/all.gitlab-ci.yml | 22 +++++++++------- .gitlab/common.gitlab-ci.yml | 41 ++++++++++++++--------------- .gitlab/release.gitlab-ci.yml | 22 +++++++++++++--- scripts/images/ci-golang/Dockerfile | 3 +++ 4 files changed, 53 insertions(+), 35 deletions(-) create mode 100644 scripts/images/ci-golang/Dockerfile diff --git a/.gitlab/all.gitlab-ci.yml b/.gitlab/all.gitlab-ci.yml index 641745eaf..4a114e533 100644 --- a/.gitlab/all.gitlab-ci.yml +++ b/.gitlab/all.gitlab-ci.yml @@ -1,5 +1,4 @@ .build validation image: - stage: pre test extends: .rules all image: docker:20 services: [ docker:20-dind ] @@ -10,17 +9,19 @@ - docker push ${VALIDATION_IMAGE} go build: - stage: test extends: [ .rules all, .go ] - needs: [] + needs: + - job: build-image:golang + optional: true variables: GIT_SUBMODULE_STRATEGY: recursive script: go build -v ./... .go test: - stage: test extends: [ .rules all, .go ] - needs: [] + needs: + - job: build-image:golang + optional: true variables: GIT_SUBMODULE_STRATEGY: recursive artifacts: @@ -53,13 +54,15 @@ go test 1/2: - go run gotest.tools/gotestsum --junitfile report.xml --format testname -- -timeout 30m ./test/validate -short -test.validate.network "$PWD/.nodes/node-1/dnn/" -run TestValidateNetwork lint: - stage: test extends: [ .rules all, .go ] - needs: [] + needs: + - job: build-image:golang + optional: true variables: GIT_DEPTH: 0 before_script: - function die { echo -e "${BOLD_RED}${1}${NO_COLOR}"; false; } + - function check-diff { git diff --quiet "$@" && return 0; git --no-pager -c color.ui=always diff; return 1; } script: - echo -e "${SECTION}section_start:`date +%s`:lint\r${SECTION}Lint" - go run ./tools/cmd/golangci-lint run --verbose --timeout 20m @@ -67,12 +70,12 @@ lint: - echo -e "${SECTION}section_start:`date +%s`:tidy\r${SECTION}Verify go.mod is tidy" - go mod tidy - - git diff --quiet go.mod go.sum || die "Go mod files are not tidy. Please run \`go mod tidy\`." + - check-diff go.mod go.sum || die "Go mod files are not tidy. Please run \`go mod tidy\`." - echo -e "${SECTION}section_end:`date +%s`:tidy\r${SECTION}" - echo -e "${SECTION}section_start:`date +%s`:generate\r${SECTION}Verify generated files are up to date" - go generate -x ./... - - git diff --quiet || die "Generated files are not up to date. Please run \`go generate ./...\`." + - check-diff || die "Generated files are not up to date. Please run \`go generate ./...\`." - echo -e "${SECTION}section_end:`date +%s`:generate\r${SECTION}" - echo -e "${SECTION}section_start:`date +%s`:imports\r${SECTION}Verify code is correctly formatted" @@ -83,7 +86,6 @@ lint: .cleanup images: # Once validation is done, delete the images # Disabled because it doesn't appear to work, though it was copied from GitLab's docs - stage: post test extends: .rules all image: docker:20 services: [ docker:20-dind ] diff --git a/.gitlab/common.gitlab-ci.yml b/.gitlab/common.gitlab-ci.yml index 25998aa9f..d110a3cce 100644 --- a/.gitlab/common.gitlab-ci.yml +++ b/.gitlab/common.gitlab-ci.yml @@ -1,10 +1,5 @@ stages: -- pre test -- test -- post test -- pre deploy -- deploy -- post deploy + - test variables: BOLD_RED: '\e[1;31m' @@ -12,9 +7,27 @@ variables: SECTION: '\e[0K' PRODUCTION_IMAGE: ${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_SLUG} VALIDATION_IMAGE: ${CI_REGISTRY_IMAGE}/validation:${CI_COMMIT_REF_SLUG} + GO_CI_IMAGE: ${CI_REGISTRY_IMAGE}/ci-golang + +build-image:golang: + stage: test + image: docker:20 + needs: [] + rules: + - changes: + - .gitlab/**/* + - scripts/images/**/* + services: + - name: docker:20-dind + command: [ --experimental ] # Do we need this? + script: + - docker build -t ${GO_CI_IMAGE} scripts/images/ci-golang + - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY + - docker push ${GO_CI_IMAGE} .go: - image: golang:1.20 + stage: test + image: ${GO_CI_IMAGE} cache: key: go-mod-cache paths: @@ -23,20 +36,6 @@ variables: variables: GOMODCACHE: ${CI_PROJECT_DIR}/.go-mod-cache GOLANGCI_LINT_CACHE: ${CI_PROJECT_DIR}/.golangci-cache - before_script: - - | - function build-daemon { - export GOOS=$1 - export GOARCH=$2 - BIN=accumulated - [ -z "$GOOS" ] || BIN=${BIN}-${GOOS} - [ -z "$GOARCH" ] || BIN=${BIN}-${GOARCH} - [[ $GOOS == windows ]] && BIN=${BIN}.exe - export BUILDFLAGS="-o ${BIN}" - echo -e "${SECTION}section_start:`date +%s`:build_${GOOS}_${GOARCH}\r${SECTION}Build accumulated for $GOOS $GOARCH" - make GIT_COMMIT=${CI_COMMIT_SHA} GIT_DESCRIBE=${GIT_DESCRIBE} - echo -e "${SECTION}section_end:`date +%s`:build_${GOOS}_${GOARCH}\r${SECTION}"; - } .rules all: rules: diff --git a/.gitlab/release.gitlab-ci.yml b/.gitlab/release.gitlab-ci.yml index 4e21465a5..9e12d0354 100644 --- a/.gitlab/release.gitlab-ci.yml +++ b/.gitlab/release.gitlab-ci.yml @@ -3,7 +3,7 @@ - if: ($CI_COMMIT_BRANCH == 'release-1.0' || $CI_COMMIT_TAG != null) && $CI_PIPELINE_SOURCE != 'merge_request_event' git describe: - stage: pre deploy + stage: test extends: .rules all needs: [] script: @@ -14,7 +14,7 @@ git describe: dotenv: git.env docker buildx: - stage: pre deploy + stage: test extends: .rules release image: docker:20-git needs: [] @@ -31,7 +31,7 @@ docker buildx: - docker build --platform=local -o . ./docker-buildx build main image: - stage: pre deploy + stage: test extends: .rules release needs: [ docker buildx ] image: docker:20 @@ -49,12 +49,26 @@ build main image: - docker buildx build --platform linux/amd64,linux/arm64 --push -t ${PRODUCTION_IMAGE} . build binaries: - stage: pre deploy + stage: test extends: [ .rules release, .go ] needs: [ git describe ] # rules: # - if: ($CI_COMMIT_BRANCH != 'develop' && $CI_COMMIT_TAG == null) || $CI_PIPELINE_SOURCE == 'merge_request_event' # when: manual + before_script: + - | + function build-daemon { + export GOOS=$1 + export GOARCH=$2 + BIN=accumulated + [ -z "$GOOS" ] || BIN=${BIN}-${GOOS} + [ -z "$GOARCH" ] || BIN=${BIN}-${GOARCH} + [[ $GOOS == windows ]] && BIN=${BIN}.exe + export BUILDFLAGS="-o ${BIN}" + echo -e "${SECTION}section_start:`date +%s`:build_${GOOS}_${GOARCH}\r${SECTION}Build accumulated for $GOOS $GOARCH" + make GIT_COMMIT=${CI_COMMIT_SHA} GIT_DESCRIBE=${GIT_DESCRIBE} + echo -e "${SECTION}section_end:`date +%s`:build_${GOOS}_${GOARCH}\r${SECTION}"; + } script: - build-daemon linux amd64 - build-daemon linux arm64 diff --git a/scripts/images/ci-golang/Dockerfile b/scripts/images/ci-golang/Dockerfile new file mode 100644 index 000000000..34102c3f8 --- /dev/null +++ b/scripts/images/ci-golang/Dockerfile @@ -0,0 +1,3 @@ +FROM golang:1.20 + +RUN apt update -y && apt install -y git-lfs \ No newline at end of file