mirror of
https://github.com/TECHNOFAB11/dbmate.git
synced 2025-12-12 08:00:04 +01:00
Cross-compile macOS and Windows builds (#89)
Adds support for cross-compiling macOS and Windows builds with cgo (using https://github.com/techknowlogick/xgo). Closes #41
This commit is contained in:
parent
23d48e48aa
commit
aa72a39a29
4 changed files with 46 additions and 22 deletions
29
Dockerfile
29
Dockerfile
|
|
@ -1,27 +1,34 @@
|
||||||
# build image
|
# build image
|
||||||
FROM golang:1.12-stretch as build
|
FROM techknowlogick/xgo:go-1.13.x as build
|
||||||
|
WORKDIR /src
|
||||||
|
ENTRYPOINT []
|
||||||
|
CMD ["/bin/bash"]
|
||||||
|
|
||||||
# required to force cgo (for sqlite driver) with cross compile
|
# enable cgo to build sqlite
|
||||||
ENV CGO_ENABLED 1
|
ENV CGO_ENABLED 1
|
||||||
|
|
||||||
# install database clients
|
# install database clients
|
||||||
RUN apt-get update \
|
RUN apt-get update \
|
||||||
&& apt-get install -y --no-install-recommends \
|
&& apt-get install -qq --no-install-recommends \
|
||||||
default-mysql-client \
|
curl \
|
||||||
|
mysql-client \
|
||||||
postgresql-client \
|
postgresql-client \
|
||||||
sqlite3 \
|
sqlite3 \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# development dependencies
|
# golangci-lint
|
||||||
RUN curl -fsSL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh \
|
RUN curl -fsSL -o /tmp/lint-install.sh https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh \
|
||||||
| sh -s v1.16.0
|
&& chmod +x /tmp/lint-install.sh \
|
||||||
|
&& /tmp/lint-install.sh -b /usr/local/bin v1.18.0 \
|
||||||
|
&& rm -f /tmp/lint-install.sh
|
||||||
|
|
||||||
# copy source files
|
# download modules
|
||||||
COPY . /src
|
COPY go.* ./
|
||||||
WORKDIR /src
|
RUN go mod download
|
||||||
|
|
||||||
# build
|
# build
|
||||||
RUN make build
|
COPY . ./
|
||||||
|
RUN make build-linux
|
||||||
|
|
||||||
# runtime image
|
# runtime image
|
||||||
FROM gcr.io/distroless/base
|
FROM gcr.io/distroless/base
|
||||||
|
|
|
||||||
31
Makefile
31
Makefile
|
|
@ -1,5 +1,4 @@
|
||||||
DC := docker-compose
|
LDFLAGS := -ldflags '-s'
|
||||||
BUILD_FLAGS := -ldflags '-s'
|
|
||||||
|
|
||||||
.PHONY: all
|
.PHONY: all
|
||||||
all: test lint build
|
all: test lint build
|
||||||
|
|
@ -22,13 +21,27 @@ clean:
|
||||||
rm -rf dist
|
rm -rf dist
|
||||||
|
|
||||||
.PHONY: build
|
.PHONY: build
|
||||||
build: clean
|
build: clean build-linux build-macos build-windows
|
||||||
GOARCH=amd64 go build $(BUILD_FLAGS) -o dist/dbmate-linux-amd64 .
|
ls -lh dist
|
||||||
# musl target does not support sqlite
|
|
||||||
GOARCH=amd64 CGO_ENABLED=0 go build $(BUILD_FLAGS) -o dist/dbmate-linux-musl-amd64 .
|
.PHONY: build-linux
|
||||||
|
build-linux:
|
||||||
|
GOOS=linux GOARCH=amd64 CGO_ENABLED=1 \
|
||||||
|
go build $(LDFLAGS) -o dist/dbmate-linux-amd64 .
|
||||||
|
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 \
|
||||||
|
go build $(LDFLAGS) -o dist/dbmate-linux-musl-amd64 .
|
||||||
|
|
||||||
|
.PHONY: build-macos
|
||||||
|
build-macos:
|
||||||
|
GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 CC=o64-clang CXX=o64-clang++ \
|
||||||
|
go build $(LDFLAGS) -o dist/dbmate-macos-amd64 .
|
||||||
|
|
||||||
|
.PHONY: build-windows
|
||||||
|
build-windows:
|
||||||
|
GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc-posix CXX=x86_64-w64-mingw32-g++-posix \
|
||||||
|
go build $(LDFLAGS) -o dist/dbmate-windows-amd64.exe .
|
||||||
|
|
||||||
.PHONY: docker
|
.PHONY: docker
|
||||||
docker:
|
docker:
|
||||||
$(DC) pull
|
docker-compose build
|
||||||
$(DC) build
|
docker-compose run --rm dbmate make
|
||||||
$(DC) run --rm dbmate make
|
|
||||||
|
|
|
||||||
2
go.mod
2
go.mod
|
|
@ -1,5 +1,7 @@
|
||||||
module github.com/amacneil/dbmate
|
module github.com/amacneil/dbmate
|
||||||
|
|
||||||
|
go 1.13
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/go-sql-driver/mysql v1.4.1
|
github.com/go-sql-driver/mysql v1.4.1
|
||||||
|
|
|
||||||
|
|
@ -133,8 +133,10 @@ func TestMySQLDumpSchema(t *testing.T) {
|
||||||
u.Path = "/fakedb"
|
u.Path = "/fakedb"
|
||||||
schema, err = drv.DumpSchema(u, db)
|
schema, err = drv.DumpSchema(u, db)
|
||||||
require.Nil(t, schema)
|
require.Nil(t, schema)
|
||||||
require.EqualError(t, err, "mysqldump: Got error: 1049: "+
|
require.EqualError(t, err, "mysqldump: [Warning] Using a password "+
|
||||||
"\"Unknown database 'fakedb'\" when selecting the database")
|
"on the command line interface can be insecure.\n"+
|
||||||
|
"mysqldump: Got error: 1049: "+
|
||||||
|
"Unknown database 'fakedb' when selecting the database")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMySQLDatabaseExists(t *testing.T) {
|
func TestMySQLDatabaseExists(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue