Skip to content

Commit

Permalink
Introduce new building system (#83)
Browse files Browse the repository at this point in the history
* Introduce new building system

* remove replacement

* remove replacement

* linter

* Keep the working directory
  • Loading branch information
wojtek-coreum authored Jun 11, 2024
1 parent 20646ad commit 032b4f1
Show file tree
Hide file tree
Showing 15 changed files with 3,010 additions and 254 deletions.
60 changes: 13 additions & 47 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,17 @@ jobs:
]
include:
- ci_step: "lint"
command: "faucet-builder lint"
command: make lint
go-cache: true
linter-cache: true
docker-cache: false
- ci_step: "test"
command: "faucet-builder test"
command: make test
go-cache: true
linter-cache: false
docker-cache: false
- ci_step: "integration tests"
command: |
faucet-builder images
coreum-builder build images
crust znet start --profiles=faucet --timeout-commit 0.5s
faucet-builder integration-tests
crust znet stop
crust znet coverage-convert
command: make integration-tests
go-cache: true
linter-cache: false
docker-cache: true
Expand All @@ -49,61 +43,33 @@ jobs:
uses: actions/checkout@v4
with:
persist-credentials: false
path: faucet
- name: Checkout coreum
uses: actions/checkout@v4
with:
repository: CoreumFoundation/coreum
path: coreum
- name: Checkout crust
uses: actions/checkout@v4
with:
repository: CoreumFoundation/crust
path: crust
- name: Set up build system
run: |
echo "$(pwd)/faucet/bin" >> $GITHUB_PATH
echo "$(pwd)/coreum/bin" >> $GITHUB_PATH
echo "$(pwd)/crust/bin" >> $GITHUB_PATH
faucet/bin/faucet-builder build/me
- name: Retrieve go version
id: goversion
run: echo "GO_VERSION=$(faucet/bin/go version)" >> $GITHUB_OUTPUT
- name: Print go version
run: echo ${{ steps.goversion.outputs.GO_VERSION }}
- name: Setup go cache
uses: actions/cache@v4
if: ${{ matrix.go-cache }}
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}-${{ steps.goversion.outputs.GO_VERSION }}
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
- name: Setup crust cache
uses: actions/cache@v4
with:
path: |
~/.cache/crust
key: ${{ runner.os }}-crust-${{ hashFiles('build/go.sum') }}
- name: Setup linter cache
uses: actions/cache@v4
if: ${{ matrix.linter-cache }}
with:
path: ~/.cache/golangci-lint
key: ${{ runner.os }}-linter-cache-${{ steps.goversion.outputs.GO_VERSION }}
- name: Get Date
id: get-year-week
run: |
echo "date=$(/bin/date -u "+%Y-%U")" >> $GITHUB_OUTPUT
shell: bash
key: ${{ runner.os }}-go-linter
- name: Set docker cache
uses: ScribeMD/docker-cache@0.5.0
if: ${{ matrix.docker-cache }}
with:
key: ${{ runner.os }}-docker-cache-scribemd-${{ steps.get-year-week.outputs.date }} # year-week key
key: ${{ runner.os }}-docker-${{ hashFiles('build/go.sum') }}
- name: Prepare dependencies
# FIXME (wojciech): Execute `download` commands for crust and coreum builders once available
run: |
crust build/me
crust build
crust znet remove
coreum-builder build/me
faucet-builder build/me
faucet-builder download
run: make dependencies
- name: Run ${{ matrix.ci_step }}
run: ${{ matrix.command }}
- name: Dump docker logs on failure
Expand Down
41 changes: 41 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
BUILDER = ./bin/faucet-builder

.PHONY: znet-start
znet-start:
$(BUILDER) znet start --profiles=1cored,faucet

.PHONY: znet-remove
znet-remove:
$(BUILDER) znet remove

.PHONY: lint
lint:
$(BUILDER) lint

.PHONY: test
test:
$(BUILDER) test

.PHONY: build
build:
$(BUILDER) build

.PHONY: images
images:
$(BUILDER) images

.PHONY: release
release:
$(BUILDER) release

.PHONY: release-images
release-images:
$(BUILDER) release/images

.PHONY: dependencies
dependencies:
$(BUILDER) download

.PHONY: integration-tests
integration-tests:
$(BUILDER) integration-tests
23 changes: 20 additions & 3 deletions bin/faucet-builder
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,29 @@ if [ ! -f "$BUILD_BIN" ]; then
rm -f ./bin/.cache/build-*

pushd build > /dev/null
go build -trimpath -o "$BUILD_BIN" ./cmd
go build -trimpath -o "$BUILD_BIN" ./cmd/builder
popd > /dev/null

"$BUILD_BIN" build/me
fi

popd > /dev/null
case "$1" in

exec "$BUILD_BIN" "$@"
"znet")
MODULE="github.com/CoreumFoundation/crust"
VERSION_TMPL='{{ if or (.Replace) (eq .Version "") }}devel{{ else }}{{ .Version }}{{ end }}'
ZNET_VERSION=`go list -m -f "$VERSION_TMPL" "$MODULE"`
ZNET_BIN="$REPO/bin/.cache/znet-${ZNET_VERSION}"

if [ ! -f "$ZNET_BIN" ]; then
"$BUILD_BIN" build/znet
fi

shift 1
exec "$ZNET_BIN" "$@"
;;

*)
exec "$BUILD_BIN" "$@"
;;
esac
2 changes: 1 addition & 1 deletion build/cmd/main.go → build/cmd/builder/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/CoreumFoundation/coreum-tools/pkg/build"
"github.com/CoreumFoundation/crust/build"
selfBuild "github.com/CoreumFoundation/faucet/build"
)

Expand Down
7 changes: 7 additions & 0 deletions build/cmd/znet/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/CoreumFoundation/crust/pkg/znet"

func main() {
znet.Main()
}
66 changes: 41 additions & 25 deletions build/faucet/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,84 @@ package faucet

import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
"time"

"github.com/CoreumFoundation/coreum-tools/pkg/build"
"github.com/samber/lo"

"github.com/CoreumFoundation/coreum/build/coreum"
"github.com/CoreumFoundation/crust/build/golang"
"github.com/CoreumFoundation/crust/build/tools"
"github.com/CoreumFoundation/crust/build/types"
"github.com/CoreumFoundation/crust/infra"
"github.com/CoreumFoundation/crust/infra/apps"
"github.com/CoreumFoundation/crust/pkg/znet"
)

const (
repoPath = "."
binaryName = "faucet"
binaryPath = "bin/" + binaryName
goCoverFlag = "-cover"
tagsFlag = "-tags"
)

// Build builds faucet in docker.
func Build(ctx context.Context, deps build.DepsFunc) error {
func Build(ctx context.Context, deps types.DepsFunc) error {
return buildFaucet(ctx, deps, tools.TargetPlatformLinuxLocalArchInDocker, []string{goCoverFlag})
}

func buildFaucet(
ctx context.Context,
deps build.DepsFunc,
deps types.DepsFunc,
targetPlatform tools.TargetPlatform,
extraFlags []string,
) error {
binOutputPath := filepath.Join("bin", ".cache", binaryName, targetPlatform.String(), "bin", binaryName)

return golang.Build(ctx, deps, golang.BinaryBuildConfig{
TargetPlatform: targetPlatform,
PackagePath: repoPath,
PackagePath: "cmd",
BinOutputPath: binOutputPath,
Flags: extraFlags,
})
}

// RunIntegrationTests runs faucet integration tests.
func RunIntegrationTests(ctx context.Context, deps build.DepsFunc) error {
return golang.RunTests(ctx, deps, golang.TestConfig{
func RunIntegrationTests(ctx context.Context, deps types.DepsFunc) error {
deps(BuildDockerImage, coreum.BuildCoredLocally, coreum.BuildCoredDockerImage)

znetConfig := &infra.ConfigFactory{
EnvName: "znet",
TimeoutCommit: 500 * time.Millisecond,
HomeDir: filepath.Join(lo.Must(os.UserHomeDir()), ".crust", "znet"),
RootDir: ".",
Profiles: []string{apps.Profile1Cored, apps.ProfileFaucet},
}

if err := znet.Remove(ctx, znetConfig); err != nil {
return err
}
if err := znet.Start(ctx, znetConfig); err != nil {
return err
}
if err := golang.RunTests(ctx, deps, golang.TestConfig{
PackagePath: filepath.Join(repoPath, "integration-tests"),
Flags: []string{
tagsFlag + "=" + "integrationtests",
"-tags=integrationtests",
fmt.Sprintf("-parallel=%d", 2*runtime.NumCPU()),
"-timeout=1h",
},
})
}

// Tidy runs `go mod tidy` for faucet repo.
func Tidy(ctx context.Context, deps build.DepsFunc) error {
return golang.Tidy(ctx, repoPath, deps)
}

// Lint lints faucet repo.
func Lint(ctx context.Context, deps build.DepsFunc) error {
return golang.Lint(ctx, repoPath, deps)
}
}); err != nil {
return err
}

// Test run unit tests in faucet repo.
func Test(ctx context.Context, deps build.DepsFunc) error {
return golang.Test(ctx, repoPath, deps)
return znet.Remove(ctx, znetConfig)
}

// DownloadDependencies downloads go dependencies.
func DownloadDependencies(ctx context.Context, deps build.DepsFunc) error {
return golang.DownloadDependencies(ctx, repoPath, deps)
func DownloadDependencies(ctx context.Context, deps types.DepsFunc) error {
return golang.DownloadDependencies(ctx, deps, repoPath)
}
5 changes: 2 additions & 3 deletions build/faucet/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"path/filepath"

"github.com/CoreumFoundation/coreum-tools/pkg/build"
"github.com/CoreumFoundation/crust/build/config"
"github.com/CoreumFoundation/crust/build/docker"
"github.com/CoreumFoundation/crust/build/tools"
"github.com/CoreumFoundation/crust/build/types"
"github.com/CoreumFoundation/faucet/build/faucet/image"
)

Expand All @@ -19,7 +19,7 @@ type imageConfig struct {
}

// BuildDockerImage builds docker image of the faucet.
func BuildDockerImage(ctx context.Context, deps build.DepsFunc) error {
func BuildDockerImage(ctx context.Context, deps types.DepsFunc) error {
deps(Build)

return buildDockerImage(ctx, imageConfig{
Expand All @@ -39,7 +39,6 @@ func buildDockerImage(ctx context.Context, cfg imageConfig) error {
}

return docker.BuildImage(ctx, docker.BuildImageConfig{
RepoPath: repoPath,
ContextDir: filepath.Join("bin", ".cache", binaryName),
ImageName: binaryName,
TargetPlatforms: cfg.TargetPlatforms,
Expand Down
10 changes: 5 additions & 5 deletions build/faucet/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import (

"github.com/pkg/errors"

"github.com/CoreumFoundation/coreum-tools/pkg/build"
"github.com/CoreumFoundation/crust/build/config"
"github.com/CoreumFoundation/crust/build/docker"
"github.com/CoreumFoundation/crust/build/git"
"github.com/CoreumFoundation/crust/build/tools"
"github.com/CoreumFoundation/crust/build/types"
)

// Release releases faucet binary for amd64 and arm64 to be published inside the release.
func Release(ctx context.Context, deps build.DepsFunc) error {
clean, _, err := git.StatusClean(ctx, repoPath)
func Release(ctx context.Context, deps types.DepsFunc) error {
clean, _, err := git.StatusClean(ctx)
if err != nil {
return err
}
if !clean {
return errors.New("released commit contains uncommitted changes")
}

version, err := git.VersionFromTag(ctx, repoPath)
version, err := git.VersionFromTag(ctx)
if err != nil {
return err
}
Expand All @@ -37,7 +37,7 @@ func Release(ctx context.Context, deps build.DepsFunc) error {
}

// ReleaseImage releases faucet docker images for amd64 and arm64.
func ReleaseImage(ctx context.Context, deps build.DepsFunc) error {
func ReleaseImage(ctx context.Context, deps types.DepsFunc) error {
deps(Release)

return buildDockerImage(ctx, imageConfig{
Expand Down
Loading

0 comments on commit 032b4f1

Please sign in to comment.