Skip to content

Commit

Permalink
Merge pull request #33 from crazy-max/dockerfile
Browse files Browse the repository at this point in the history
container based dev flow
  • Loading branch information
crazy-max authored Jun 13, 2022
2 parents dbe29c6 + 25700dc commit f108786
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 20 deletions.
38 changes: 18 additions & 20 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@ on:
pull_request:

jobs:
validate:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v3
-
name: Run
uses: docker/bake-action@v2
with:
targets: validate

test:
runs-on: ubuntu-latest
env:
GO111MODULE: on
strategy:
fail-fast: false
matrix:
go:
- 1.13
- 1.14
go_version:
- 1.15
- 1.16
- 1.17
Expand All @@ -27,23 +35,13 @@ jobs:
-
name: Checkout
uses: actions/checkout@v3
-
name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}
-
name: Cache Go modules
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-${{ matrix.go }}-
-
name: Test
run: |
go test -coverprofile=coverage.txt -covermode=atomic ./...
uses: docker/bake-action@v2
with:
targets: test
env:
GO_VERSION: ${{ matrix.go_version }}
-
name: Upload coverage
uses: codecov/codecov-action@v3
Expand Down
51 changes: 51 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# syntax=docker/dockerfile:1

ARG GO_VERSION="1.18"
ARG GOLANGCI_LINT_VERSION="v1.45"

FROM golangci/golangci-lint:${GOLANGCI_LINT_VERSION}-alpine AS golangci-lint

FROM golang:${GO_VERSION}-alpine AS base
RUN apk add --no-cache gcc git linux-headers musl-dev
ENV CGO_ENABLED=0
ENV GO111MODULE=on
WORKDIR /src

FROM base AS vendored
RUN --mount=type=bind,target=.,rw \
--mount=type=cache,target=/go/pkg/mod \
go mod tidy && go mod download && \
mkdir /out && cp go.mod go.sum /out

FROM scratch AS vendor-update
COPY --from=vendored /out /

FROM vendored AS vendor-validate
RUN --mount=type=bind,target=.,rw <<EOT
set -e
git add -A
cp -rf /out/* .
diff=$(git status --porcelain -- go.mod go.sum)
if [ -n "$diff" ]; then
echo >&2 'ERROR: Vendor result differs. Please vendor your package with "docker buildx bake vendor"'
echo "$diff"
exit 1
fi
EOT

FROM base AS lint
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/root/.cache \
--mount=from=golangci-lint,source=/usr/bin/golangci-lint,target=/usr/bin/golangci-lint \
golangci-lint run ./...

FROM vendored AS test
ENV CGO_ENABLED=1
RUN --mount=type=bind,target=. \
--mount=type=cache,target=/root/.cache \
--mount=type=cache,target=/go/pkg/mod \
go test -v -coverprofile=/tmp/coverage.txt -covermode=atomic -race ./... && \
go tool cover -func=/tmp/coverage.txt

FROM scratch AS test-coverage
COPY --from=test /tmp/coverage.txt /coverage.txt
41 changes: 41 additions & 0 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
variable "GO_VERSION" {
default = "1.18"
}

target "_common" {
args = {
GO_VERSION = GO_VERSION
}
}

group "default" {
targets = ["test"]
}

group "validate" {
targets = ["lint", "vendor-validate"]
}

target "lint" {
inherits = ["_common"]
target = "lint"
output = ["type=cacheonly"]
}

target "vendor-validate" {
inherits = ["_common"]
target = "vendor-validate"
output = ["type=cacheonly"]
}

target "vendor-update" {
inherits = ["_common"]
target = "vendor-update"
output = ["."]
}

target "test" {
inherits = ["_common"]
target = "test-coverage"
output = ["."]
}
1 change: 1 addition & 0 deletions loader_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

// FlagLoader is the structure representring a flag loader.
type FlagLoader struct {
//nolint:structcheck,unused
filename string
cfg FlagLoaderConfig
}
Expand Down

0 comments on commit f108786

Please sign in to comment.