From bd10c37b9d96374411305bb61c5544fe6854bee5 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Wed, 14 Jul 2021 10:31:11 +0100 Subject: [PATCH] [CI] automate update-beats (#5692) --- .ci/scripts/update-beats.sh | 16 ++++++ .ci/update-beats.groovy | 104 ++++++++++++++++++++++++++++++++++++ .mergify.yml | 33 ++++++++++-- 3 files changed, 148 insertions(+), 5 deletions(-) create mode 100755 .ci/scripts/update-beats.sh create mode 100644 .ci/update-beats.groovy diff --git a/.ci/scripts/update-beats.sh b/.ci/scripts/update-beats.sh new file mode 100755 index 00000000000..5cfbefa61ae --- /dev/null +++ b/.ci/scripts/update-beats.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +# +# This script is executed by the automation we are putting in place +# and it requires the git add/commit commands. +# +set -euo pipefail + +make update-beats +COMMIT_MESSAGE="Update to elastic/beats@$(go list -m -f {{.Version}} github.com/elastic/beats/... | cut -d- -f3)" + +git checkout -b "update-beats-$(date "+%Y%m%d%H%M%S")" +git add go.mod go.sum NOTICE.txt +git diff --staged --quiet || git commit -m "$COMMIT_MESSAGE" +git --no-pager log -1 + +echo "You can now push and create a Pull Request" diff --git a/.ci/update-beats.groovy b/.ci/update-beats.groovy new file mode 100644 index 00000000000..164657a9bb3 --- /dev/null +++ b/.ci/update-beats.groovy @@ -0,0 +1,104 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// https://apm-ci.elastic.co/job/apm-server/job/update-json-schema-mbp/ + +@Library('apm@current') _ + +pipeline { + agent { label 'linux && immutable' } + environment { + REPO = 'apm-server' + BASE_DIR = "src/github.com/elastic/${env.REPO}" + HOME = "${env.WORKSPACE}" + NOTIFY_TO = credentials('notify-to') + JOB_GCS_BUCKET = credentials('gcs-bucket') + JOB_GIT_CREDENTIALS = "f6c7695a-671e-4f4f-a331-acdce44ff9ba" + PIPELINE_LOG_LEVEL = 'INFO' + } + triggers { + // Only master branch will run on a timer basis + cron(env.BRANCH_NAME == 'master' ? 'H H(4-5) * * 1,5' : '') + } + options { + timeout(time: 1, unit: 'HOURS') + buildDiscarder(logRotator(numToKeepStr: '5', artifactNumToKeepStr: '5')) + timestamps() + ansiColor('xterm') + disableResume() + durabilityHint('PERFORMANCE_OPTIMIZED') + } + parameters { + booleanParam(name: 'DRY_RUN_MODE', defaultValue: false, description: 'If true, allows to execute this pipeline in dry run mode, without sending a PR.') + } + stages { + stage('Checkout'){ + steps { + deleteDir() + gitCheckout(basedir: "${BASE_DIR}", repo: "git@github.com:elastic/${REPO}.git", credentialsId: "${JOB_GIT_CREDENTIALS}") + } + } + stage('Update beats') { + options { skipDefaultCheckout() } + steps { + dir("${BASE_DIR}"){ + withGoEnv(){ + setupAPMGitEmail(global: true) + sh(label: 'make update-beats', script: '.ci/scripts/update-beats.sh') + } + } + } + } + stage('Send Pull Request'){ + options { skipDefaultCheckout() } + steps { + dir("${BASE_DIR}"){ + createPullRequest() + } + } + } + } + post { + cleanup { + notifyBuildResult(prComment: false) + } + } +} + +def createPullRequest(Map args = [:]) { + def title = '[automation] update libbeat and beats packaging' + def message = createPRDescription() + def labels = "automation" + if (params.DRY_RUN_MODE) { + log(level: 'INFO', text: "DRY-RUN: createPullRequest(labels: ${labels}, message: '${message}')") + return + } + def branchName = (isPR()) ? env.CHANGE_TARGET : env.BRANCH_NAME + if (anyChangesToBeSubmitted("${branchName}")) { + githubCreatePullRequest(title: "${title}", labels: "${labels}", description: "${message}", base: "${branchName}") + } else { + log(level: 'INFO', text: "There are no changes to be submitted.") + } +} + +def anyChangesToBeSubmitted(String branch) { + return sh(returnStatus: true, script: "git diff --quiet HEAD..${branch}") != 0 +} + +def createPRDescription() { + return """### What \n Update with libbeat and beats packaging.""" +} diff --git a/.mergify.yml b/.mergify.yml index cddd2333ef7..47d4f83f81c 100644 --- a/.mergify.yml +++ b/.mergify.yml @@ -58,11 +58,34 @@ pull_request_rules: merge: method: squash strict: smart+fasttrack - - name: delete upstream branch after merging changes on docker-compose.yml + - name: delete upstream branch with changes on docker-compose.yml after merging/closing it conditions: - - merged - - label=automation - - head~=^update-stack-version - - files=docker-compose.yml + - or: + - merged + - closed + - and: + - label=automation + - head~=^update-.*-version + - files=docker-compose.yml actions: delete_head_branch: + - name: automatic merge when CI passes for the make update-beats + conditions: + - check-success=apm-ci/pr-merge + - label=automation + - base~=^update-beats + - files~=^(go.mod|go.sum|NOTICE.txt) + actions: + merge: + method: squash + strict: smart+fasttrack + - name: delete upstream branch after merging changes for the make update-beats + conditions: + - or: + - merged + - closed + - and: + - label=automation + - head~=^update-beats + actions: + delete_head_branch: \ No newline at end of file