Skip to content

Commit 345e94e

Browse files
committed
update builder
1 parent 8f81c6a commit 345e94e

File tree

8 files changed

+163
-97
lines changed

8 files changed

+163
-97
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
name: "build and release"
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- "main"
8+
schedule:
9+
- cron: "00 01 * * *"
10+
workflow_dispatch:
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0
20+
- name: Create cache folder
21+
run: |
22+
sudo mkdir --parents /media/saved-cache
23+
sudo chown -R "$(whoami)" /media/saved-cache
24+
- name: Get cache
25+
uses: actions/cache/restore@v4
26+
with:
27+
path: /media/saved-cache
28+
key: docker-meshcentral-build-cache-${{ github.run_id }}
29+
restore-keys: docker-meshcentral-build-cache
30+
- name: Import cache
31+
run: |
32+
if (ls /media/saved-cache/*.tar.zst); then
33+
docker run --pull always --rm \
34+
--volume "/media/saved-cache:/media/saved-cache" \
35+
--volume "docker-meshcentral-build-cache:/media/build-cache" \
36+
--workdir /media \
37+
madebytimo/scripts \
38+
compress.sh --decompress /media/saved-cache/*.tar.zst
39+
rm /media/saved-cache/*.tar.zst
40+
fi
41+
- name: Set secrets and variables
42+
run: |
43+
mkdir data-local
44+
echo "latest_version=$(git describe --tags --abbrev=0)" >> "$GITHUB_ENV"
45+
echo "version=$(cat Version.txt)" >> "$GITHUB_ENV"
46+
if [[ -n '${{ secrets.UNITY_LICENSE_FILE }}' ]]; then
47+
echo '${{ secrets.UNITY_LICENSE_FILE }}' > data-local/unity-license.ulf
48+
fi
49+
if [[ -n '${{ secrets.DOCKER_REGISTRY_USERNAME }}' ]]; then
50+
echo ${{ secrets.DOCKER_REGISTRY_PASSWORD }} | \
51+
docker login --username ${{ secrets.DOCKER_REGISTRY_USERNAME }} \
52+
--password-stdin ${{ secrets.DOCKER_REGISTRY_URL }}
53+
fi
54+
- name: Prepare environment
55+
run: |
56+
if [[ -f builder/docker.sh ]]; then
57+
docker buildx create --use
58+
fi
59+
- name: Build
60+
if: ${{ github.event_name != 'schedule'}}
61+
run: |
62+
if [[ ${{ github.ref }} == 'refs/heads/main' ]]; then
63+
builder/build.sh --publish
64+
else
65+
builder/build.sh
66+
fi
67+
- name: Build update base
68+
if: ${{ github.event_name == 'schedule'}}
69+
run: builder/build.sh --publish --update-base
70+
- name: Upload
71+
uses: actions/upload-artifact@v4
72+
with:
73+
name: ${{ env.version }}
74+
path: builds/*
75+
- name: Release
76+
if: ${{ github.ref == 'refs/heads/main' && env.latest_version != env.version }}
77+
uses: softprops/action-gh-release@v2
78+
with:
79+
files: builds/*
80+
tag_name: ${{ env.version }}
81+
- name: Export cache
82+
run: |
83+
docker run --pull always --rm \
84+
--volume "/media/saved-cache:/media/saved-cache" \
85+
--volume "docker-meshcentral-build-cache:/media/build-cache" \
86+
madebytimo/scripts \
87+
compress.sh --fast --output /media/saved-cache/build-cache /media/build-cache
88+
sudo chown -R "$(whoami)" /media/saved-cache
89+
- name: Delete old caches
90+
env:
91+
GH_TOKEN: ${{ github.token }}
92+
run: |
93+
for CACHE in $(gh cache list --key Factory-build-cache --ref ${{ github.ref}} \
94+
| cut --fields 1); do
95+
echo "Deleting cache \"$CACHE\"."
96+
gh cache delete "$CACHE"
97+
done
98+
- name: Set cache
99+
uses: actions/cache/save@v4
100+
with:
101+
path: /media/saved-cache
102+
key: docker-meshcentral-build-cache-${{ github.run_id }}

.github/workflows/check-version-increment.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- name: Checkout new
13-
uses: actions/checkout@v3
13+
uses: actions/checkout@v4
1414
with:
1515
path: new
1616
- name: Checkout old
17-
uses: actions/checkout@v3
17+
uses: actions/checkout@v4
1818
with:
1919
path: old
2020
ref: refs/heads/main
2121
- name: Setup Node.js
22-
uses: actions/setup-node@v3
22+
uses: actions/setup-node@v4
2323
- name: Download script
2424
run: |
2525
curl --silent --output check-version-increment.sh \
2626
https://gitlab.com/madebyTimo/scripts-development/-/raw/main/scripts/\
27-
check-version-increment.sh \
27+
check-version-increment.sh \
2828
&& chmod +x check-version-increment.sh
2929
- name: Test version increment
3030
if: startsWith(github.ref, '/refs/heads/feature/') ||

.github/workflows/docker-build-and-push.yaml

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

.github/workflows/docker-update-base-and-push.yaml

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

.github/workflows/static-code-analysis.yaml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,24 @@ jobs:
99
runs-on: ubuntu-latest
1010
steps:
1111
- name: Get cache
12-
uses: actions/cache/restore@v3
12+
uses: actions/cache/restore@v4
1313
with:
1414
path: /media/saved-cache
1515
key: docker-meshcentral-build-cache-${{ github.run_id }}
1616
restore-keys: docker-meshcentral-build-cache
1717
- name: Import cache
1818
run: |
19-
cat | \
19+
if (ls /media/saved-cache/*.tar.zst); then
2020
docker run --pull always --rm \
2121
--volume "/media/saved-cache:/media/saved-cache" \
2222
--volume "docker-meshcentral-build-cache:/media/build-cache" \
23+
--workdir /media \
2324
madebytimo/scripts \
24-
bash \
25-
<< EOF
26-
cd /media
27-
if (ls /media/saved-cache/*.tar.zst); then
28-
compress.sh --decompress /media/saved-cache/*.tar.zst
25+
compress.sh --decompress /media/saved-cache/*.tar.zst
2926
rm /media/saved-cache/*.tar.zst
3027
fi
31-
EOF
3228
- name: Checkout
33-
uses: actions/checkout@v3
29+
uses: actions/checkout@v4
3430
- name: Download script
3531
run: |
3632
curl --silent --output /usr/local/bin/static-code-analysis.sh \
@@ -53,8 +49,17 @@ jobs:
5349
madebytimo/scripts \
5450
compress.sh --fast --output /media/saved-cache/build-cache /media/build-cache
5551
sudo chown -R "$(whoami)" /media/saved-cache
52+
- name: Delete old caches
53+
env:
54+
GH_TOKEN: ${{ github.token }}
55+
run: |
56+
for CACHE in $(gh cache list --key Factory-build-cache --ref ${{ github.ref}} \
57+
| cut --fields 1); do
58+
echo "Deleting cache \"$CACHE\"."
59+
gh cache delete "$CACHE"
60+
done
5661
- name: Set cache
57-
uses: actions/cache/save@v3
62+
uses: actions/cache/save@v4
5863
with:
5964
path: /media/saved-cache
6065
key: docker-meshcentral-build-cache-${{ github.run_id }}

builder/build.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
5+
6+
# help message
7+
for ARGUMENT in "$@"; do
8+
if [ "$ARGUMENT" == "-h" ] || [ "$ARGUMENT" == "--help" ]; then
9+
echo "usage: $(basename "$0")"
10+
echo "Run all scripts in the same folder."
11+
echo "All arguments are passed to the build scripts."
12+
exit
13+
fi
14+
done
15+
16+
mapfile -t BUILD_SCRIPTS -d '' < <(find "$SCRIPT_DIR" -name '*.sh' -not -name "$(basename "$0")")
17+
for BUILD_SCRIPT in "${BUILD_SCRIPTS[@]}"; do
18+
echo "Start \"$(basename "${BUILD_SCRIPT}")\""
19+
"$BUILD_SCRIPT" "$@"
20+
echo "Finished \"$(basename "${BUILD_SCRIPT}")\""
21+
done

docker-build.sh renamed to builder/docker.sh

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ set -e
44
BUILD_ARGUMENTS=()
55
DEPENDENCIES=(docker)
66
UPDATE_BASE=false
7+
PLATFORMS=(amd64 arm64 arm)
78
REGISTRY_USER="madebytimo"
8-
REPOSIITORY_NAME="docker-meshcentral"
9+
APPLICATION_NAME="meshcentral"
910

1011
# help message
1112
for ARGUMENT in "$@"; do
1213
if [ "$ARGUMENT" == "-h" ] || [ "$ARGUMENT" == "--help" ]; then
1314
echo "usage: $(basename "$0") [ARGUMENT]"
1415
echo "Builds the docker image from the Dockerfile."
1516
echo "ARGUMENT can be"
16-
echo "--platform [amd64|arm64|arm] build only specified platform"
17-
echo "--push push the build"
18-
echo "--update-base only build if newer base image available"
17+
echo "--platform [amd64|arm64|arm] Build only for specified platform."
18+
echo "--publish Push the build."
19+
echo "--update-base Only build if newer base image is available."
1920
exit
2021
fi
2122
done
@@ -32,23 +33,26 @@ done
3233
while [[ -n "$1" ]]; do
3334
if [[ "$1" = "--platform" ]]; then
3435
shift
35-
BUILD_ARGUMENTS+=("--set" "default.platform=$1")
36+
PLATFORMS=("$1")
3637
elif [[ "$1" = "--update-base" ]]; then
3738
UPDATE_BASE=true
38-
elif [[ "$1" = "--push" ]]; then
39+
elif [[ "$1" = "--publish" ]]; then
3940
BUILD_ARGUMENTS+=("--push")
40-
else
41-
echo "Unknown argument: \"$1\""
42-
exit 1
4341
fi
4442
shift
4543
done
4644

47-
BASE_IMAGE="$(tac Dockerfile | grep --max-count=1 "FROM" | cut -d" " -f2)"
45+
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
46+
VERSION="$(cat Version.txt)"
47+
48+
cd "$PROJECT_DIR"
49+
mkdir --parents builds
50+
51+
BASE_IMAGE="$(tac Dockerfile | grep --max-count=1 "^FROM" | cut -d" " -f2)"
4852
docker pull "$BASE_IMAGE"
4953
BASE_IMAGE_DATE="$(docker image inspect --format="{{ .Created }}" "$BASE_IMAGE" | cut -d "T" -f1)"
5054
echo "Base image is $BASE_IMAGE from $BASE_IMAGE_DATE"
51-
IMAGE="${REGISTRY_USER}/${REPOSIITORY_NAME#docker-}"
55+
IMAGE="${REGISTRY_USER}/${APPLICATION_NAME}"
5256
if [[ "$UPDATE_BASE" == true ]]; then
5357
docker pull "$IMAGE"
5458
PUSHED_IMAGE_DATE="$(docker image inspect --format="{{ .Created }}" "$IMAGE" | cut -d "T" -f1)"
@@ -58,7 +62,10 @@ if [[ "$UPDATE_BASE" == true ]]; then
5862
exit;
5963
fi
6064
fi
61-
VERSION="$(cat Version.txt)"
6265

63-
export BASE_IMAGE BASE_IMAGE_DATE IMAGE VERSION
64-
docker buildx bake --file docker-bake.hcl "${BUILD_ARGUMENTS[@]}"
66+
PLATFORMS_STRING="${PLATFORMS[*]}"
67+
BUILD_ARGUMENTS+=(--platform "${PLATFORMS_STRING// /,}")
68+
69+
docker buildx build "${BUILD_ARGUMENTS[@]}" \
70+
--output type=oci,dest=builds/"${IMAGE//"/"/-}-${VERSION}.tar" --tag "${IMAGE}:latest" \
71+
--tag "${IMAGE}:${VERSION}" --tag "${IMAGE}:${VERSION}-base-${BASE_IMAGE_DATE}" .

docker-bake.hcl

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

0 commit comments

Comments
 (0)