Skip to content

Commit

Permalink
Merge branch 'test' into upgrade/cosmos46
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Junge authored Mar 28, 2023
2 parents bbe26d2 + 456e8fa commit d0e252f
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 43 deletions.
70 changes: 70 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: docker-build

on:
pull_request:
branches:
- main
- test
push:
branches:
- main
- test
tags:
- 'v*'

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout
uses: actions/checkout@v3

# https://docs.docker.com/build/ci/github-actions/multi-platform/
- name: Set up QEMU
uses: docker/setup-qemu-action@v2

# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v2

# # https://github.com/docker/login-action
# - name: Login to DockerHub
# uses: docker/login-action@v2
# with:
# username: ${{ secrets.DOCKER_USERNAME }}
# password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Log in to the github container registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
images: |
ghcr.io/${{ github.repository }}
# terramoney/core
tags: |
type=sha
type=edge,branch=test
type=semver,pattern={{tag}}
type=semver,pattern={{version}}
type=raw,value=latest,enable={{is_default_branch}}
- name: Build docker image
uses: docker/build-push-action@v3
with:
push: ${{ github.event_name != 'pull_request' }}
file: Dockerfile
platforms: linux/amd64,linux/arm64
tags: ${{ env.DOCKER_METADATA_OUTPUT_TAGS }}
labels: ${{ env.DOCKER_METADATA_OUTPUT_LABELS }}
58 changes: 58 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Release

on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+' # Push events to matching v*, i.e. v1.0, v20.15.10
- 'v[0-9]+.[0-9]+.[0-9]+-rc*' # Push events to matching v*, i.e. v1.0-rc1, v20.15.10-rc5

jobs:
release:
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
steps:
- name: Create release for ${{github.ref_name}}
run: gh release create ${{github.ref_name}} --prerelease --generate-notes --repo ${{github.repository}}

artifacts:
if: startsWith(github.ref, 'refs/tags/')
needs: release
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
strategy:
matrix:
build_type: ['build-release-arm64', 'build-release-amd64']
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set version tag
run: echo "VERSION=$(echo ${{github.ref_name}} | sed 's/^v//')" >> $GITHUB_ENV
- name: Create build directory
run: mkdir -p build/release
- name: Build ${{matrix.build_type}}
run: make ${{matrix.build_type}}
- name: Upload the artifacts to release
run: gh release upload ${{github.ref_name}} ./build/release/*

calculate-checksums:
needs: artifacts
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
steps:
- name: Create build directory
run: mkdir -p build/release
- name: Download artifacts
run: gh release download ${{github.ref_name}} --pattern '*.tar.gz' --dir build/release --repo ${{github.repository}}
- name: Create checksums
run: |
cd build/release
sha256sum *.tar.gz > checksum.txt
- name: Display checksums
run: cat build/release/checksum.txt
- name: Upload the checksum to release
run: gh release upload ${{github.ref_name}} build/release/checksum.txt --repo ${{github.repository}}
141 changes: 99 additions & 42 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,57 +1,114 @@
# docker build . -t cosmwasm/wasmd:latest
# docker run --rm -it cosmwasm/wasmd:latest /bin/sh
FROM golang:1.20-alpine3.17 AS go-builder
#ARG arch=x86_64
# syntax=docker/dockerfile:1

# See https://github.com/terra-money/wasmvm/releases
ENV LIBWASMVM_VERSION=v1.0.0
ARG GO_VERSION="1.20"
ARG ALPINE_VERSION="3.16"
ARG BUILDPLATFORM=linux/amd64
ARG BASE_IMAGE="golang:${GO_VERSION}-alpine${ALPINE_VERSION}"
FROM --platform=${BUILDPLATFORM} ${BASE_IMAGE} as base

# this comes from standard alpine nightly file
# https://github.com/rust-lang/docker-rust-nightly/blob/master/alpine3.12/Dockerfile
# with some changes to support our toolchain, etc
RUN set -eux; apk add --no-cache ca-certificates build-base;
###############################################################################
# Builder
###############################################################################

RUN apk add git cmake
# NOTE: add these to run with LEDGER_ENABLED=true
# RUN apk add libusb-dev linux-headers
FROM base as builder-stage-1

WORKDIR /code
COPY . /code/
ARG GIT_COMMIT
ARG GIT_VERSION
ARG BUILDPLATFORM
ARG GOOS=linux \
GOARCH=amd64

# Install mimalloc
RUN git clone --depth 1 https://github.com/microsoft/mimalloc; cd mimalloc; mkdir build; cd build; cmake ..; make -j$(nproc); make install
ENV MIMALLOC_RESERVE_HUGE_OS_PAGES=4
ENV GOOS=$GOOS \
GOARCH=$GOARCH

# See https://github.com/CosmWasm/wasmvm/releases
ADD https://github.com/CosmWasm/wasmvm/releases/download/${LIBWASMVM_VERSION}/libwasmvm_muslc.aarch64.a /lib/libwasmvm_muslc.aarch64.a
ADD https://github.com/CosmWasm/wasmvm/releases/download/${LIBWASMVM_VERSION}/libwasmvm_muslc.x86_64.a /lib/libwasmvm_muslc.x86_64.a
RUN sha256sum /lib/libwasmvm_muslc.aarch64.a | grep 7d2239e9f25e96d0d4daba982ce92367aacf0cbd95d2facb8442268f2b1cc1fc
RUN sha256sum /lib/libwasmvm_muslc.x86_64.a | grep f6282df732a13dec836cda1f399dd874b1e3163504dbd9607c6af915b2740479
# NOTE: add libusb-dev to run with LEDGER_ENABLED=true
RUN set -eux &&\
apk update &&\
apk add --no-cache \
ca-certificates \
linux-headers \
build-base \
cmake \
git

# Copy the library you want to the final location that will be found by the linker flag `-lwasmvm_muslc`
RUN cp /lib/libwasmvm_muslc.`uname -m`.a /lib/libwasmvm_muslc.a
# install mimalloc for musl
WORKDIR ${GOPATH}/src/mimalloc
RUN set -eux &&\
git clone --depth 1 https://github.com/microsoft/mimalloc . &&\
mkdir -p build &&\
cd build &&\
cmake .. &&\
make -j$(nproc) &&\
make install

# force it to use static lib (from above) not standard libgo_cosmwasm.so file
RUN LEDGER_ENABLED=false BUILD_TAGS=muslc LDFLAGS="-linkmode=external -extldflags \"-L/code/mimalloc/build -lmimalloc -Wl,-z,muldefs -static\"" make build

FROM alpine:3.15.4
# download dependencies to cache as layer
WORKDIR ${GOPATH}/src/app
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/root/go/pkg/mod \
go mod download -x

RUN addgroup terra \
&& adduser -G terra -D -h /terra terra
# Cosmwasm - Download correct libwasmvm version
RUN set -eux &&\
WASMVM_VERSION=$(go list -m github.com/CosmWasm/wasmvm | cut -d ' ' -f 2) && \
WASMVM_DOWNLOADS="https://github.com/CosmWasm/wasmvm/releases/download/${WASMVM_VERSION}"; \
wget ${WASMVM_DOWNLOADS}/checksums.txt -O /tmp/checksums.txt; \
if [ ${BUILDPLATFORM} = "linux/amd64" ]; then \
WASMVM_URL="${WASMVM_DOWNLOADS}/libwasmvm_muslc.x86_64.a"; \
# elif [ ${GOOS} = "darwin" ] -a [ ${GOARCH} = "arm64" ]; then \
# WASMVM_URL="${WASMVM_DOWNLOADS}/libwasmvm.dylib"; \
elif [ ${BUILDPLATFORM} = "linux/arm64" ]; then \
WASMVM_URL="${WASMVM_DOWNLOADS}/libwasmvm_muslc.aarch64.a"; \
else \
echo "Unsupported Build Platfrom ${BUILDPLATFORM}"; \
exit 1; \
fi; \
wget ${WASMVM_URL} -O /lib/libwasmvm_muslc.a; \
CHECKSUM=`sha256sum /lib/libwasmvm_muslc.a | cut -d" " -f1`; \
grep ${CHECKSUM} /tmp/checksums.txt; \
rm /tmp/checksums.txt

WORKDIR /terra
###############################################################################

COPY --from=go-builder /code/build/terrad /usr/local/bin/terrad
FROM builder-stage-1 as builder-stage-2

USER terra
ARG GOOS=linux \
GOARCH=amd64

# rest server
EXPOSE 1317
# grpc
EXPOSE 9090
# tendermint p2p
EXPOSE 26656
# tendermint rpc
EXPOSE 26657
ENV GOOS=$GOOS \
GOARCH=$GOARCH

CMD ["/usr/local/bin/terrad", "version"]
# Copy the remaining files
COPY . .

# Build app binary
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/root/go/pkg/mod \
go install \
-mod=readonly \
-tags "netgo,muslc" \
-ldflags " \
-w -s -linkmode=external -extldflags \
'-L/go/src/mimalloc/build -lmimalloc -Wl,-z,muldefs -static' \
-X github.com/cosmos/cosmos-sdk/version.Name='terrad' \
-X github.com/cosmos/cosmos-sdk/version.AppName='terrad' \
-X github.com/cosmos/cosmos-sdk/version.Version=${GIT_VERSION} \
-X github.com/cosmos/cosmos-sdk/version.Commit=${GIT_COMMIT} \
-X github.com/cosmos/cosmos-sdk/version.BuildTags='netgo,muslc' \
" \
-trimpath \
./...

################################################################################

FROM alpine:${ALPINE_VERSION} as terra-core

COPY --from=builder-stage-2 /go/bin/terrad /usr/local/bin/terrad

RUN addgroup -g 1000 terra && \
adduser -u 1000 -G terra -D -h /app terra

WORKDIR /app

CMD ["terrad", "--home", "/app", "start"]
44 changes: 43 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ LEDGER_ENABLED ?= true
BINDIR ?= $(GOPATH)/bin
BUILDDIR ?= $(CURDIR)/build
DOCKER := $(shell which docker)

SHA256_CMD = sha256sum
GO_VERSION ?= "1.20"
# don't override user values
ifeq (,$(VERSION))
VERSION := $(shell git describe --tags)
Expand Down Expand Up @@ -133,6 +134,47 @@ build-linux-with-shared-library:
docker cp temp:/lib/libwasmvm.so $(BUILDDIR)/
docker rm temp

build-release: build-release-amd64 build-release-arm64

build-release-amd64: go.sum $(BUILDDIR)/
$(DOCKER) buildx create --name core-builder || true
$(DOCKER) buildx use core-builder
$(DOCKER) buildx build \
--build-arg GO_VERSION=$(GO_VERSION) \
--build-arg GIT_VERSION=$(VERSION) \
--build-arg GIT_COMMIT=$(COMMIT) \
--build-arg BUILDPLATFORM=linux/amd64 \
--build-arg GOOS=linux \
--build-arg GOARCH=amd64 \
-t core:local-amd64 \
--load \
-f Dockerfile .
$(DOCKER) rm -f core-builder || true
$(DOCKER) create -ti --name core-builder core:local-amd64
$(DOCKER) cp core-builder:/usr/local/bin/terrad $(BUILDDIR)/release/terrad
tar -czvf $(BUILDDIR)/release/terra_$(VERSION)_Linux_x86_64.tar.gz -C $(BUILDDIR)/release/ terrad
rm $(BUILDDIR)/release/terrad
$(DOCKER) rm -f core-builder

build-release-arm64: go.sum $(BUILDDIR)/
$(DOCKER) buildx create --name core-builder || true
$(DOCKER) buildx use core-builder
$(DOCKER) buildx build \
--build-arg GO_VERSION=$(GO_VERSION) \
--build-arg GIT_VERSION=$(VERSION) \
--build-arg GIT_COMMIT=$(COMMIT) \
--build-arg BUILDPLATFORM=linux/arm64 \
--build-arg GOOS=linux \
--build-arg GOARCH=arm64 \
-t core:local-arm64 \
--load \
-f Dockerfile .
$(DOCKER) rm -f core-builder || true
$(DOCKER) create -ti --name core-builder core:local-arm64
$(DOCKER) cp core-builder:/usr/local/bin/terrad $(BUILDDIR)/release/terrad
tar -czvf $(BUILDDIR)/release/terra_$(VERSION)_Linux_arm64.tar.gz -C $(BUILDDIR)/release/ terrad
rm $(BUILDDIR)/release/terrad
$(DOCKER) rm -f core-builder
install: go.sum
go install -mod=readonly $(BUILD_FLAGS) ./cmd/terrad

Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ If you are interested in contributing to the Terra Core source code, please revi
<br />
<br />
© 2023 Terraform Labs, PTE LTD

<br />
<a href="LICENSE">This software is licensed under the Apache 2.0 license.</a>
</p>

0 comments on commit d0e252f

Please sign in to comment.