Skip to content
This repository has been archived by the owner on Sep 21, 2023. It is now read-only.

Buidkite pipeline #328

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions .buildkite/hooks/post-checkout
Original file line number Diff line number Diff line change
@@ -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 ""
8 changes: 8 additions & 0 deletions .buildkite/hooks/pre-command
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

set -euo pipefail

echo "Golang version:"
version=$(cat .go-version)
export GO_VERSION="${version}"
echo "${GO_VERSION}"
53 changes: 51 additions & 2 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions .buildkite/scripts/build.ps1
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions .buildkite/scripts/build.sh
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions .buildkite/scripts/check.sh
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions .buildkite/scripts/pre-install-command.ps1
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 26 additions & 0 deletions .buildkite/scripts/pre-install-command.sh
Original file line number Diff line number Diff line change
@@ -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)"}
20 changes: 20 additions & 0 deletions .buildkite/scripts/publish-snapshot.sh
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions .buildkite/scripts/test.ps1
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions .buildkite/scripts/test.sh
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions .buildkite/scripts/tooling.sh
Original file line number Diff line number Diff line change
@@ -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
}