diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..69126d8 --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,78 @@ +name: Go build and test + +on: + push: + branches: [ "*" ] + pull_request: + branches: [ master ] + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +jobs: + build: + strategy: + matrix: + version: ["1.16", "1.17", "1.18", "1.19", "1.20", "1.21"] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.version }} + cache: true + + - name: Build + run: go build -v ./... + + test: + strategy: + matrix: + version: ["1.16", "1.17", "1.18", "1.19", "1.20", "1.21"] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.version }} + cache: true + + - name: Unit tests + run: go test -v ./... + + race-condition: + strategy: + matrix: + version: ["1.16", "1.17", "1.18", "1.19", "1.20", "1.21"] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.version }} + cache: true + + - name: Test race condition + run: go test -race -vet=off ./... + + docker: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Build + uses: docker/build-push-action@v5 + with: + push: false + cache-from: type=gha + cache-to: type=gha,mode=max \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..8e2e318 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,80 @@ +name: Release with Docker and goreleaser + +on: + push: + # run only against tags + tags: + - '*' + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Fetch all tags + run: git fetch --force --tags + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.21" + cache: true + + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v5 + with: + # either 'goreleaser' (default) or 'goreleaser-pro' + distribution: goreleaser + version: latest + args: release --clean + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + docker: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + # list of Docker images to use as base name for tags + images: | + ghcr.io/lescactus/unix2dos-go + # generate Docker tags based on the following events/attributes + tags: | + type=ref,event=branch + type=semver,pattern={{raw}} + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}},enable=${{ !startsWith(github.ref, 'refs/tags/v0.') }} # disable for major version starting with 0 + type=sha + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Build + uses: docker/build-push-action@v5 + env: + tag: ${{ github.ref }} + with: + push: true + cache-from: type=gha + cache-to: type=gha,mode=max + labels: ${{ steps.meta.outputs.labels }} + tags: ${{ steps.meta.outputs.tags }} \ No newline at end of file diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..af4c3c1 --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,3 @@ +builds: + - env: + - CGO_ENABLED=0 diff --git a/Dockerfile b/Dockerfile index 125809f..a8c9402 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,27 +1,20 @@ -FROM golang:1.16-alpine as builder +FROM golang:1.21 as builder -ADD go.* /go/src/ +WORKDIR /app -WORKDIR /go/src/ +COPY go.* ./ RUN go mod download -COPY . /go/src/ - -RUN go build -o main +COPY . . -FROM alpine:3 +RUN CGO_ENABLED=0 go build -ldflags '-d -w -s' -o main -RUN apk update \ - && apk add ca-certificates \ - && rm -rf /var/cache/apk* \ - && adduser -u 1000 -D -s /bin/sh app \ - && install -d -m 0750 -o app -g app /app - -WORKDIR /app +FROM scratch -COPY --from=builder /go/src/main /app +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +COPY --from=builder /app/main / -USER app +EXPOSE 8080 -ENTRYPOINT ["/app/main"] +ENTRYPOINT ["/main"] \ No newline at end of file diff --git a/README.md b/README.md index e67258f..c107b76 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Flags: ### From source with go -You need a working [go](https://golang.org/doc/install) toolchain (It has been developped and tested with go 1.16 and go 1.16 only, but should work with go >= 1.12). Refer to the official documentation for more information (or from your Linux/Mac/Windows distribution documentation to install it from your favorite package manager). +You need a working [go](https://golang.org/doc/install) toolchain (It has been developped and tested with go 1.16 and go 1.16 only, but should work with go >= 1.16). Refer to the official documentation for more information (or from your Linux/Mac/Windows distribution documentation to install it from your favorite package manager). ```sh # Clone this repository