Skip to content

Commit 1b86371

Browse files
committed
refactor: refactor the build process
1 parent d381ad6 commit 1b86371

File tree

6 files changed

+186
-132
lines changed

6 files changed

+186
-132
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Build and Release
2+
3+
on:
4+
push:
5+
branches:
6+
- develop
7+
- main
8+
pull_request:
9+
branches:
10+
- main
11+
12+
permissions:
13+
contents: write
14+
issues: write
15+
pull-requests: write
16+
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
env:
21+
HTTP_CLI_VERSION: v1.0.1
22+
steps:
23+
- uses: actions/checkout@v3
24+
- uses: actions/setup-node@v3
25+
with:
26+
node-version: 20
27+
- uses: actions/cache@v3
28+
with:
29+
path: ~/.npm
30+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
31+
restore-keys: |
32+
${{ runner.os }}-node-
33+
- run: npm ci
34+
- name: Set up QEMU
35+
uses: docker/setup-qemu-action@v3
36+
- name: Set up Docker Buildx
37+
uses: docker/setup-buildx-action@v3
38+
- name: Create and use buildx builder
39+
run: |
40+
docker buildx create --name shell-runtime-builder --driver docker-container --use
41+
docker buildx inspect shell-runtime-builder --bootstrap
42+
- name: Cache Docker layers
43+
uses: actions/cache@v3
44+
with:
45+
path: /tmp/.buildx-cache
46+
key: ${{ runner.os }}-buildx-${{ github.sha }}
47+
restore-keys: |
48+
${{ runner.os }}-buildx-
49+
- name: Set version
50+
id: version
51+
env:
52+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
53+
run: |
54+
if [ "${{ github.ref_name }}" = "main" ]; then
55+
# Get semantic version for main branch
56+
VERSION=$(npx semantic-release --no-ci --dry-run --branch main 2>&1 | grep -oP 'The next release version is \K[0-9]+\.[0-9]+\.[0-9]+' || echo "")
57+
if [ -z "$VERSION" ]; then
58+
echo "No release needed"
59+
echo "VERSION=develop" >> $GITHUB_ENV
60+
echo "SHOULD_RELEASE=false" >> $GITHUB_ENV
61+
else
62+
echo "VERSION=$VERSION" >> $GITHUB_ENV
63+
echo "SHOULD_RELEASE=true" >> $GITHUB_ENV
64+
fi
65+
else
66+
# Use branch name for develop
67+
echo "VERSION=${{ github.ref_name }}" >> $GITHUB_ENV
68+
echo "SHOULD_RELEASE=false" >> $GITHUB_ENV
69+
fi
70+
echo "Detected VERSION: $VERSION"
71+
- name: Build and push images
72+
run: |
73+
echo "${{ secrets.GHCR_PAT }}" > github_token
74+
export GITHUB_TOKEN="${{ secrets.GHCR_PAT }}"
75+
76+
# Build and push all variants
77+
make push VERSION="$VERSION" REGISTRY="ghcr.io/${{ github.repository_owner }}"
78+
shell: bash
79+
- name: Log in to GHCR
80+
run: echo "${{ secrets.GHCR_PAT }}" | docker login ghcr.io -u skunxicat --password-stdin
81+
- name: Create release
82+
if: env.SHOULD_RELEASE == 'true'
83+
run: npx semantic-release
84+
env:
85+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
86+
GHCR_PAT: ${{ secrets.GHCR_PAT }}

.github/workflows/build-base.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Build Base Image
2+
3+
on:
4+
push:
5+
branches: [ main, develop ]
6+
paths:
7+
- 'Dockerfile'
8+
- 'runtime/**'
9+
- 'task/handler.sh'
10+
- '.github/workflows/build-base.yml'
11+
pull_request:
12+
branches: [ main ]
13+
paths:
14+
- 'Dockerfile'
15+
- 'runtime/**'
16+
- 'task/handler.sh'
17+
18+
jobs:
19+
build-base:
20+
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@v4
23+
24+
- name: Set up Docker Buildx
25+
uses: docker/setup-buildx-action@v3
26+
27+
- name: Login to GitHub Container Registry
28+
uses: docker/login-action@v3
29+
with:
30+
registry: ghcr.io
31+
username: ${{ github.actor }}
32+
password: ${{ secrets.GITHUB_TOKEN }}
33+
34+
- name: Build and push base
35+
run: |
36+
./build --platform linux/arm64 --tag ghcr.io/${{ github.repository_owner }}/lambda-shell-runtime --push base
37+
env:
38+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/release.yml

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

Dockerfile

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ COPY task/handler.sh handler.sh
4343
LABEL org.opencontainers.image.source="https://github.com/ql4b/lambda-shell-runtime"
4444
LABEL org.opencontainers.image.version="${VERSION}"
4545

46-
# tiny: add lamnda helper functions
47-
FROM base AS tiny
46+
# tiny: add lambda helper functions
47+
FROM ghcr.io/ql4b/lambda-shell-runtime:base AS tiny
4848

4949
ARG VERSION
5050
ARG HTTP_CLI_VERSION
@@ -65,8 +65,12 @@ RUN pip3 install --no-cache-dir --target /tmp/awscurl awscurl && \
6565
find /tmp/awscurl -type f -name '*.pyc' -delete && \
6666
find /tmp/awscurl -type d -name '*.dist-info' -exec rm -rf {} +
6767

68-
# micro: inclues awscurl
69-
FROM tiny AS micro
68+
# micro: includes awscurl
69+
FROM ghcr.io/ql4b/lambda-shell-runtime:base AS micro-base
70+
71+
COPY task/helpers.sh helpers.sh
72+
73+
FROM micro-base AS micro
7074

7175
ARG VERSION
7276
ARG HTTP_CLI_VERSION
@@ -93,7 +97,11 @@ LABEL org.opencontainers.image.version="${VERSION}"
9397
LABEL org.opencontainers.image.http_cli_version="${HTTP_CLI_VERSION}"
9498

9599
# full: includes aws-cli for complete AWS functionality
96-
FROM tiny AS full
100+
FROM ghcr.io/ql4b/lambda-shell-runtime:base AS full-base
101+
102+
COPY task/helpers.sh helpers.sh
103+
104+
FROM full-base AS full
97105

98106
ARG VERSION
99107
ARG HTTP_CLI_VERSION

Makefile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.PHONY: help build push clean base tiny micro full
2+
3+
PLATFORM ?= linux/arm64
4+
TAG ?= lambda-shell-runtime
5+
VERSION ?= develop
6+
REGISTRY ?= ghcr.io/ql4b
7+
8+
help: ## Show this help
9+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}'
10+
11+
build: tiny micro full ## Build all variants locally
12+
13+
base: ## Build base image
14+
VERSION=$(VERSION) ./build --platform $(PLATFORM) --tag $(TAG) --load base
15+
16+
tiny: base ## Build tiny variant
17+
VERSION=$(VERSION) ./build --platform $(PLATFORM) --tag $(TAG) --load tiny
18+
19+
micro: base ## Build micro variant
20+
VERSION=$(VERSION) ./build --platform $(PLATFORM) --tag $(TAG) --load micro
21+
22+
full: base ## Build full variant
23+
VERSION=$(VERSION) ./build --platform $(PLATFORM) --tag $(TAG) --load full
24+
25+
push-base: ## Push base to registry
26+
VERSION=$(VERSION) ./build --platform $(PLATFORM) --tag $(REGISTRY)/$(TAG) --push base
27+
28+
push: ## Push all variants to registry
29+
VERSION=$(VERSION) ./build --platform $(PLATFORM) --tag $(REGISTRY)/$(TAG) --push tiny micro full
30+
31+
clean: ## Remove local images
32+
docker rmi -f $(TAG):base $(TAG):tiny $(TAG):micro $(TAG):full 2>/dev/null || true

build

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#!/bin/sh
22

3-
set -ex
3+
set -e
44

55
# Default config
66
PLATFORM="linux/arm64"
77
MODE="--load"
88
TAG="lambda-shell-runtime"
9-
VERSION="${VERSION:-dev}"
9+
VERSION="${VERSION:-develop}"
1010
VARIANTS="base tiny micro full"
11+
# VARIANTS="base tiny"
1112

1213
# Parse arguments
1314
while [ $# -gt 0 ]; do
@@ -28,13 +29,10 @@ while [ $# -gt 0 ]; do
2829
MODE="--load"
2930
shift
3031
;;
31-
full|tiny|micro|base)
32-
VARIANTS="$1"
33-
shift
34-
;;
3532
*)
36-
echo "Unknown option: $1"
37-
exit 1
33+
# Remaining arguments are variants
34+
VARIANTS="$*"
35+
break
3836
;;
3937
esac
4038
done
@@ -45,19 +43,26 @@ for VARIANT in $VARIANTS; do
4543

4644
[ "$VARIANT" = "base" ] && TARGET=""
4745

48-
[ "$VARIANT" = "base" ] && MODE="--load"
49-
5046
echo "Building $VARIANT ($DOCKERFILE) with platform $PLATFORM..."
47+
48+
# Build tags
49+
TAGS="--tag $TAG:$VARIANT"
50+
if [ -n "$VERSION" ]; then
51+
TAGS="$TAGS --tag $TAG:$VARIANT-$VERSION"
52+
fi
53+
5154
docker buildx build \
5255
--platform "$PLATFORM" \
56+
--provenance=false \
5357
--secret id=github_token,env=GITHUB_TOKEN \
54-
--tag $TAG:$VARIANT \
58+
$TAGS \
5559
--file "$DOCKERFILE" \
5660
${TARGET:+--target "$TARGET"} \
5761
$MODE \
5862
.
5963

60-
if [ -n "$VERSION" ]; then
64+
# Only do local tagging for --load mode if version wasn't already tagged
65+
if [ -n "$VERSION" ] && [ "$MODE" = "--load" ]; then
6166
echo "Tagging $TAG:$VARIANT-$VERSION"
6267
docker tag $TAG:$VARIANT $TAG:$VARIANT-$VERSION
6368
fi

0 commit comments

Comments
 (0)