Skip to content

Commit e78b093

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

File tree

4 files changed

+103
-147
lines changed

4 files changed

+103
-147
lines changed

.github/workflows/publish-container.yaml

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,62 @@ 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
31+
shell: 'script -q -e -c "bash {0}"'
3632
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
33+
[[ $EUID -ne 0 ]] && exec sudo /bin/sh "$0" "$@"
34+
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; \
35+
do apt-get remove $pkg; \
36+
done
37+
apt-get update
38+
apt-get install ca-certificates curl
39+
install -m 0755 -d /etc/apt/keyrings
40+
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
41+
chmod a+r /etc/apt/keyrings/docker.asc
42+
echo \
43+
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
44+
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
45+
tee /etc/apt/sources.list.d/docker.list > /dev/null
46+
apt-get update
47+
cat << EOF >/etc/docker/daemon.json
48+
{
49+
"features": {
50+
"containerd-snapshotter": true
51+
},
52+
"registry-mirrors": ["https://ghcr.io/tkhq"]
53+
}
54+
EOF
55+
apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
56+
systemctl restart docker
57+
docker buildx create --driver docker-container --bootstrap --name build --use
4058
- name: Login to GHCR
4159
if: github.event_name != 'pull_request'
4260
uses: docker/login-action@0d4c9c5ea7693da7b068278f7b52bda2a190a446 # v3.2.0
4361
with:
4462
registry: ghcr.io
4563
username: ${{ github.actor }}
4664
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 }}
65+
- name: Build
66+
shell: 'script -q -e -c "bash {0}"'
67+
run: |
68+
make
69+
- name: Push
70+
shell: 'script -q -e -c "bash {0}"'
71+
run: |
72+
env -C out/ecr-proxy tar -cf - . | docker load
73+
docker tag "tkhq/ecr-proxy:latest" "ghcr.io/tkhq/ecr-proxy:latest"
74+
for tag in ${tags}; do
75+
docker tag "tkhq/ecr-proxy:latest" "ghcr.io/tkhq/ecr-proxy:${tag}"
76+
done
77+
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)