From 8b945c8f905265666d3984ccba9f89d7c218ee48 Mon Sep 17 00:00:00 2001 From: "Stanislav N. aka pztrn" Date: Sat, 4 May 2024 13:18:37 +0500 Subject: [PATCH] Initial commit. --- .drone.yml | 37 ++++++++++++++++++++++++++++ .gitignore | 3 +++ .markdownlint.json | 3 +++ .yamllint | 8 ++++++ Dockerfile | 19 +++++++++++++++ README.md | 39 ++++++++++++++++++++++++++++++ build.sh | 37 ++++++++++++++++++++++++++++ scripts/helpers/arch.sh | 17 +++++++++++++ scripts/workers/debian.sh | 4 +++ scripts/workers/dlv.sh | 7 ++++++ scripts/workers/go-junit-report.sh | 10 ++++++++ scripts/workers/gofumpt.sh | 12 +++++++++ scripts/workers/golang.sh | 22 +++++++++++++++++ scripts/workers/golangci-lint.sh | 20 +++++++++++++++ scripts/workers/mockery.sh | 14 +++++++++++ scripts/workers/taskfile.sh | 21 ++++++++++++++++ 16 files changed, 273 insertions(+) create mode 100644 .drone.yml create mode 100755 .gitignore create mode 100755 .markdownlint.json create mode 100755 .yamllint create mode 100755 Dockerfile create mode 100755 README.md create mode 100755 build.sh create mode 100755 scripts/helpers/arch.sh create mode 100755 scripts/workers/debian.sh create mode 100755 scripts/workers/dlv.sh create mode 100755 scripts/workers/go-junit-report.sh create mode 100755 scripts/workers/gofumpt.sh create mode 100755 scripts/workers/golang.sh create mode 100755 scripts/workers/golangci-lint.sh create mode 100755 scripts/workers/mockery.sh create mode 100755 scripts/workers/taskfile.sh diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..445ca68 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,37 @@ +--- +kind: pipeline +type: docker +name: "lint" + +steps: + - name: lint + image: koalaman/shellcheck-alpine:v0.8.0 + pull: if-not-exists + commands: + - /bin/shellcheck build.sh + +--- +kind: pipeline +type: docker +name: "build toolbox" + +trigger: + event: ["tag"] + +depends_on: + - "lint" + +steps: + - name: "build toolbox" + image: "code.pztrn.name/containers/mirror/docker:26.1.1-dind" + pull: if-not-exists + privileged: true + environment: + REGISTRY: code.pztrn.name + REGISTRY_PROJECT: /containers/go-toolbox + REGISTRY_USERNAME: drone + REGISTRY_PASSWORD: + from_secret: drone_secret + commands: + - apk add --no-cache bash + - ./build.sh diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..d28bd7a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +.vscode +*DS_Store* diff --git a/.markdownlint.json b/.markdownlint.json new file mode 100755 index 0000000..ac20dfa --- /dev/null +++ b/.markdownlint.json @@ -0,0 +1,3 @@ +{ + "line-length": false +} diff --git a/.yamllint b/.yamllint new file mode 100755 index 0000000..d65ed2c --- /dev/null +++ b/.yamllint @@ -0,0 +1,8 @@ +--- +extends: default + +rules: + line-length: disable + braces: + min-spaces-inside: 1 + max-spaces-inside: 1 diff --git a/Dockerfile b/Dockerfile new file mode 100755 index 0000000..4d00058 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM debian:12.5-slim + +COPY . /src +RUN for file in $(find /src -type f -name "*.sh"); do chmod +x $file; done + +RUN /src/scripts/workers/debian.sh +RUN /src/scripts/workers/golang.sh +RUN /src/scripts/workers/dlv.sh +RUN /src/scripts/workers/golangci-lint.sh +RUN /src/scripts/workers/gofumpt.sh +RUN /src/scripts/workers/mockery.sh +RUN /src/scripts/workers/taskfile.sh +RUN /src/scripts/workers/go-junit-report.sh + +RUN mkdir /home/container && chmod 0777 /home/container + +ENV HOME=/home/container +ENV GOPATH=/home/container/go +ENV GOCACHE=/home/container/.cache diff --git a/README.md b/README.md new file mode 100755 index 0000000..a8b331e --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# Go ToolBox + +Docker image with everything you might need for developing apps in Go. + +**Note that for newer mac hardware you have to use Docker Desktop or amd64 virtual machine in lima/other tools!** + +## What's inside + +Debian 12 (slim) is used as base image. + +| Binary | Version | Project | Repo | +| ----------------- | ------- | --------------- | ------------------------------------------------------------- | +| `go` | 1.22.2 | Go | | +| `golangci-lint` | 1.57.2 | golangci-lint | [External link](https://github.com/golangci/golangci-lint) | +| `gofumpt` | 0.6.0 | gofumpt | [External link](https://github.com/mvdan/gofumpt) | +| `mockery_v2` | 2.42.2 | mockery | [External link](https://github.com/vektra/mockery) | +| `task` | 3.36.0 | taskfile | [External link](https://github.com/go-task/task) | +| `go-junit-report` | 2.1.0 | go-junit-report | [External link](https://github.com/jstemmer/go-junit-report) | +| `delve` | 1.22.1 | delve | [External link](https://github.com/go-delve/delve) | + +## HOME + +Home directory is `/home/container`. + +## Caching + +Go's build cache resides in `/home/container/.cache`, `GOCACHE` environment variable is defined. You can mount it when needed in Dockerfile as: + +```Dockerfile +RUN --mount=type=cache,target="$GOCACHE" go build +``` + +## Utilities examples + +### JUnit report generation (for Gitlab) + +```shell +go test -v 2>&1 ./... | go-junit-report -set-exit-code > unit-report.xml +``` diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..f78392e --- /dev/null +++ b/build.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +# build.sh checks if docker image with same sha256 exists remotely. +# if exists, do nothing, if not, build the image + +commit_sha="${DRONE_COMMIT_SHA}" +image_path="${REGISTRY}${REGISTRY_PROJECT}" + +if [ "${DRONE_TAG}" != "" ]; then + tag="${DRONE_TAG}" +else + tag="${DRONE_COMMIT_SHA}" +fi + +# Starting Docker daemon. +/usr/local/bin/dockerd --data-root=/var/lib/docker --max-concurrent-uploads 1 & + +# Wait for it. +echo "* Waiting for Docker daemon to start..." +while true; do + if docker ps &>/dev/null; then + break + fi +done + +echo "* Logging in to registry..." +docker login -u "${REGISTRY_USERNAME}" -p "${REGISTRY_PASSWORD}" "${REGISTRY}" + +echo "* Building docker image ${image_path}:${tag}..." + +if docker manifest inspect "${image_path}:${tag}" >/dev/null; then + echo "* Docker image ${image_path}:${tag} is already exists on remote, no rebuilt necessary." + exit 0 +fi + +docker build --pull --build-arg VCS_REF="${commit_sha}" --build-arg VCS_URL="${image_path}" --tag "${image_path}:${tag}" . +docker push "${image_path}:${tag}" diff --git a/scripts/helpers/arch.sh b/scripts/helpers/arch.sh new file mode 100755 index 0000000..dd25dc6 --- /dev/null +++ b/scripts/helpers/arch.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# shellcheck disable=SC2034 +# Line above disables shellcheck linters: +# * SC2043 - unused variable (this file sourced elsewhere). + +base_arch=$(uname -m) +arch="" + +if [ "$base_arch" = "x86_64" ]; then + arch=amd64 +elif [ "$base_arch" = "aarch64" ]; then + arch=arm64 +else + echo "unknown arch" + exit 1 +fi diff --git a/scripts/workers/debian.sh b/scripts/workers/debian.sh new file mode 100755 index 0000000..38e0215 --- /dev/null +++ b/scripts/workers/debian.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +apt update && apt upgrade -y +apt install -y build-essential curl file git make diff --git a/scripts/workers/dlv.sh b/scripts/workers/dlv.sh new file mode 100755 index 0000000..a0b84c0 --- /dev/null +++ b/scripts/workers/dlv.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +dlv_version=1.22.1 + +set -xe + +CGO_ENABLED=0 GOBIN=/usr/local/bin/ go install -ldflags "-s -w -extldflags '-static'" github.com/go-delve/delve/cmd/dlv@v"$dlv_version" diff --git a/scripts/workers/go-junit-report.sh b/scripts/workers/go-junit-report.sh new file mode 100755 index 0000000..528fa67 --- /dev/null +++ b/scripts/workers/go-junit-report.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +go_junit_report_version=2.1.0 + +cd /tmp +git clone https://github.com/jstemmer/go-junit-report.git +cd go-junit-report || exit 1 +git checkout "v${go_junit_report_version}" +go build -o /usr/local/bin/go-junit-report . +rm -rf /tmp/go-junit-report diff --git a/scripts/workers/gofumpt.sh b/scripts/workers/gofumpt.sh new file mode 100755 index 0000000..e523bc2 --- /dev/null +++ b/scripts/workers/gofumpt.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +gofumpt_version="0.6.0" + +set -xe + +cd /tmp +git clone https://github.com/mvdan/gofumpt.git +cd gofumpt || exit 1 +git checkout "v${gofumpt_version}" +go build -o /usr/local/bin/gofumpt . +rm -rf /tmp/gofumpt diff --git a/scripts/workers/golang.sh b/scripts/workers/golang.sh new file mode 100755 index 0000000..835295d --- /dev/null +++ b/scripts/workers/golang.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# shellcheck disable=SC2154 +# Line above disables shellcheck linters: +# * SC2154 - variable referenced but not assigned (false positive, assigned when sourced arch.sh). + +set -xe + +go_version=1.22.2 + +# shellcheck disable=SC2086,SC2046,SC2164 +cd $(dirname ${BASH_SOURCE[0]}) +script_path=$(pwd) + +# shellcheck disable=SC1091 +source "${script_path}/../helpers/arch.sh" + +curl "https://dl.google.com/go/go${go_version}.linux-${arch}.tar.gz" -o "/tmp/go-${arch}.tar.gz" +file "/tmp/go-${arch}.tar.gz" +tar -xf "/tmp/go-${arch}.tar.gz" -C /usr/local/ +rm "/tmp/go-${arch}.tar.gz" +ln -s /usr/local/go/bin/* /usr/local/bin diff --git a/scripts/workers/golangci-lint.sh b/scripts/workers/golangci-lint.sh new file mode 100755 index 0000000..3875ed5 --- /dev/null +++ b/scripts/workers/golangci-lint.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# shellcheck disable=SC2154 +# Line above disables shellcheck linters: +# * SC2154 - variable referenced but not assigned (false positive, assigned when sourced arch.sh). + +golangci_lint_version=1.57.2 + +# shellcheck disable=SC2086,SC2046,SC2164 +cd "$(dirname ${BASH_SOURCE[0]})" +script_path=$(pwd) + +# shellcheck disable=SC1091 +source "${script_path}/../helpers/arch.sh" + +curl -L "https://github.com/golangci/golangci-lint/releases/download/v${golangci_lint_version}/golangci-lint-${golangci_lint_version}-linux-${arch}.tar.gz" -o "/tmp/golangci-lint-${arch}.tar.gz" +file "/tmp/golangci-lint-${arch}.tar.gz" +tar -xf "/tmp/golangci-lint-${arch}.tar.gz" -C /tmp +mv "/tmp/golangci-lint-${golangci_lint_version}-linux-${arch}/golangci-lint" /usr/local/bin +rm -rf "/tmp/golangci-lint-${arch}.tar.gz" "/tmp/golangci-lint-${golangci_lint_version}-linux-${arch}/" diff --git a/scripts/workers/mockery.sh b/scripts/workers/mockery.sh new file mode 100755 index 0000000..cd7bd24 --- /dev/null +++ b/scripts/workers/mockery.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +mockery_v2_version=2.42.2 + +cd /tmp || exit 1 + +# mockery v2 we download from releases page +arch=$(uname -m) +download_url="https://github.com/vektra/mockery/releases/download/v${mockery_v2_version}/mockery_${mockery_v2_version}_Linux_${arch}.tar.gz" +curl -L ${download_url} -o "/tmp/mockery_${arch}.tar.gz" +mkdir /tmp/mockery_v2 +tar xf "/tmp/mockery_${arch}.tar.gz" -C /tmp/mockery_v2 +mv /tmp/mockery_v2/mockery /usr/local/bin/mockery_v2 +rm -rf "/tmp/mockery_$(uname -m).tar.gz" /tmp/mockery* diff --git a/scripts/workers/taskfile.sh b/scripts/workers/taskfile.sh new file mode 100755 index 0000000..4cadf54 --- /dev/null +++ b/scripts/workers/taskfile.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# shellcheck disable=SC2154 +# Line above disables shellcheck linters: +# * SC2154 - variable referenced but not assigned (false positive, assigned when sourced arch.sh). + +taskfile_version=3.36.0 + +# shellcheck disable=SC2086,SC2046,SC2164 +cd "$(dirname ${BASH_SOURCE[0]})" +script_path=$(pwd) + +# shellcheck disable=SC1091 +source "${script_path}/../helpers/arch.sh" + +curl -L "https://github.com/go-task/task/releases/download/v${taskfile_version}/task_linux_${arch}.tar.gz" -o "/tmp/taskfile-${arch}.tar.gz" +file "/tmp/taskfile-${arch}.tar.gz" +tar -xf "/tmp/taskfile-${arch}.tar.gz" -C /tmp +mv "/tmp/task" /usr/local/bin +rm -rf "/tmp/*" +ls -la /usr/local/bin