Skip to content

Feature/docker #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
54 changes: 53 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,62 @@ Additionally you can override the destination of installation:
PLUGIN_FOLDER=/path/to/plugin/folder make install
```

Note that the plugin has to be built for target OS and archetecture (`make build`
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