diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..87478f8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,38 @@ +FROM golang:1.16.3-alpine AS builder + +RUN apk add --update --no-cache make +ENV PROJECT_NAME=tfdocs-format-template +ENV SOURCE_DIR=/go/src/${PROJECT_NAME} +# replace tdocs-format-template with actual formatter plugin name. +WORKDIR ${SOURCE_DIR} + +COPY go.mod . +COPY go.sum . +RUN go mod download + +COPY . . +RUN make build + +################ + +FROM quay.io/terraform-docs/terraform-docs:latest +# added because custom plugin should be installed into the $HOME +ENV PROJECT_NAME=tfdocs-format-template +ENV BUILD_CONTAINER_WD=/go/src/${PROJECT_NAME} +ENV USER=docker +ENV UID=12345 +ENV GID=23456 +# creating user in docker container +RUN adduser \ + --disabled-password \ + --gecos "" \ + --home "$(pwd)" \ + --ingroup "root" \ + --uid "$UID" \ + "$USER" +# after this, all commands will be executed from this user +USER docker + +COPY --from=builder ${BUILD_CONTAINER_WD}/bin/linux-amd64/${PROJECT_NAME} $HOME/.tfdocs.d/plugins/${PROJECT_NAME} + +CMD ["/bin/sh","-c","terraform-docs","$@"] \ No newline at end of file diff --git a/Makefile b/Makefile index b531e2c..5472772 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,13 @@ PLUGIN_FOLDER ?= ~/.tfdocs.d/plugins GOOS ?= $(shell go env GOOS) GOARCH ?= $(shell go env GOARCH) +CUR_VERSION ?=v1.0.0 +DEFAULT_TAG ?= $(shell echo "$(CUR_VERSION)" | tr -d 'v') +DOCKER_REGISTRY :=quay.io +PROJECT_OWNER := terraform-focs +DOCKER_IMAGE := $(DOCKER_REGISTRY)/$(PROJECT_OWNER)/$(BUILD_NAME) +DOCKER_TAG ?= $(DEFAULT_TAG) + all: clean verify build clean: @@ -21,4 +28,10 @@ install: build mkdir -p $(PLUGIN_FOLDER) mv ./$(BUILD_DIR)/$(GOOS)-$(GOARCH)/$(BUILD_NAME) $(PLUGIN_FOLDER) +docker: ## Build Docker image + docker build --pull --tag $(DOCKER_IMAGE):$(DOCKER_TAG) --file Dockerfile . + +push: ## Push Docker image + docker push $(DOCKER_IMAGE):$(DOCKER_TAG) + .PHONY: all clean verify build install help \ No newline at end of file diff --git a/README.md b/README.md index 6a22d2a..1dd72f2 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,58 @@ Note that the plugin has to be built for target OS and architecture (`make build and `make install` do that,) but if you want to redistribute the plugin for other people to use you have to cross-compile it (for example you can use [gox].) +## Docker + +If you don't want cross-compile your plugin, you can pack it to the docker image. +So the only dependency needed the user/or a server machine is `docker`. + +### Building and publishing docker image + +```bash +make docker +``` + +Before you will build your docker image you have to replace very few things in the `Makefile` and the `Dockerfile`. + +Things to replace: + +`Makefile`: +- DOCKER_REGISTRY (could be any docker registry) +- PROJECT_OWNER (how the the place file in your registry. In our company it's something like `devops/tools`) +- BUILD_NAME (your project name should follow the patter `tfdocs-format-` ) + +`Dockerfile`: +- all **PROJECT_NAME** entries should be replaced with your project name( and it should follow the pattern `tfdocs-format-`) + +After all changes and `make docker` successful execution, you can push your new perfect image to the registry by: +```bash +make push +``` + +### Using docker image with your custom plugin + +I'm a little lazy, and I made a simple sh script for easy usage of docker image with terraform-docs and the custom plugin: + +```shell +#!/bin/bash + +img= + +dir=$1 +out_file=$2 + +function printMarkdownTo() { + pushd $1 + (docker run --rm -v `pwd`:`pwd` -w `pwd` $img .) > $2 + popd +} + +printMarkdownTo ${dir} ${out_file} + +``` + +## Links + [terraform-docs]: https://github.com/terraform-docs/terraform-docs [plugin SDK]: https://github.com/terraform-docs/plugin-sdk [Go]: https://golang.org/