Skip to content

Commit

Permalink
add dockerfile. add make targets. add sections to the readme.
Browse files Browse the repository at this point in the history
Signed-off-by: Sergey Yakovlev <[email protected]>
  • Loading branch information
syakovlevdalet committed May 4, 2021
1 parent 0dd040f commit af62438
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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","$@"]
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-<uniqueName>` )

`Dockerfile`:
- all **PROJECT_NAME** entries should be replaced with your project name( and it should follow the pattern `tfdocs-format-<uniqueName>`)

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=<link to the docker image with terraform-docs and custom plugin>

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/
Expand Down

0 comments on commit af62438

Please sign in to comment.