Thumbnail image

Static Files in Buildpacks

Table of Contents

Intro

I spend quite some time the other day, trubleshooting how to preserve static files, when using Buildpacks. So I thought it was a good idea to document it here, in case other’s have the same problem.

The problem

When you are using Buildpacks, in Tanzu Application Platform (TAP), like I am.

You point to your repository, and then you get a container, that is build to run that code. It does not know, of the static files, you might reference in your code.

In my case, I was building a Golang Webserver, to serve some static HTML content. And yes, that HTML content, was the source of my problem.

File structure

So my file structure in my project, looks like this.

.
├── README.md
├── accelerator-log.md
├── app
│   ├── html
│   │   ├── index.html
│   │   ├── style.css
│   │   └── tap-into-prod.png
│   └── main.go
├── catalog
│   └── catalog-info.yaml
├── config
│   └── workload.yaml
├── docs
│   ├── index.md
│   └── links.md
├── k8s-resource.yaml
├── mkdocs.yml
└── project.toml

The interesting part, is what’s in the app folder and below. The rest is mostly for TAP, and someting we can talk about another day.

Build

So TAP (or Buildpacks) looks at the root of the directory, for code to determine how to build the container. In my case my code is in the app folder. So I needed to tell it, where to look. That is done, using a project.toml file, in the root of my repository.

To point it to my folder, I first added an env variable that does just that.

[[ build.env ]]
name="BP_GO_TARGETS"
value="./app"

That resulted in a nice container, without any files. So I had to add another variable, to tell it which files to preserve.

[[ build.env ]]
name="BP_KEEP_FILES"
value="app/html/*"

I had to give it the full path, for it to work.

One could think, that a html folder would now apear, in my container. But no.

The full path of the files, is now at /workspace/app/html/ So that is where I ended up pointing my webserver root to.

Not hard, when you find out how, but I did have to ask some collegaues, before I found the solution.

The full project.toml file, ended up looking like this

# project.toml
[ build ]

[[ build.env ]]
name="BP_GO_TARGETS"
value="./app"

[[ build.env ]]
name="BP_KEEP_FILES"
value="app/html/*"

I’m not 100% sure the first part is needed, but it’s working now, so i’m keeping it.

Documentation

It turns out, it is actualily documented quite nicely in the Github repo https://github.com/paketo-buildpacks/go-build but it was not something I found, while i was searching.

Photo by Amos from Stockphotos.com on Unsplash

Related Posts