Collecting files for GitHub Pages with Go and bitfield/script

I wanted to host my old university MATLAB assignments on GitHub Pages. The repository already had the published reports as HTML and PDF files scattered across folders, so all the deploy needed was:
- Copy every
.htmland.pdffile into onegh-pagesfolder. - Write an
index.htmlthat links to each of them. - Publish that folder from GitHub Actions.
A shell script would do, but I wrote it in Go using github.com/bitfield/script, which gives you shell-style pipelines (find, grep, exec) as Go method chains. The examples below were checked against script v0.25.2.
The plain shell version
Section titled “The plain shell version”For comparison, the core of the job in bash:
#!/usr/bin/env bashset -euo pipefail
out=gh-pagesrm -rf "$out"mkdir -p "$out"
# Copy every HTML and PDF file, flattening the folder structurefind . -path "./$out" -prune -o -path ./.git -prune -o \ -type f \( -name '*.html' -o -name '*.pdf' \) -print \ | while read -r f; do cp "$f" "$out/"; donecp overwrites silently, so two reports with the same filename in different folders will clobber each other. That was fine for my repo, but worth knowing.
The Go version with bitfield/script
Section titled “The Go version with bitfield/script”script.FindFiles(".") lists every file under the current directory, one path per line. MatchRegexp keeps only the lines that match, and Slice collects them into a []string. After that it’s ordinary Go: copy the files and render the index with html/template.
package main
import ( "html/template" "log" "os" "path/filepath" "regexp"
"github.com/bitfield/script")
const outputFolder = "gh-pages"
var index = template.Must(template.New("files").Parse(`<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>List of Matlab Reports and Files</title> </head> <body> <h1>List of Files</h1> <ul> {{- range . }} <li><a href="{{ . }}">{{ . }}</a></li> {{- end }} </ul> </body></html>`))
func main() { // Start from an empty output folder. if err := os.RemoveAll(outputFolder); err != nil { log.Fatal(err) }
// Find every .html and .pdf file in the repo. files, err := script.FindFiles("."). MatchRegexp(regexp.MustCompile(`\.(html|pdf)$`)). Slice() if err != nil { log.Fatal(err) }
if err := os.MkdirAll(outputFolder, 0o755); err != nil { log.Fatal(err) }
// Copy each file into gh-pages and keep its base name for the index. var names []string for _, f := range files { name := filepath.Base(f) if _, err := script.File(f).WriteFile(filepath.Join(outputFolder, name)); err != nil { log.Fatal(err) } names = append(names, name) }
out, err := os.Create(filepath.Join(outputFolder, "index.html")) if err != nil { log.Fatal(err) } defer out.Close()
if err := index.Execute(out, names); err != nil { log.Fatal(err) } log.Printf("copied %d files to %s", len(names), outputFolder)}Run it from the repository root with go run . after go mod init and go get github.com/bitfield/script.
A few notes on the changes from my original version (linked below):
- The original built shell commands as strings (
script.Exec("cp " + file + " gh-pages")). That breaks on filenames with spaces and only works wherecp,mvandmkdirexist, so not on a stock Windows machine.script.File(f).WriteFile(dst)copies the file without spawning a process. - The original used
strings.Contains(line, ".html"), which also matches paths likenotes.html.bak. Anchoring the regex to the end of the path avoids that. - Every
scriptpipe call returns an error (Stdoutreturns(int, error)). The original ignored them, so a failed copy would still produce a “successful” deploy. - The
hrefvalue is now quoted.html/templateescapes it either way, but quoted attributes are easier to read.
Deploying with GitHub Actions
Section titled “Deploying with GitHub Actions”The workflow builds and runs the program, adds an ads.txt file, and publishes gh-pages with peaceiris/actions-gh-pages. I’ve updated the action versions from the original (checkout@v2, setup-go@v2, actions-gh-pages@v3) to their current major releases.
name: deploy
on: push: branches: [master]
permissions: contents: write
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7
- name: Set up Go uses: actions/setup-go@v7 with: go-version: stable
- name: Build run: | go build -o main . ./main echo "google.com, pub-2479144310234386, DIRECT, f08c47fec0942fa0" > gh-pages/ads.txt
- name: Deploy to gh-pages uses: peaceiris/actions-gh-pages@v4 with: github_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: ./gh-pagesTwo things that tripped me up:
actions-gh-pagespushes to agh-pagesbranch usingGITHUB_TOKEN, so the job needscontents: write. Repositories created after early 2023 default to a read-only token. Then set Settings → Pages → Source to thegh-pagesbranch.- Writing
ads.txtstraight intogh-pages/after./mainruns avoids an ordering problem in the original: the Go program ranmv ads.txt gh-pagesbefore the workflow step that writesads.txt, so (unless the file is committed) that move failed silently.
References
Section titled “References”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)