diff --git a/.buildkite/hooks/post-checkout b/.buildkite/hooks/post-checkout new file mode 100644 index 0000000..e10f15d --- /dev/null +++ b/.buildkite/hooks/post-checkout @@ -0,0 +1,53 @@ +#!/bin/bash + +set -euo pipefail + +checkout_merge() { + local target_branch=$1 + local pr_commit=$2 + local merge_branch=$3 + + if [[ -z "${target_branch}" ]]; then + echo "No pull request target branch" + exit 1 + fi + + git fetch -v origin "${target_branch}" + git checkout FETCH_HEAD + echo "Current branch: $(git rev-parse --abbrev-ref HEAD)" + + # create temporal branch to merge the PR with the target branch + git checkout -b ${merge_branch} + echo "New branch created: $(git rev-parse --abbrev-ref HEAD)" + + # set author identity so it can be run git merge + git config user.name "github-merged-pr-post-checkout" + git config user.email "auto-merge@buildkite" + + git merge --no-edit "${BUILDKITE_COMMIT}" || { + local merge_result=$? + echo "Merge failed: ${merge_result}" + git merge --abort + exit ${merge_result} + } +} + +pull_request="${BUILDKITE_PULL_REQUEST:-false}" + +if [[ "${pull_request}" == "false" ]]; then + echo "Not a pull request, skipping" + exit 0 +fi + +TARGET_BRANCH="${BUILDKITE_PULL_REQUEST_BASE_BRANCH:-master}" +PR_COMMIT="${BUILDKITE_COMMIT}" +PR_ID=${BUILDKITE_PULL_REQUEST} +MERGE_BRANCH="pr_merge_${PR_ID}" + +checkout_merge "${TARGET_BRANCH}" "${PR_COMMIT}" "${MERGE_BRANCH}" + +echo "Commit information" +git --no-pager log --format=%B -n 1 + +# Ensure buildkite groups are rendered +echo "" diff --git a/.buildkite/hooks/pre-command b/.buildkite/hooks/pre-command new file mode 100644 index 0000000..56385d2 --- /dev/null +++ b/.buildkite/hooks/pre-command @@ -0,0 +1,8 @@ +#!/bin/bash + +set -euo pipefail + +echo "Golang version:" +version=$(cat .go-version) +export GO_VERSION="${version}" +echo "${GO_VERSION}" diff --git a/.buildkite/pipeline.yml b/.buildkite/pipeline.yml index 34321b6..c5b568f 100644 --- a/.buildkite/pipeline.yml +++ b/.buildkite/pipeline.yml @@ -1,5 +1,54 @@ # yaml-language-server: $schema=https://raw.githubusercontent.com/buildkite/pipeline-schema/main/schema.json +env: + SETUP_MAGE_VERSION: '1.14.0' + steps: - - label: "Example test" - command: echo "Hello!" + - label: ":buildkite: Check" + command: + - ".buildkite/scripts/check.sh" + agents: + image: golang:${GO_VERSION} + cpu: "8" + memory: "4G" + + - label: ":linux: Test" + env: + SNAPSHOT: true + command: + - ".buildkite/scripts/build.sh" + - ".buildkite/scripts/test.sh" + agents: + image: golang:${GO_VERSION} + cpu: "8" + memory: "4G" + + - label: ":win: Test" + env: + SNAPSHOT: true + command: + - ".buildkite/scripts/build.ps1" + - ".buildkite/scripts/test.ps1" + agents: + provider: "gcp" + image: "family/ci-windows-2019" + + - label: ":buildkite: Publish Snapshot" + env: + SNAPSHOT: true + command: + - ".buildkite/scripts/publish-snapshot.ps1" + agents: + image: golang:${GO_VERSION} + cpu: "8" + memory: "4G" + + - label: ":buildkite: Publish Release" + env: + SNAPSHOT: true # TODO should be false + - label: ":buildkite: DRA Snapshot" + env: + SNAPSHOT: true + - label: ":buildkite: DRA Staging" + env: + SNAPSHOT: true # TODO should be false diff --git a/.buildkite/scripts/build.ps1 b/.buildkite/scripts/build.ps1 new file mode 100644 index 0000000..8a5f513 --- /dev/null +++ b/.buildkite/scripts/build.ps1 @@ -0,0 +1,10 @@ +$ErrorActionPreference = "Stop" # set -e +. ($PSScriptRoot + "/pre-install-command.ps1") + +Write-Host "--- Setup" +fixCRLF +withGolang $env:GO_VERSION +withMage $env:SETUP_MAGE_VERSION + +Write-Host "--- Build" +mage -v build diff --git a/.buildkite/scripts/build.sh b/.buildkite/scripts/build.sh new file mode 100755 index 0000000..6c1f4af --- /dev/null +++ b/.buildkite/scripts/build.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +set -euo pipefail + +echo "--- Pre install" +source .buildkite/scripts/pre-install-command.sh +add_bin_path +with_mage + +echo "--- Build" +mage -v build diff --git a/.buildkite/scripts/check.sh b/.buildkite/scripts/check.sh new file mode 100755 index 0000000..a5ad75b --- /dev/null +++ b/.buildkite/scripts/check.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +set -euo pipefail + +echo "--- Pre install" +source .buildkite/scripts/pre-install-command.sh +add_bin_path +with_mage + +echo "--- Check" +mage -v notice +mage -v check diff --git a/.buildkite/scripts/pre-install-command.ps1 b/.buildkite/scripts/pre-install-command.ps1 new file mode 100644 index 0000000..2b1b223 --- /dev/null +++ b/.buildkite/scripts/pre-install-command.ps1 @@ -0,0 +1,25 @@ + +# Forcing to checkout again all the files with a correct autocrlf. +# Doing this here because we cannot set git clone options before. +function fixCRLF { + Write-Host "-- Fixing CRLF in git checkout --" + git config core.autocrlf input + git rm --quiet --cached -r . + git reset --quiet --hard +} + +function withGolang($version) { + Write-Host "-- Install golang --" + choco install -y golang --version $version + $env:ChocolateyInstall = Convert-Path "$((Get-Command choco).Path)\..\.." + Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1" + refreshenv + go version + go env +} + +function withMage($version) { + Write-Host "-- Install Mage --" + go mod download -x + go install github.com/magefile/mage@v$version +} diff --git a/.buildkite/scripts/pre-install-command.sh b/.buildkite/scripts/pre-install-command.sh new file mode 100755 index 0000000..7d7d568 --- /dev/null +++ b/.buildkite/scripts/pre-install-command.sh @@ -0,0 +1,26 @@ +#!/bin/bash +set -euo pipefail + +source .buildkite/scripts/tooling.sh + +add_bin_path(){ + mkdir -p "${WORKSPACE}/bin" + export PATH="${WORKSPACE}/bin:${PATH}" +} + +with_mage() { + mkdir -p "${WORKSPACE}/bin" + retry 5 curl -sL -o "${WORKSPACE}/bin/mage.tar.gz" "https://github.com/magefile/mage/releases/download/v${SETUP_MAGE_VERSION}/mage_${SETUP_MAGE_VERSION}_Linux-64bit.tar.gz" + + tar -xvf "${WORKSPACE}/bin/mage.tar.gz" -C "${WORKSPACE}/bin" + chmod +x "${WORKSPACE}/bin/mage" + mage --version +} + +with_go_junit_report() { + go install github.com/jstemmer/go-junit-report/v2@latest +} + +# Required env variables: +# WORKSPACE +WORKSPACE=${WORKSPACE:-"$(pwd)"} diff --git a/.buildkite/scripts/publish-snapshot.sh b/.buildkite/scripts/publish-snapshot.sh new file mode 100644 index 0000000..e223730 --- /dev/null +++ b/.buildkite/scripts/publish-snapshot.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +set -euo pipefail + +echo "--- Pre install" +source .buildkite/scripts/pre-install-command.sh +add_bin_path +with_mage + +echo "--- Package" +mage -v package:all + +echo "--- Publich artifacts" +# // Copy those files to another location with the sha commit to test them afterward. +# googleStorageUpload(bucket: getBucketLocation(args.type), +# credentialsId: "${JOB_GCS_CREDENTIALS}", +# pathPrefix: "${BASE_DIR}/build/distributions/", +# pattern: "${BASE_DIR}/build/distributions/**/*", +# sharedPublicly: true, +# showInline: true) diff --git a/.buildkite/scripts/test.ps1 b/.buildkite/scripts/test.ps1 new file mode 100644 index 0000000..b1eebec --- /dev/null +++ b/.buildkite/scripts/test.ps1 @@ -0,0 +1,10 @@ +$ErrorActionPreference = "Stop" # set -e +. ($PSScriptRoot + "/pre-install-command.ps1") + +Write-Host "--- Setup" +fixCRLF +withGolang $env:GO_VERSION +withMage $env:SETUP_MAGE_VERSION + +Write-Host "--- Test" +mage test > test-report-win.txt diff --git a/.buildkite/scripts/test.sh b/.buildkite/scripts/test.sh new file mode 100755 index 0000000..d2c4977 --- /dev/null +++ b/.buildkite/scripts/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +set -euo pipefail + +echo "--- Pre install" +source .buildkite/scripts/pre-install-command.sh +add_bin_path +with_mage + +echo "--- Test" +ls -l +mage test diff --git a/.buildkite/scripts/tooling.sh b/.buildkite/scripts/tooling.sh new file mode 100755 index 0000000..49dedab --- /dev/null +++ b/.buildkite/scripts/tooling.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +retry() { + local retries=$1 + shift + + local count=0 + until "$@"; do + exit=$? + wait=$((2 ** count)) + count=$((count + 1)) + if [ $count -lt "$retries" ]; then + >&2 echo "Retry $count/$retries exited $exit, retrying in $wait seconds..." + sleep $wait + else + >&2 echo "Retry $count/$retries exited $exit, no more retries left." + return $exit + fi + done + return 0 +}