Browse Source

Initial revamp test

master
Ambrose Chua 10 months ago
parent
commit
1f2ed024fd
  1. 30
      .github/workflows/bump_caddy.yml
  2. 38
      .github/workflows/docker.yml
  3. 33
      .github/workflows/regenerate_stackbrew.yml
  4. 9
      2.1/alpine/Dockerfile
  5. 10
      Makefile
  6. 55
      generate-stackbrew-library.sh
  7. 8
      go.mod
  8. 3
      legacy/Caddyfile
  9. 53
      legacy/Dockerfile
  10. 61
      legacy/plugger.go
  11. 9
      library/caddy
  12. 169
      update.go
  13. 55
      update.sh

30
.github/workflows/bump_caddy.yml

@ -1,30 +0,0 @@
name: Bump Caddy version
on:
schedule:
- cron: "12 */4 * * *"
jobs:
bump_caddy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
token: ${{ secrets.PUSH_GITHUB_TOKEN }}
- name: Regenerate Dockerfiles
run: make update
- name: Configure git
run: |
git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
- name: Commit and push changes
run: |
git add */Dockerfile
if output=$(git status --porcelain) && [ ! -z "$output" ]; then
git commit -m 'Bump Caddy version'
git push
fi

38
.github/workflows/docker.yml

@ -1,6 +1,8 @@
name: Build and push Docker images
name: Update, build and push Docker images
on:
schedule:
- cron: "12 */4 * * *"
push:
branches:
- master
@ -15,18 +17,46 @@ jobs:
env:
BASHBREW_LIBRARY: ./library
BASHBREW_NAMESPACE: productionwentdown
if: github.repository == 'productionwentdown/caddy' && github.ref == 'refs/heads/master'
steps:
- uses: actions/checkout@v2
- name: build
run: bashbrew build --pull always caddy
- name: Set Git user
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
- name: Regenerate Dockerfiles
run: |
go run update.go -dockerfiles
- name: Commit and push changes
id: commit_dockerfiles
run: |
git add */*/Dockerfile
if output=$(git status --porcelain) && [ ! -z "$output" ]; then
git commit -m "Bump Caddy version" -a
git push
echo "::set-output name=bumped::yes"
fi
- name: Regenerate Bashbrew manifests
if: steps.commit_dockerfiles.bumped == 'yes'
run: |
go run update.go -manifest -commit $(git log -1 --format='format:%H' HEAD)
- name: Build on Linux
if: steps.commit_dockerfiles.bumped == 'yes'
run: |
bashbrew build --pull always caddy
- name: push
if: steps.commit_dockerfiles.bumped == 'yes'
run: |
echo ${{ secrets.DOCKERHUB_TOKEN }} | docker login --username ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
bashbrew push caddy
if: github.repository == 'productionwentdown/caddy' && github.ref == 'refs/heads/master'
- name: microbadger
if: steps.commit_dockerfiles.bumped == 'yes'
run: curl -X POST https://hooks.microbadger.com/images/productionwentdown/caddy/OVgee2in7ldJ7GgTZiScpwH_mTs=

33
.github/workflows/regenerate_stackbrew.yml

@ -1,33 +0,0 @@
name: Generate stackbrew library
on:
push:
branches:
- master
paths:
- '*/Dockerfile'
jobs:
regenerate_stackbrew:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
token: ${{ secrets.PUSH_GITHUB_TOKEN }}
- name: Regenerate stackbrew library
run: make library/caddy
- name: Configure git
run: |
git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
- name: Commit and push changes
run: |
git add library/caddy
if output=$(git status --porcelain) && [ ! -z "$output" ]; then
git commit -m 'Regenerate stackbrew library'
git push
fi

scratch/Dockerfile → 2.1/alpine/Dockerfile

10
Makefile

@ -1,10 +0,0 @@
.PHONY: all
all: update library/caddy
.PHONY: update
update: update.sh
./update.sh
.PHONY: library/caddy
library/caddy: generate-stackbrew-library.sh
./generate-stackbrew-library.sh > library/caddy

55
generate-stackbrew-library.sh

@ -1,55 +0,0 @@
#!/bin/bash
set -Eeuo pipefail
gitHubUrl='https://github.com/productionwentdown/caddy'
gitHubUpstreamUrl='https://github.com/caddyserver/caddy-docker'
join() {
local sep="$1"; shift
local out; printf -v out "${sep//%/%%}%s" "$@"
echo "${out#$sep}"
}
_wget() {
wget -qO- -o /dev/null "$@"
}
cat <<-EOH
# see https://github.com/caddyserver/caddy-docker
Maintainers: Ambrose Chua <ambrose@makerforce.io> (@serverwentdown)
GitRepo: https://github.com/productionwentdown/caddy.git
GitCommit: $(git log --format='format:%H' -1)
EOH
caddyStackbrew="$(_wget "$gitHubUpstreamUrl/raw/master/stackbrew-config.yaml")"
caddyVersion="$(echo "$caddyStackbrew" | grep -oP '(?<=caddy_version: '"'"').+(?='"'"')')"
caddyMajor="$(echo "$caddyStackbrew" | grep -oP '(?<=caddy_major: '"'"').+(?='"'"')')"
version="${caddyVersion#v}"
versionAliases=($version)
arches=(amd64 arm64v8 arm32v6 arm32v7)
if [[ "$version" =~ "-rc" ]]; then
versionAliases+=(test)
elif [[ "$version" =~ "-beta" ]]; then
versionAliases+=()
else
versionAliases+=("$caddyMajor" latest)
fi
cat <<-EOE
Tags: $(join ', ' "${versionAliases[@]}")
Architectures: $(join ', ' "${arches[@]}")
Directory: scratch/
EOE
cat <<-EOE
Tags: 1.0.5, 1
Architectures: $(join ', ' "${arches[@]}")
Directory: legacy/
EOE

8
go.mod

@ -0,0 +1,8 @@
module github.com/productionwentdown/caddy
go 1.14
require (
github.com/docker-library/go-dockerlibrary v0.0.0-20200501195759-c5db18cf68eb
pault.ag/go/debian v0.0.0-20190530135403-b831f604d664 // indirect
)

3
legacy/Caddyfile

@ -1,3 +0,0 @@
:2015 {
root /srv
}

53
legacy/Dockerfile

@ -1,53 +0,0 @@
# LEGACY
#
# Build stage
#
FROM golang:1.13-alpine as build
# args
ARG version="1.0.5"
# add plugin import paths here separated by commas
ARG plugins=""
ARG telemetry="true"
# build root
WORKDIR /build
# plugins
COPY plugger.go ./
# build & test
RUN apk add --no-cache git upx ca-certificates \
&& echo -e "module caddy\nrequire github.com/caddyserver/caddy v${version}" > go.mod \
&& go run plugger.go -plugins="${plugins}" -telemetry="${telemetry}" \
&& CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build \
&& upx --ultra-brute caddy \
&& ./caddy -version
#
# Final image
#
FROM scratch
# labels
LABEL org.label-schema.vcs-url="https://github.com/productionwentdown/caddy"
LABEL org.label-schema.version=v${version}
LABEL org.label-schema.schema-version="1.0"
# copy binary and ca certs
COPY --from=build /build/caddy /bin/caddy
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
# copy default caddyfile
COPY Caddyfile /etc/Caddyfile
# set default caddypath
ENV CADDYPATH=/etc/.caddy
VOLUME /etc/.caddy
# serve from /srv
WORKDIR /srv
EXPOSE 2015
ENTRYPOINT ["/bin/caddy", "--conf", "/etc/Caddyfile", "--log", "stdout"]

61
legacy/plugger.go

@ -1,61 +0,0 @@
// +build ignore
package main
import (
"flag"
"log"
"os"
"strings"
"text/template"
)
var plugins string
var telemetry bool
func main() {
flag.StringVar(&plugins, "plugins", "", "Specify plugins by full paths, seperated by commas")
flag.BoolVar(&telemetry, "telemetry", false, "Enable telemetry")
flag.Parse()
d := &data{
Plugins: strings.FieldsFunc(plugins, func(c rune) bool { return c == ',' }),
EnableTelemetry: telemetry,
}
log.Printf("Additional plugins: %v", d.Plugins)
log.Printf("Enabled telemetry: %v", d.EnableTelemetry)
f, err := os.Create("caddy.go")
defer f.Close()
if err != nil {
log.Fatal("Unable to open file")
}
t := template.Must(template.New("caddy.go").Parse(caddyTemplate))
t.Execute(f, d)
}
type data struct {
Plugins []string
EnableTelemetry bool
}
var caddyTemplate = `
package main
import (
"github.com/caddyserver/caddy/caddy/caddymain"
// plug in plugins here
{{range $plugin := .Plugins}}
_ "{{$plugin}}"
{{end}}
)
func main() {
// optional: disable telemetry
caddymain.EnableTelemetry = {{.EnableTelemetry}}
caddymain.Run()
}
`

9
library/caddy

@ -1,9 +0,0 @@
# see https://github.com/caddyserver/caddy-docker
Maintainers: Ambrose Chua <ambrose@makerforce.io> (@serverwentdown)
GitRepo: https://github.com/productionwentdown/caddy.git
GitCommit: 0087b38b91932150f42a7ea4bc6d9c2d3b32bc17
Tags: 2.1.1, 2.1, 2
Architectures: amd64, arm64v8, arm32v6, arm32v7
Directory: scratch/

169
update.go

@ -0,0 +1,169 @@
package main
import (
"os"
"flag"
"io/ioutil"
"path"
"strings"
"text/template"
"github.com/docker-library/go-dockerlibrary/manifest"
)
var manifestURL = "https://github.com/docker-library/official-images/raw/master/library/caddy"
var maintainers = []string{"Ambrose Chua <ambrose@makerforce.io> (@serverwentdown)"}
var gitRepo = "https://github.com/productionwentdown/caddy.git"
var dockerfileTemplate = template.Must(template.New("name").Parse(`FROM caddy:{{.Tag}} as build
RUN apk add --no-cache upx ca-certificates \
&& upx --ultra-brute /usr/bin/caddy \
&& caddy version
FROM scratch
COPY --from=build /etc/caddy/Caddyfile /etc/caddy/Caddyfile
COPY --from=build /usr/share/caddy/index.html /usr/share/caddy/index.html
COPY --from=build /usr/bin/caddy /usr/bin/caddy
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=build /etc/nsswitch.conf /etc/nsswitch.conf
ENV XDG_CONFIG_HOME=/config
ENV XDG_DATA_HOME=/data
ENV HOME=/
VOLUME /config
VOLUME /data
LABEL org.opencontainers.image.version=v{{.Version}}
LABEL org.opencontainers.image.title="Caddy (productionwentdown build)"
LABEL org.opencontainers.image.description="a powerful, enterprise-ready, open source web server with automatic HTTPS written in Go"
LABEL org.opencontainers.image.url=https://productionwentdown.makerforce.io
LABEL org.opencontainers.image.documentation=https://caddyserver.com/docs
LABEL org.opencontainers.image.vendor="productionwentdown"
LABEL org.opencontainers.image.licenses=Apache-2.0
LABEL org.opencontainers.image.source="https://github.com/productionwentdown/caddy"
EXPOSE 80
EXPOSE 443
EXPOSE 2019
WORKDIR /srv
CMD ["/usr/bin/caddy", "run", "--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]
`))
type dockerfileData struct {
Tag string
Version string
Arch string
}
func main() {
doDockerfiles := flag.Bool("dockerfiles", false, "Update Dockerfiles")
doManifest := flag.Bool("manifest", false, "Update manifest")
commit := flag.String("commit", "", "Current commit hash")
flag.Parse()
_, _, man, err := manifest.Fetch("nonexistent", manifestURL)
if err != nil {
panic(err)
}
man = subsetManifest(man)
if *doDockerfiles {
err = updateDockerfiles(man)
if err != nil {
panic(err)
}
}
if *doManifest {
err = updateManifest(man, *commit)
if err != nil {
panic(err)
}
}
}
// subsetManifest returns the subset of the manifest that can be built
func subsetManifest(man *manifest.Manifest2822) *manifest.Manifest2822 {
entries := make([]manifest.Manifest2822Entry, 0, 1)
for _, entry := range man.Entries {
hasWindows := false
for _, arch := range entry.Architectures {
if strings.HasPrefix(arch, "windows") {
hasWindows = true
}
}
isBuilder := false
for _, tag := range entry.Tags {
if strings.HasSuffix(tag, "builder") {
isBuilder = true
}
}
if hasWindows || isBuilder {
continue
}
entries = append(entries, entry)
}
return &manifest.Manifest2822{
Global: man.Global,
Entries: entries,
}
}
// updateDockerfiles generates a new set of Dockerfiles based on the manifest
func updateDockerfiles(man *manifest.Manifest2822) error {
for _, entry := range man.Entries {
tag := entry.Tags[0]
version := strings.Split(entry.Tags[0], "-")[0]
for _, arch := range entry.Architectures {
data := dockerfileData{Tag: tag, Version: version, Arch: arch}
err := writeDockerfile(entry, data)
if err != nil {
return err
}
}
}
// TODO: Remove old Dockerfiles
return nil
}
func writeDockerfile(entry manifest.Manifest2822Entry, data dockerfileData) error {
dockerfilePath := path.Join(entry.ArchDirectory(data.Arch), entry.ArchFile(data.Arch))
err := os.MkdirAll(entry.ArchDirectory(data.Arch), 0755)
if err != nil {
return err
}
dockerfileFile, err := os.OpenFile(dockerfilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer dockerfileFile.Close()
err = dockerfileTemplate.Execute(dockerfileFile, data)
if err != nil {
return err
}
return nil
}
// updateManifest replaces upstream maintainer and Git info with this repo
func updateManifest(man *manifest.Manifest2822, gitCommit string) error {
man.Global.Maintainers = maintainers
for i := range man.Entries {
entry := &man.Entries[i]
entry.Maintainers = maintainers
entry.GitRepo = gitRepo
entry.GitCommit = gitCommit
}
manifestPath := path.Join("library", "caddy")
return ioutil.WriteFile(manifestPath, []byte(man.String()), 0644)
}

55
update.sh

@ -1,55 +0,0 @@
#!/bin/bash
set -Eeuo pipefail
gitHubUpstreamUrl='https://github.com/caddyserver/caddy-docker'
join() {
local sep="$1"; shift
local out; printf -v out "${sep//%/%%}%s" "$@"
echo "${out#$sep}"
}
_wget() {
wget -qO- -o /dev/null "$@"
}
caddyStackbrew="$(_wget "$gitHubUpstreamUrl/raw/master/stackbrew-config.yaml")"
caddyVersion="$(echo "$caddyStackbrew" | grep -oP '(?<=caddy_version: '"'"').+(?='"'"')')"
cat > scratch/Dockerfile <<-EODF
FROM caddy:$caddyVersion-alpine as build
RUN apk add --no-cache upx ca-certificates \
&& upx --ultra-brute /usr/bin/caddy \
&& caddy version
FROM scratch
COPY --from=build /etc/caddy/Caddyfile /etc/caddy/Caddyfile
COPY --from=build /usr/share/caddy/index.html /usr/share/caddy/index.html
COPY --from=build /usr/bin/caddy /usr/bin/caddy
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
ENV XDG_CONFIG_HOME=/config
ENV XDG_DATA_HOME=/data
ENV HOME=/
VOLUME /config
VOLUME /data
LABEL org.opencontainers.image.version=v$caddyVersion
LABEL org.opencontainers.image.title="Caddy (productionwentdown fork)"
LABEL org.opencontainers.image.description="a powerful, enterprise-ready, open source web server with automatic HTTPS written in Go"
LABEL org.opencontainers.image.url=https://productionwentdown.makerforce.io
LABEL org.opencontainers.image.documentation=https://caddyserver.com/docs
LABEL org.opencontainers.image.vendor="productionwentdown"
LABEL org.opencontainers.image.licenses=Apache-2.0
LABEL org.opencontainers.image.source="https://github.com/productionwentdown/caddy"
EXPOSE 80
EXPOSE 443
EXPOSE 2019
CMD ["/usr/bin/caddy", "run", "--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]
EODF
Loading…
Cancel
Save