From 1f2ed024fdb7464cf812572e89a9dc0958e816e7 Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Fri, 3 Jul 2020 00:31:29 +0800 Subject: [PATCH] Initial revamp test --- .github/workflows/bump_caddy.yml | 30 ---- .github/workflows/docker.yml | 38 ++++- .github/workflows/regenerate_stackbrew.yml | 33 ---- {scratch => 2.1/alpine}/Dockerfile | 9 +- Makefile | 10 -- generate-stackbrew-library.sh | 55 ------- go.mod | 8 + legacy/Caddyfile | 3 - legacy/Dockerfile | 53 ------- legacy/plugger.go | 61 -------- library/caddy | 9 -- update.go | 169 +++++++++++++++++++++ update.sh | 55 ------- 13 files changed, 218 insertions(+), 315 deletions(-) delete mode 100644 .github/workflows/bump_caddy.yml delete mode 100644 .github/workflows/regenerate_stackbrew.yml rename {scratch => 2.1/alpine}/Dockerfile (85%) delete mode 100644 Makefile delete mode 100755 generate-stackbrew-library.sh create mode 100644 go.mod delete mode 100644 legacy/Caddyfile delete mode 100644 legacy/Dockerfile delete mode 100644 legacy/plugger.go delete mode 100644 library/caddy create mode 100644 update.go delete mode 100755 update.sh diff --git a/.github/workflows/bump_caddy.yml b/.github/workflows/bump_caddy.yml deleted file mode 100644 index 549939e..0000000 --- a/.github/workflows/bump_caddy.yml +++ /dev/null @@ -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 diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 94cd089..ed26628 100644 --- a/.github/workflows/docker.yml +++ b/.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= diff --git a/.github/workflows/regenerate_stackbrew.yml b/.github/workflows/regenerate_stackbrew.yml deleted file mode 100644 index addf478..0000000 --- a/.github/workflows/regenerate_stackbrew.yml +++ /dev/null @@ -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 diff --git a/scratch/Dockerfile b/2.1/alpine/Dockerfile similarity index 85% rename from scratch/Dockerfile rename to 2.1/alpine/Dockerfile index 0b0a977..fe8086c 100644 --- a/scratch/Dockerfile +++ b/2.1/alpine/Dockerfile @@ -1,6 +1,8 @@ FROM caddy:2.1.1-alpine as build -RUN apk add --no-cache upx ca-certificates && upx --ultra-brute /usr/bin/caddy && caddy version +RUN apk add --no-cache upx ca-certificates \ + && upx --ultra-brute /usr/bin/caddy \ + && caddy version FROM scratch @@ -8,6 +10,7 @@ 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 @@ -17,7 +20,7 @@ VOLUME /config VOLUME /data LABEL org.opencontainers.image.version=v2.1.1 -LABEL org.opencontainers.image.title="Caddy (productionwentdown fork)" +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 @@ -29,4 +32,6 @@ EXPOSE 80 EXPOSE 443 EXPOSE 2019 +WORKDIR /srv + CMD ["/usr/bin/caddy", "run", "--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"] diff --git a/Makefile b/Makefile deleted file mode 100644 index b87a9db..0000000 --- a/Makefile +++ /dev/null @@ -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 diff --git a/generate-stackbrew-library.sh b/generate-stackbrew-library.sh deleted file mode 100755 index 6730e24..0000000 --- a/generate-stackbrew-library.sh +++ /dev/null @@ -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 (@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 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5e372f8 --- /dev/null +++ b/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 +) diff --git a/legacy/Caddyfile b/legacy/Caddyfile deleted file mode 100644 index ced9040..0000000 --- a/legacy/Caddyfile +++ /dev/null @@ -1,3 +0,0 @@ -:2015 { - root /srv -} diff --git a/legacy/Dockerfile b/legacy/Dockerfile deleted file mode 100644 index 3528824..0000000 --- a/legacy/Dockerfile +++ /dev/null @@ -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"] diff --git a/legacy/plugger.go b/legacy/plugger.go deleted file mode 100644 index ab632a8..0000000 --- a/legacy/plugger.go +++ /dev/null @@ -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() -} -` diff --git a/library/caddy b/library/caddy deleted file mode 100644 index ae24f6a..0000000 --- a/library/caddy +++ /dev/null @@ -1,9 +0,0 @@ -# see https://github.com/caddyserver/caddy-docker - -Maintainers: Ambrose Chua (@serverwentdown) -GitRepo: https://github.com/productionwentdown/caddy.git -GitCommit: 0087b38b91932150f42a7ea4bc6d9c2d3b32bc17 - -Tags: 2.1.1, 2.1, 2 -Architectures: amd64, arm64v8, arm32v6, arm32v7 -Directory: scratch/ diff --git a/update.go b/update.go new file mode 100644 index 0000000..c16163c --- /dev/null +++ b/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 (@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) +} diff --git a/update.sh b/update.sh deleted file mode 100755 index 40d95ba..0000000 --- a/update.sh +++ /dev/null @@ -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