From d4a91af2bc5b470d8708af0be6cbb71a2cd2a24f Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Fri, 12 Jun 2020 16:13:14 +0100 Subject: [PATCH 01/11] [CI]: CI pipeline --- Jenkinsfile | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ Makefile.common | 2 +- fpm/Makefile | 2 +- 3 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 00000000..6d8ef1f1 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,89 @@ + +@Library('apm@current') _ + +pipeline { + agent { label 'linux && immutable' } + environment { + REPO = 'golang-crossbuild' + BASE_DIR = "src/github.com/elastic/${env.REPO}" + NOTIFY_TO = credentials('notify-to') + PIPELINE_LOG_LEVEL = 'INFO' + PATH = "${env.PATH}:${env.WORKSPACE}/bin" + HOME = "${env.WORKSPACE}" + DOCKER_REGISTRY_SECRET = 'secret/apm-team/ci/docker-registry/prod' + REGISTRY = 'docker.elastic.co' + STAGING_IMAGE = "${env.REGISTRY}/observability-ci" + } + options { + timeout(time: 1, unit: 'HOURS') + buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '20', daysToKeepStr: '30')) + timestamps() + ansiColor('xterm') + disableResume() + durabilityHint('PERFORMANCE_OPTIMIZED') + rateLimitBuilds(throttle: [count: 60, durationName: 'hour', userBoost: true]) + quietPeriod(10) + } + triggers { + issueCommentTrigger('(?i).*jenkins\\W+run\\W+(?:the\\W+)?tests(?:\\W+please)?.*') + } + stages { + stage('Checkout') { + steps { + deleteDir() + gitCheckout(basedir: BASE_DIR) + stash allowEmpty: true, name: 'source', useDefaultExcludes: false + } + } + stage('Build') { + steps { + withGithubNotify(context: 'Build') { + deleteDir() + unstash 'source' + dir(BASE_DIR){ + sh 'make build' + } + } + } + } + stage('Staging') { + steps { + withGithubNotify(context: 'Staging') { + deleteDir() + unstash 'source' + dir(BASE_DIR){ + dockerLogin(secret: "${DOCKER_REGISTRY_SECRET}", registry: "${REGISTRY}") + sh(label: "push docker image to ${env.STAGING_IMAGE}/${env.REPO_NAME}", + script: "REPOSITORY=${env.STAGING_IMAGE}/${env.REPO_NAME} VERSION==${env.BRANCH_NAME} make push") + } + } + } + } + stage('Release') { + when { + anyOf { + tag pattern: 'v\\d+\\.\\d+.*', comparator: 'REGEXP' + } + } + stages { + stage('Publish') { + steps { + withGithubNotify(context: 'Publish') { + deleteDir() + unstash 'source' + dir(BASE_DIR){ + dockerLogin(secret: "${DOCKER_REGISTRY_SECRET}", registry: "${REGISTRY}") + sh 'make push' + } + } + } + } + } + } + } + post { + always { + notifyBuildResult() + } + } +} \ No newline at end of file diff --git a/Makefile.common b/Makefile.common index ce1e363e..ec4cee74 100644 --- a/Makefile.common +++ b/Makefile.common @@ -1,4 +1,4 @@ -REPOSITORY := docker.elastic.co/beats-dev +REPOSITORY ?= docker.elastic.co/beats-dev VCS_REF := $(shell git rev-parse HEAD) VCS_URL := https://github.com/elastic/golang-crossbuild BUILD_DATE := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") diff --git a/fpm/Makefile b/fpm/Makefile index 4a79ad91..f967ed6a 100644 --- a/fpm/Makefile +++ b/fpm/Makefile @@ -1,7 +1,7 @@ include ../Makefile.common NAME := fpm -VERSION := 1.11.0 +VERSION ?= 1.11.0 build: @echo ">> Building $(REPOSITORY)/$(NAME):$(VERSION)$(SUFFIX)" From 596d9f96930fc5aa434cf2995ba90757e5fbcbff Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Fri, 12 Jun 2020 16:30:15 +0100 Subject: [PATCH 02/11] ci: reuse workspace for pushing and refactor variables --- Jenkinsfile | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 6d8ef1f1..0f9a96c7 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -8,14 +8,13 @@ pipeline { BASE_DIR = "src/github.com/elastic/${env.REPO}" NOTIFY_TO = credentials('notify-to') PIPELINE_LOG_LEVEL = 'INFO' - PATH = "${env.PATH}:${env.WORKSPACE}/bin" HOME = "${env.WORKSPACE}" DOCKER_REGISTRY_SECRET = 'secret/apm-team/ci/docker-registry/prod' REGISTRY = 'docker.elastic.co' STAGING_IMAGE = "${env.REGISTRY}/observability-ci" } options { - timeout(time: 1, unit: 'HOURS') + timeout(time: 2, unit: 'HOURS') buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '20', daysToKeepStr: '30')) timestamps() ansiColor('xterm') @@ -49,12 +48,10 @@ pipeline { stage('Staging') { steps { withGithubNotify(context: 'Staging') { - deleteDir() - unstash 'source' dir(BASE_DIR){ dockerLogin(secret: "${DOCKER_REGISTRY_SECRET}", registry: "${REGISTRY}") - sh(label: "push docker image to ${env.STAGING_IMAGE}/${env.REPO_NAME}", - script: "REPOSITORY=${env.STAGING_IMAGE}/${env.REPO_NAME} VERSION==${env.BRANCH_NAME} make push") + sh(label: "push docker image to ${env.STAGING_IMAGE}/${env.REPO}", + script: "REPOSITORY=${env.STAGING_IMAGE}/${env.REPO} VERSION=${env.BRANCH_NAME} make push") } } } @@ -69,8 +66,6 @@ pipeline { stage('Publish') { steps { withGithubNotify(context: 'Publish') { - deleteDir() - unstash 'source' dir(BASE_DIR){ dockerLogin(secret: "${DOCKER_REGISTRY_SECRET}", registry: "${REGISTRY}") sh 'make push' From 544c61052e09d70385be9da2dfe61efae8ca5e6c Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Fri, 12 Jun 2020 17:14:32 +0100 Subject: [PATCH 03/11] ci: VERSION is not possible to change at the moment --- Jenkinsfile | 8 ++++++-- fpm/Makefile | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 0f9a96c7..182b4d84 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -50,8 +50,12 @@ pipeline { withGithubNotify(context: 'Staging') { dir(BASE_DIR){ dockerLogin(secret: "${DOCKER_REGISTRY_SECRET}", registry: "${REGISTRY}") - sh(label: "push docker image to ${env.STAGING_IMAGE}/${env.REPO}", - script: "REPOSITORY=${env.STAGING_IMAGE}/${env.REPO} VERSION=${env.BRANCH_NAME} make push") + withEnv( [ "REPOSITORY=${env.STAGING_IMAGE}" ] ) { + // It will use the already cached docker images that were created in the + // Build stage. But it's required to retag them with the staging repo. + sh 'make build' + sh(label: "push docker image to ${env.REPOSITORY}", script: 'make push') + } } } } diff --git a/fpm/Makefile b/fpm/Makefile index f967ed6a..4a79ad91 100644 --- a/fpm/Makefile +++ b/fpm/Makefile @@ -1,7 +1,7 @@ include ../Makefile.common NAME := fpm -VERSION ?= 1.11.0 +VERSION := 1.11.0 build: @echo ">> Building $(REPOSITORY)/$(NAME):$(VERSION)$(SUFFIX)" From cd3263813ed9cb098c4721a3e7d0ba7e5ac9c65d Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Mon, 15 Jun 2020 10:52:25 +0100 Subject: [PATCH 04/11] Apply suggestions from code review Co-authored-by: cachedout --- Jenkinsfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 182b4d84..04e63afd 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -31,7 +31,7 @@ pipeline { steps { deleteDir() gitCheckout(basedir: BASE_DIR) - stash allowEmpty: true, name: 'source', useDefaultExcludes: false + stash name: 'source', useDefaultExcludes: false } } stage('Build') { @@ -85,4 +85,4 @@ pipeline { notifyBuildResult() } } -} \ No newline at end of file +} From c5cc23429827378a199e9989d1955d1bb6ebd9be Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Tue, 16 Jun 2020 15:10:46 +0100 Subject: [PATCH 05/11] Publish automatically when there is build on master --- Jenkinsfile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 182b4d84..1f8001b0 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -62,9 +62,7 @@ pipeline { } stage('Release') { when { - anyOf { - tag pattern: 'v\\d+\\.\\d+.*', comparator: 'REGEXP' - } + branch 'master' } stages { stage('Publish') { From b643ff7c02ea8808548f0d410a76d44cdf0d8c53 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Thu, 18 Jun 2020 11:36:45 +0100 Subject: [PATCH 06/11] ci: install go --- Jenkinsfile | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index fad832b3..6e540eec 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -12,6 +12,7 @@ pipeline { DOCKER_REGISTRY_SECRET = 'secret/apm-team/ci/docker-registry/prod' REGISTRY = 'docker.elastic.co' STAGING_IMAGE = "${env.REGISTRY}/observability-ci" + GO_VERSION = '1.14.2' } options { timeout(time: 2, unit: 'HOURS') @@ -39,8 +40,10 @@ pipeline { withGithubNotify(context: 'Build') { deleteDir() unstash 'source' - dir(BASE_DIR){ - sh 'make build' + withGoEnv(){ + dir(BASE_DIR){ + sh 'make build' + } } } } @@ -48,13 +51,15 @@ pipeline { stage('Staging') { steps { withGithubNotify(context: 'Staging') { - dir(BASE_DIR){ - dockerLogin(secret: "${DOCKER_REGISTRY_SECRET}", registry: "${REGISTRY}") - withEnv( [ "REPOSITORY=${env.STAGING_IMAGE}" ] ) { - // It will use the already cached docker images that were created in the - // Build stage. But it's required to retag them with the staging repo. - sh 'make build' - sh(label: "push docker image to ${env.REPOSITORY}", script: 'make push') + withGoEnv(){ + dir(BASE_DIR){ + dockerLogin(secret: "${DOCKER_REGISTRY_SECRET}", registry: "${REGISTRY}") + withEnv( [ "REPOSITORY=${env.STAGING_IMAGE}" ] ) { + // It will use the already cached docker images that were created in the + // Build stage. But it's required to retag them with the staging repo. + sh 'make build' + sh(label: "push docker image to ${env.REPOSITORY}", script: 'make push') + } } } } @@ -68,9 +73,11 @@ pipeline { stage('Publish') { steps { withGithubNotify(context: 'Publish') { - dir(BASE_DIR){ - dockerLogin(secret: "${DOCKER_REGISTRY_SECRET}", registry: "${REGISTRY}") - sh 'make push' + withGoEnv(){ + dir(BASE_DIR){ + dockerLogin(secret: "${DOCKER_REGISTRY_SECRET}", registry: "${REGISTRY}") + sh 'make push' + } } } } From 0fa39bca812f352df3c36fc4820cf1b5c4c0f6a5 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Thu, 18 Jun 2020 11:38:28 +0100 Subject: [PATCH 07/11] ci: more declarative based with the environment --- Jenkinsfile | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 6e540eec..2171ec37 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -49,17 +49,18 @@ pipeline { } } stage('Staging') { + environment { + REPOSITORY = "${env.STAGING_IMAGE}" + } steps { withGithubNotify(context: 'Staging') { withGoEnv(){ dir(BASE_DIR){ dockerLogin(secret: "${DOCKER_REGISTRY_SECRET}", registry: "${REGISTRY}") - withEnv( [ "REPOSITORY=${env.STAGING_IMAGE}" ] ) { - // It will use the already cached docker images that were created in the - // Build stage. But it's required to retag them with the staging repo. - sh 'make build' - sh(label: "push docker image to ${env.REPOSITORY}", script: 'make push') - } + // It will use the already cached docker images that were created in the + // Build stage. But it's required to retag them with the staging repo. + sh 'make build' + sh(label: "push docker image to ${env.REPOSITORY}", script: 'make push') } } } From e3f95cebe1347febc95de5a39cc26473143f0753 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Thu, 18 Jun 2020 11:42:56 +0100 Subject: [PATCH 08/11] ci: use ubuntu label that matches the ubuntu-16 version --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 2171ec37..bd749476 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,7 +2,7 @@ @Library('apm@current') _ pipeline { - agent { label 'linux && immutable' } + agent { label 'ubuntu && immutable' } environment { REPO = 'golang-crossbuild' BASE_DIR = "src/github.com/elastic/${env.REPO}" From 4ae0dd0b6fcfae7c1ce3ca4b1d66baed789e0418 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Thu, 18 Jun 2020 12:35:03 +0100 Subject: [PATCH 09/11] ci: use the right credentials --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index bd749476..f69e630d 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -9,7 +9,7 @@ pipeline { NOTIFY_TO = credentials('notify-to') PIPELINE_LOG_LEVEL = 'INFO' HOME = "${env.WORKSPACE}" - DOCKER_REGISTRY_SECRET = 'secret/apm-team/ci/docker-registry/prod' + DOCKER_REGISTRY_SECRET = 'secret/observability-team/ci/docker-registry/prod' REGISTRY = 'docker.elastic.co' STAGING_IMAGE = "${env.REGISTRY}/observability-ci" GO_VERSION = '1.14.2' From a4faae5c2d8a159fd7951e0c903ae5311045ac57 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Tue, 23 Jun 2020 11:04:25 +0100 Subject: [PATCH 10/11] [ci] refactor --- Jenkinsfile | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index f69e630d..9bdfa096 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -7,8 +7,8 @@ pipeline { REPO = 'golang-crossbuild' BASE_DIR = "src/github.com/elastic/${env.REPO}" NOTIFY_TO = credentials('notify-to') - PIPELINE_LOG_LEVEL = 'INFO' HOME = "${env.WORKSPACE}" + PIPELINE_LOG_LEVEL = 'INFO' DOCKER_REGISTRY_SECRET = 'secret/observability-team/ci/docker-registry/prod' REGISTRY = 'docker.elastic.co' STAGING_IMAGE = "${env.REGISTRY}/observability-ci" @@ -40,11 +40,7 @@ pipeline { withGithubNotify(context: 'Build') { deleteDir() unstash 'source' - withGoEnv(){ - dir(BASE_DIR){ - sh 'make build' - } - } + buildImages() } } } @@ -54,15 +50,10 @@ pipeline { } steps { withGithubNotify(context: 'Staging') { - withGoEnv(){ - dir(BASE_DIR){ - dockerLogin(secret: "${DOCKER_REGISTRY_SECRET}", registry: "${REGISTRY}") - // It will use the already cached docker images that were created in the - // Build stage. But it's required to retag them with the staging repo. - sh 'make build' - sh(label: "push docker image to ${env.REPOSITORY}", script: 'make push') - } - } + // It will use the already cached docker images that were created in the + // Build stage. But it's required to retag them with the staging repo. + buildImages() + publishImages() } } } @@ -74,12 +65,7 @@ pipeline { stage('Publish') { steps { withGithubNotify(context: 'Publish') { - withGoEnv(){ - dir(BASE_DIR){ - dockerLogin(secret: "${DOCKER_REGISTRY_SECRET}", registry: "${REGISTRY}") - sh 'make push' - } - } + publishImages() } } } @@ -92,3 +78,18 @@ pipeline { } } } + +def buildImages(){ + withGoEnv(){ + dir("${env.BASE_DIR}"){ + sh 'make build' + } + } +} + +def publishImages(){ + dockerLogin(secret: "${env.DOCKER_REGISTRY_SECRET}", registry: "${env.REGISTRY}") + dir("${env.BASE_DIR}"){ + sh(label: "push docker image to ${env.REPOSITORY}", script: 'make push') + } +} From ead3be24997023206d6f0c35ae25df389013603a Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Tue, 23 Jun 2020 12:19:08 +0100 Subject: [PATCH 11/11] push.* is not required --- Makefile.common | 3 +-- README.md | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Makefile.common b/Makefile.common index ec4cee74..f96da809 100644 --- a/Makefile.common +++ b/Makefile.common @@ -7,7 +7,6 @@ BUILD_DATE := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") # Requires login at https://docker.elastic.co:7000/. push: echo ">> Pushing $(REPOSITORY)/$(NAME):$(VERSION)$(SUFFIX)$(TAG_EXTENSION)" - @docker tag "$(REPOSITORY)/$(NAME):$(VERSION)$(SUFFIX)$(TAG_EXTENSION)" "push.$(REPOSITORY)/$(NAME):$(VERSION)$(SUFFIX)$(TAG_EXTENSION)" - @docker push "push.$(REPOSITORY)/$(NAME):$(VERSION)$(SUFFIX)$(TAG_EXTENSION)" + @docker push "$(REPOSITORY)/$(NAME):$(VERSION)$(SUFFIX)$(TAG_EXTENSION)" .PHONY: push diff --git a/README.md b/README.md index e3eccddd..bb17526a 100644 --- a/README.md +++ b/README.md @@ -53,5 +53,4 @@ GOARM, PLATFORM_ID, CC, and CXX. 1. Commit the changes. `git add -u && git commit -m 'Update to Go 1.x.y'`. 1. Build the images from the project's root with `make`. 1. Get a logon token for the container registry by visiting . - In the provided login command change `docker.elastic.co` to `push.docker.elastic.co`. 1. Publish the images with `make push`.