Build using native OS workers (#231)

This commit is contained in:
Adrian Macneil 2021-12-19 21:08:22 -08:00 committed by GitHub
parent 06d8bb7567
commit f69f1dea03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 123 additions and 96 deletions

View file

@ -3,44 +3,94 @@ name: CI
on:
push:
branches: [main]
tags: "v*"
tags: "*"
pull_request:
branches: [main]
jobs:
build:
name: Build
strategy:
fail-fast: false
matrix:
include:
- os: linux
image: ubuntu-latest
arch: amd64
env: {}
- os: linux
image: ubuntu-latest
arch: arm64
setup: sudo apt-get update && sudo apt-get install -qq gcc-aarch64-linux-gnu
env:
CC: aarch64-linux-gnu-gcc
CXX: aarch64-linux-gnu-g++
- os: macos
image: macos-latest
arch: amd64
env: {}
- os: macos
image: macos-latest
arch: arm64
env: {}
- os: windows
image: windows-latest
arch: amd64
env: {}
name: Build (${{ matrix.os }}/${{ matrix.arch }})
runs-on: ${{ matrix.image }}
env: ${{ matrix.env }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: "1.17"
- name: Setup environment
run: ${{ matrix.setup }}
- run: go mod download
- run: make build ls
env:
GOARCH: ${{ matrix.arch }}
OUTPUT: dbmate-${{ matrix.os }}-${{ matrix.arch }}
- run: dist/dbmate-${{ matrix.os }}-${{ matrix.arch }} --help
if: ${{ matrix.arch == 'amd64' }}
- name: Publish binaries
uses: softprops/action-gh-release@v1
if: ${{ startsWith(github.ref, 'refs/tags/v') }}
with:
files: dist/dbmate-*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
docker:
name: Docker Test (linux/amd64)
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: actions/checkout@v2
- name: Environment
- name: Check docker environment
run: |
set -x
docker version
docker-compose version
- name: Cache
uses: actions/cache@v2
with:
key: cache
path: .cache
- name: Build docker image
run: |
set -x
docker-compose build
docker-compose run --rm --no-deps dbmate --version
- name: Build binaries
run: |
set -x
docker-compose run --rm --no-deps dev make build-all
dist/dbmate-linux-amd64 --version
- name: Run make build
run: docker-compose run --rm --no-deps dev make build ls
- name: Lint
- name: Run make lint
run: docker-compose run --rm --no-deps dev make lint
- name: Start test dependencies
@ -48,12 +98,10 @@ jobs:
set -x
docker-compose pull --quiet
docker-compose up --detach
- name: Run tests
run: |
set -x
docker-compose run --rm dev make wait
docker-compose run --rm dev make test
- name: Run make test
run: docker-compose run --rm dev make test
- name: Publish docker image
if: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v') }}
@ -65,12 +113,4 @@ jobs:
GHCR_IMAGE: ghcr.io/${{ github.repository }}
GHCR_USERNAME: ${{ github.actor }}
GHCR_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: .github/workflows/publish-docker.sh
- name: Publish release binaries
uses: softprops/action-gh-release@v1
if: ${{ startsWith(github.ref, 'refs/tags/v') }}
with:
files: dist/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ci/publish-docker.sh

View file

@ -1,16 +1,13 @@
# development image
FROM techknowlogick/xgo:go-1.16.x as dev
FROM golang:1.17 as dev
WORKDIR /src
ENV GOCACHE /src/.cache/go-build
# enable cgo to build sqlite
ENV CGO_ENABLED 1
# install database clients
RUN apt-get update \
&& apt-get install -qq --no-install-recommends \
curl \
mysql-client \
file \
mariadb-client \
postgresql-client \
sqlite3 \
&& rm -rf /var/lib/apt/lists/*
@ -20,15 +17,9 @@ RUN curl -fsSL https://raw.githubusercontent.com/golangci/golangci-lint/master/i
| sh -s -- -b /usr/local/bin v1.39.0
# download modules
COPY go.* ./
COPY go.* /src/
RUN go mod download
ENTRYPOINT []
CMD ["/bin/bash"]
# build stage
FROM dev as build
COPY . ./
COPY . /src/
RUN make build
# release stage
@ -38,5 +29,5 @@ RUN apk add --no-cache \
postgresql-client \
sqlite \
tzdata
COPY --from=build /src/dist/dbmate-linux-amd64 /usr/local/bin/dbmate
ENTRYPOINT ["dbmate"]
COPY --from=dev /src/dist/dbmate /usr/local/bin/dbmate
ENTRYPOINT ["/usr/local/bin/dbmate"]

View file

@ -1,60 +1,58 @@
# no static linking for macos
LDFLAGS := -ldflags '-s'
# statically link binaries (to support alpine + scratch containers)
STATICLDFLAGS := -ldflags '-s -extldflags "-static"'
# avoid building code that is incompatible with static linking
TAGS := -tags netgo,osusergo,sqlite_omit_load_extension,sqlite_json
# enable cgo to build sqlite
export CGO_ENABLED = 1
# strip binaries
FLAGS := -tags sqlite_omit_load_extension,sqlite_json -ldflags '-s'
GOOS := $(shell go env GOOS)
ifeq ($(GOOS),linux)
# statically link binaries to support alpine linux
FLAGS := -tags netgo,osusergo,sqlite_omit_load_extension,sqlite_json -ldflags '-s -extldflags "-static"'
endif
ifeq ($(GOOS),darwin)
export SDKROOT ?= $(shell xcrun --sdk macosx --show-sdk-path)
endif
OUTPUT ?= dbmate
.PHONY: all
all: build test lint
all: fix build wait test
.PHONY: clean
clean:
rm -rf dist
.PHONY: build
build: clean
go build -o dist/$(OUTPUT) $(FLAGS) .
.PHONY: ls
ls:
ls -lh dist/$(OUTPUT)
file dist/$(OUTPUT)
.PHONY: test
test:
go test -p 1 $(TAGS) $(STATICLDFLAGS) ./...
.PHONY: fix
fix:
golangci-lint run --fix
go test -p 1 $(FLAGS) ./...
.PHONY: lint
lint:
golangci-lint run
.PHONY: fix
fix:
golangci-lint run --fix
.PHONY: wait
wait:
dist/dbmate-linux-amd64 -e CLICKHOUSE_TEST_URL wait
dist/dbmate-linux-amd64 -e MYSQL_TEST_URL wait
dist/dbmate-linux-amd64 -e POSTGRES_TEST_URL wait
.PHONY: clean
clean:
rm -rf dist/*
.PHONY: build
build: clean build-linux-amd64
ls -lh dist
.PHONY: build-linux-amd64
build-linux-amd64:
GOOS=linux GOARCH=amd64 \
go build $(TAGS) $(STATICLDFLAGS) -o dist/dbmate-linux-amd64 .
.PHONY: build-all
build-all: clean build-linux-amd64
GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc-5 CXX=aarch64-linux-gnu-g++-5 \
go build $(TAGS) $(STATICLDFLAGS) -o dist/dbmate-linux-arm64 .
GOOS=darwin GOARCH=amd64 CC=o64-clang CXX=o64-clang++ \
go build $(TAGS) $(LDFLAGS) -o dist/dbmate-macos-amd64 .
GOOS=darwin GOARCH=arm64 CC=o64-clang CXX=o64-clang++ \
go build $(TAGS) $(LDFLAGS) -o dist/dbmate-macos-arm64 .
GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc-posix CXX=x86_64-w64-mingw32-g++-posix \
go build $(TAGS) $(STATICLDFLAGS) -o dist/dbmate-windows-amd64.exe .
ls -lh dist
dist/dbmate -e CLICKHOUSE_TEST_URL wait
dist/dbmate -e MYSQL_TEST_URL wait
dist/dbmate -e POSTGRES_TEST_URL wait
.PHONY: docker-all
docker-all:
docker-compose build
docker-compose run --rm dev make
docker-compose run --rm dev make all
.PHONY: docker-sh
docker-sh:

View file

@ -1,4 +1,4 @@
version: '2.3'
version: "2.3"
services:
dev:
build:

View file

@ -179,10 +179,8 @@ func TestMySQLDumpSchema(t *testing.T) {
drv.databaseURL.Path = "/fakedb"
schema, err = drv.DumpSchema(db)
require.Nil(t, schema)
require.EqualError(t, err, "mysqldump: [Warning] Using a password "+
"on the command line interface can be insecure.\n"+
"mysqldump: Got error: 1049: "+
"Unknown database 'fakedb' when selecting the database")
require.Error(t, err)
require.Contains(t, err.Error(), "Unknown database 'fakedb'")
}
func TestMySQLDatabaseExists(t *testing.T) {

View file

@ -186,8 +186,8 @@ func TestPostgresDumpSchema(t *testing.T) {
drv.databaseURL.Path = "/fakedb"
schema, err = drv.DumpSchema(db)
require.Nil(t, schema)
require.EqualError(t, err, "pg_dump: [archiver (db)] connection to database "+
"\"fakedb\" failed: FATAL: database \"fakedb\" does not exist")
require.Error(t, err)
require.Contains(t, err.Error(), "database \"fakedb\" does not exist")
})
t.Run("custom migrations table with schema", func(t *testing.T) {