Skip to content

Commit 37bdd0f

Browse files
committed
cleanup: refactor to use stagex
1 parent 8626353 commit 37bdd0f

File tree

4 files changed

+99
-154
lines changed

4 files changed

+99
-154
lines changed

.github/workflows/publish-container.yaml

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,51 @@ jobs:
1616
permissions:
1717
contents: read
1818
packages: write
19+
env:
20+
tags: >-
21+
${{ github.ref == format('refs/heads/{0}', 'main') && 'latest' || '' }}
22+
${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || '' }}
23+
${{ github.event_name == 'push' && github.ref_name || '' }}
24+
sha-${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
1925
steps:
2026
- name: Checkout
2127
uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
2228
with:
2329
ref: ${{ github.event.pull_request.head.sha }}
24-
- name: Docker meta
25-
id: meta
26-
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # v5.5.1
27-
with:
28-
images: |
29-
ghcr.io/tkhq/ecr-proxy
30-
tags: |
31-
type=raw,value=latest,enable={{is_default_branch}}
32-
type=ref,event=pr
33-
type=semver,pattern={{version}}
34-
type=sha,format=long
35-
- name: Get committer date
30+
- name: Setup and configure Docker
3631
run: |
37-
echo "committer_date=$(git log -1 --pretty=%ct)" >> "$GITHUB_ENV"
38-
- name: Set up Docker Buildx
39-
uses: docker/setup-buildx-action@d70bba72b1f3fd22344832f00baa16ece964efeb # v3.3.0
40-
- name: Login to GHCR
41-
if: github.event_name != 'pull_request'
42-
uses: docker/login-action@0d4c9c5ea7693da7b068278f7b52bda2a190a446 # v3.2.0
43-
with:
44-
registry: ghcr.io
45-
username: ${{ github.actor }}
46-
password: ${{ secrets.GITHUB_TOKEN }}
47-
- name: Build and push
48-
uses: docker/build-push-action@ca052bb54ab0790a636c9b5f226502c73d547a25 # v5.4.0
49-
with:
50-
context: .
51-
platforms: linux/amd64,linux/arm64
52-
build-args: |
53-
SOURCE_DATE_EPOCH=${{ env.committer_date }}
54-
provenance: "false"
55-
push: ${{ github.event_name != 'pull_request' }}
56-
tags: ${{ steps.meta.outputs.tags }}
32+
[[ $EUID -ne 0 ]] && exec sudo /bin/sh "$0" "$@"
33+
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; \
34+
do apt-get remove $pkg; \
35+
done
36+
apt-get update
37+
apt-get install ca-certificates curl
38+
install -m 0755 -d /etc/apt/keyrings
39+
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
40+
chmod a+r /etc/apt/keyrings/docker.asc
41+
echo \
42+
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
43+
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
44+
tee /etc/apt/sources.list.d/docker.list > /dev/null
45+
apt-get update
46+
cat << EOF >/etc/docker/daemon.json
47+
{
48+
"features": {
49+
"containerd-snapshotter": true
50+
}
51+
}
52+
EOF
53+
apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
54+
systemctl restart docker
55+
docker buildx create --driver docker-container --bootstrap --name build --use
56+
- name: Build
57+
run: make
58+
- name: Push
59+
run: |
60+
env -C out/ecr-proxy tar -cf - . | docker load
61+
docker tag "tkhq/ecr-proxy:latest" "ghcr.io/tkhq/ecr-proxy:latest"
62+
for tag in ${tags}; do
63+
docker tag "tkhq/ecr-proxy:latest" "ghcr.io/tkhq/ecr-proxy:${tag}"
64+
done
65+
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin
66+
docker image push --all-tags "ghcr.io/tkhq/ecr-proxy"

Containerfile

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
FROM stagex/busybox:sx2024.04.2@sha256:8cb9360041cd17e8df33c5cbc6c223875045c0c249254367ed7e0eb445720757 AS busybox
2+
FROM stagex/musl:sx2024.04.2@sha256:f888fcf45fabaaae3d0268bcec902ceb94edba7bf8d09ef6966ebb20e00b7127 AS musl
3+
FROM stagex/go:sx2024.04.2@sha256:7a0c200995e220519aae02554c082b45cc3f7452480ea45d19e15ad3ecdffb4c AS go
4+
FROM stagex/ca-certificates:sx2024.04.2@sha256:f9fe6e67df91083fee3d88cf221f84ef77f0b67480fb5b0689e890509a712533 AS ca-certificates
5+
6+
FROM scratch as builder
7+
COPY --from=busybox . /
8+
COPY --from=musl . /
9+
COPY --from=go . /
10+
COPY --from=ca-certificates . /
11+
12+
ARG TARGETOS
13+
ARG TARGETARCH
14+
15+
ENV GOPATH=/usr/home/build
16+
ENV GOOS=${TARGETOS}
17+
ENV GOARCH=${TARGETARCH}
18+
ENV GOPROXY=off
19+
ENV CGO_ENABLED=0
20+
ENV GOPROXY="https://proxy.golang.org,direct"
21+
ENV GO_BUILDFLAGS="-x -v -trimpath -buildvcs=false"
22+
ENV GO_LDFLAGS="-s -w -buildid= -extldflags=-static"
23+
ENV GOFLAGS=${GO_BUILDFLAGS} -ldflags="${GO_LDFLAGS}"
24+
25+
RUN <<-EOF
26+
set -eux
27+
mkdir -p /newroot/etc/ssl/certs
28+
cp -ra --parents /etc/ssl/certs /newroot/
29+
EOF
30+
31+
WORKDIR /usr/home/build/src
32+
33+
COPY ./src/go.mod ./src/go.sum ./
34+
RUN go mod download
35+
36+
COPY ./src ./
37+
RUN --network=none go build ${GOFLAGS} \
38+
-o /newroot/usr/local/bin/ecr-proxy \
39+
./cmd/ecr-proxy
40+
41+
FROM scratch
42+
LABEL org.opencontainers.image.source https://github.com/tkhq/ecr-proxy
43+
COPY --from=builder /newroot /
44+
USER 65532:65532
45+
ENTRYPOINT ["/usr/local/bin/ecr-proxy"]

Dockerfile

Lines changed: 0 additions & 39 deletions
This file was deleted.

Makefile

Lines changed: 12 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,19 @@
1-
include $(PWD)/src/toolchain/Makefile
2-
3-
KEYS := \
4-
6B61ECD76088748C70590D55E90A401336C8AAA9 \
5-
A8864A8303994E3A18ACD1760CAB4418C834B102 \
6-
66039AA59D823C8BD68DB062D3EC673DF9843E7B \
7-
DE050A451E6FAF94C677B58B9361DEC647A087BD
8-
9-
LOCAL_BUILD_DIR := 'build'
10-
111
.DEFAULT_GOAL :=
2+
export
123
.PHONY: default
13-
default: \
14-
cache \
15-
toolchain \
16-
$(patsubst %,$(KEY_DIR)/%.asc,$(KEYS)) \
17-
$(OUT_DIR)/ecr-proxy.linux-x86_64 \
18-
$(OUT_DIR)/ecr-proxy.linux-aarch64 \
19-
$(OUT_DIR)/release.env \
20-
toolchain-profile
4+
default: out/ecr-proxy/index.json
215

226
.PHONY: lint
237
lint:
24-
$(call toolchain,' \
25-
GOCACHE=/home/build/$(CACHE_DIR) \
26-
GOPATH=/home/build/$(CACHE_DIR) \
27-
env -C $(SRC_DIR) go vet -v ./... \
28-
')
29-
30-
.PHONY: cache
31-
cache:
32-
ifneq ($(TOOLCHAIN_REPRODUCE),true)
33-
git lfs pull --include=cache/toolchain.tgz
34-
$(MAKE) toolchain-restore-mtime
35-
touch fetch/apt/Packages.bz2 cache/toolchain.tgz
36-
endif
37-
38-
.PHONY: dist
39-
dist: toolchain-dist
40-
41-
.PHONY: reproduce
42-
reproduce:
43-
git lfs pull --include=fetch/apt/
44-
$(MAKE) toolchain-reproduce toolchain-profile
8+
env -C src go vet -v ./...
459

4610
.PHONY: test
47-
test: $(OUT_DIR)/ecr-proxy.linux-x86_64
48-
$(call toolchain,' \
49-
GOCACHE=/home/build/$(CACHE_DIR) \
50-
GOPATH=/home/build/$(CACHE_DIR) \
51-
env -C $(SRC_DIR) go test -v ./... \
52-
')
53-
54-
.PHONY: install
55-
install: default
56-
mkdir -p ~/.local/bin
57-
cp $(OUT_DIR)/ecr-proxy.$(HOST_OS)-$(HOST_ARCH) ~/.local/bin/ecr-proxy
58-
59-
# Clean repo back to initial clone state
60-
.PHONY: clean
61-
clean: toolchain-clean
62-
git clean -dfx $(SRC_DIR)
63-
rm -rf $(LOCAL_BUILD_DIR)
64-
65-
$(KEY_DIR)/%.asc:
66-
$(call fetch_pgp_key,$(basename $(notdir $@)))
67-
68-
$(OUT_DIR)/ecr-proxy.%:
69-
$(call toolchain-profile-start)
70-
$(call toolchain,' \
71-
GOHOSTOS="linux" \
72-
GOHOSTARCH="amd64" \
73-
GOOS="$(word 1,$(subst -, ,$(word 2,$(subst ., ,$@))))" \
74-
GOARCH="$(call altarch,$(word 2,$(subst -, ,$(word 2,$(subst ., ,$@)))))" \
75-
GOCACHE=/home/build/$(CACHE_DIR) \
76-
GOPATH=/home/build/$(CACHE_DIR) \
77-
CGO_ENABLED=0 \
78-
env -C $(SRC_DIR)/cmd/ecr-proxy \
79-
go build \
80-
-trimpath \
81-
-ldflags="-s -w -buildid=''" \
82-
-o /home/build/$@ . \
83-
')
84-
$(call toolchain-profile-stop)
85-
86-
.PHONY: build-local
87-
build-local:
88-
pushd $(shell git rev-parse --show-toplevel)/src; \
89-
go build -o ../$(LOCAL_BUILD_DIR)/ecr-proxy; \
90-
popd;
11+
test:
12+
env -C src go test -v ./...
13+
14+
out/ecr-proxy/index.json:
15+
docker build \
16+
-f Containerfile \
17+
--tag tkhq/ecr-proxy:latest \
18+
--output type=oci,tar=false,rewrite_timestamps=true,dest=out/ecr-proxy \
19+
.

0 commit comments

Comments
 (0)