Skip to content

Commit 448f11b

Browse files
committed
github workflow
1 parent 69c80f2 commit 448f11b

File tree

12 files changed

+244
-132
lines changed

12 files changed

+244
-132
lines changed

.github/workflows/build-image.yaml

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Build Image
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
build-postgres-image:
9+
strategy:
10+
matrix:
11+
# LABEL: MULTI_PG_VERSIONS
12+
version: [12, 13, 14, 15]
13+
uses: ./.github/workflows/reusable-image.yaml
14+
with:
15+
version: ${{ matrix.version }}
16+
target: "postgres-image"
17+
secrets:
18+
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
19+
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
20+
21+
build-operator-image:
22+
uses: ./.github/workflows/reusable-image.yaml
23+
with:
24+
target: "operator-image"
25+
secrets:
26+
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
27+
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
28+
29+
build-exporter-image:
30+
uses: ./.github/workflows/reusable-image.yaml
31+
with:
32+
target: "exporter-image"
33+
secrets:
34+
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
35+
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
36+
37+
# because create release on tag. so the release code can't include the package.
38+
# build-helm-package:
39+
# runs-on: ubuntu-latest
40+
# permissions:
41+
# contents: write
42+
# steps:
43+
# - name: Get Repo Directory
44+
# uses: actions/checkout@v3
45+
# with:
46+
# ref: ${{ github.head_ref }}
47+
#
48+
# - name: Build
49+
# run: |
50+
# make helm-package
51+
#
52+
# - name: Sync Change
53+
# uses: stefanzweifel/git-auto-commit-action@v4
54+
# with:
55+
# branch: main
56+
# commit_message: "generate helm repo"

.github/workflows/reformat.yaml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Format Code
2+
3+
on: [push]
4+
5+
jobs:
6+
format-code:
7+
runs-on: ubuntu-latest
8+
permissions:
9+
contents: write
10+
steps:
11+
- name: Get Repo Directory
12+
uses: actions/checkout@v3
13+
with:
14+
ref: ${{ github.head_ref }}
15+
16+
- name: Set up Python
17+
uses: actions/setup-python@v4
18+
with:
19+
python-version: "3.10"
20+
21+
- name: Build
22+
run: |
23+
pip install yapf
24+
make format
25+
26+
- name: Sync Change
27+
uses: stefanzweifel/git-auto-commit-action@v4
28+
with:
29+
commit_message: "reformat code"

.github/workflows/reusable-image.yaml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
on:
2+
workflow_call:
3+
inputs:
4+
version:
5+
required: false
6+
type: string
7+
description: "Postgres Kernel Major Version"
8+
default: "12"
9+
target:
10+
required: true
11+
type: string
12+
description: "Make Target"
13+
secrets:
14+
DOCKER_USERNAME:
15+
required: true
16+
DOCKER_PASSWORD:
17+
required: true
18+
19+
jobs:
20+
build-image:
21+
runs-on: ubuntu-latest
22+
timeout-minutes: 2880
23+
steps:
24+
- name: Get Repo Directory
25+
uses: actions/checkout@v3
26+
27+
- name: Docker Login
28+
run: |
29+
docker login --username=${{ secrets.DOCKER_USERNAME }} --password=${{ secrets.DOCKER_PASSWORD }}
30+
31+
- name: Build Image
32+
run: |
33+
make pgversion=${{ inputs.version }} platform=all ${{ inputs.target }}

Makefile

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ FILEDESC = "multi platform for computer/kubernetes postgresql"
1515
#docker_version ?= ""
1616
export platform=amd64# make platform=arm64 build arm, make platform=all build and push arm64/amd64 image
1717
export pgversion=all# make pgversion=12 only build postgresql 12 that in versions.json field. make pgversion="'13' '14' '15'" build postgresql 13 14 15.
18-
export forcebuildimage=0# make forcebuildimage=1 even though the image exists, it also compiles.
18+
export forcebuildimage=# local/remote
1919
export namespace=radondb# used when make platform=all and no repositry namespace
20+
export forcebuildhelm=0# 1 is force build helm package
2021

2122
### make log
2223
# nohup make postgres-image &
@@ -40,6 +41,7 @@ operator-yaml: operator-image
4041
cp jq-template.awk platforms/kubernetes/postgres-operator/deploy/jq-template.awk
4142
cd platforms/kubernetes/postgres-operator/deploy/; awk -f jq-template.awk postgres-operator.yaml.template > postgres-operator.yaml
4243
helm-package:
44+
if [ -s "./docs/postgres-operator-`jq -r '.version' helmversions.json`.tgz" -a "${forcebuildhelm}" != "1" ]; then echo "helm package exists, skiping ..." && exit 255; fi
4345
cp helmversions.json platforms/kubernetes/postgres-operator/deploy/versions.json
4446
cp jq-template.awk platforms/kubernetes/postgres-operator/deploy/jq-template.awk
4547
cd platforms/kubernetes/postgres-operator/deploy/; \
@@ -62,4 +64,5 @@ format:
6264
depends:
6365
sudo pip install yapf paramiko kubernetes kopf
6466
docker run --privileged --rm tonistiigi/binfmt --install all
65-
echo "TODO ubuntu: apt install jq"
67+
@echo "TODO ubuntu: apt install jq curl"
68+
@echo "build helm-package need helm environment."

exporterversions.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"image": "radondb-postgres-exporter:v1.1.0"
2+
"image": "radondb-postgres-exporter:dev"
33
}

image/common/common.sh

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env bash
2+
set -Eeo pipefail
3+
4+
function pre_build_image()
5+
{
6+
image=$1
7+
8+
tempImage=($(echo $image | tr "/" " "))
9+
if [ ${#tempImage[@]} == 1 ]; then
10+
tempImage="$namespace/$image"
11+
fi
12+
# get imageName and imageTag
13+
temp=($(echo $tempImage | tr ":" " "))
14+
imageName=${temp[0]}
15+
imageTag=${temp[1]}
16+
# check local and repository image
17+
repositoryImageExists=$(curl --silent -f --head -lL https://hub.docker.com/v2/repositories/$imageName/tags/$imageTag/ > /dev/null && echo "success" || echo "failed")
18+
localImageExists=$( docker image ls --format "{{.Repository}}:{{.Tag}}" | awk -v aaa=$image '{print $0} END{print aaa}' | grep -c $image )
19+
20+
# check local image
21+
if [ "$localImageExists" -ne 1 ]; then
22+
if [ "$forcebuildimage" ]; then
23+
echo "docker image $image exists, rebuilding the image ..."
24+
else
25+
echo "docker image $image exists, skiping ..."
26+
return 255
27+
fi
28+
fi
29+
30+
# check repository image
31+
if [ "$repositoryImageExists" == "success" ]; then
32+
if [ "$forcebuildimage" != "remote" ]; then
33+
echo "docker image $imageName:$imageTag exists on dockerhub, skiping ... "
34+
return 255
35+
else
36+
echo "docker image $imageName:$imageTag exists on dockerhub, rebuild image ..."
37+
fi
38+
fi
39+
40+
echo "pre_build_image success!"
41+
return 0
42+
}
43+
44+
function get_build_cmd() {
45+
image=$1
46+
platform=$2
47+
48+
build_cmd="docker buildx build --no-cache"
49+
#docker_version=$(docker version --format '{{index (split .Server.Version ".") 0}}')
50+
if [ $platform == "all" ]; then
51+
builder_exists=$(docker buildx ls | awk '{if ($1=="multi-platform") print $1}')
52+
if [ "$builder_exists" ]; then
53+
docker buildx rm multi-platform
54+
fi
55+
# create a new builder instance
56+
docker buildx create --use --name multi-platform --platform=linux/amd64,linux/arm64
57+
58+
temp=($(echo $image | tr "/" " "))
59+
if [ ${#temp[@]} == 1 ]; then
60+
image="$namespace/$image"
61+
fi
62+
63+
build_cmd="$build_cmd --push --platform linux/amd64,linux/arm64"
64+
else
65+
build_cmd="$build_cmd -o type=docker --platform linux/${platform}"
66+
fi
67+
68+
echo "$build_cmd -t $image ."
69+
70+
# remove builder instance
71+
# if [ $platform == "all" ]; then
72+
# docker buildx rm multi-platform
73+
# fi
74+
}

image/exporter/generate_image.sh

+7-34
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,23 @@
11
#!/usr/bin/env bash
22
set -Eeo pipefail
33

4+
source ../common/common.sh
45
build_image()
56
{
67
image=$1
78
platform=$2
89

9-
image_exists=$( docker image ls --format "{{.Repository}}:{{.Tag}}" | awk -v aaa=$image '{print $0} END{print aaa}' | grep -c $image )
10-
if [ "$image_exists" -ne 1 ]; then
11-
if [ "$forcebuildimage" = 1 ]; then
12-
echo "docker image $image exists, rebuilding the image ..."
13-
else
14-
echo "docker image $image exists, skiping ..."
15-
return
16-
fi
10+
# image check
11+
pre_build_image "$image" || error=true
12+
if [ "$error" ]; then
13+
return
1714
fi
1815

1916
echo "build docker image $image ..."
20-
build_cmd="docker buildx build --no-cache"
21-
22-
if [ $platform == "all" ]; then
23-
echo "build all image need docker login. "
24-
builder_exists=$(docker buildx ls | awk '{if ($1=="multi-platform") print $1}')
25-
if [ "$builder_exists" ]; then
26-
docker buildx rm multi-platform
27-
fi
28-
# create a new builder instance
29-
docker buildx create --use --name multi-platform --platform=linux/amd64,linux/arm64
30-
31-
temp=($(echo $image | tr "/" " "))
32-
if [ ${#temp[@]} == 1 ]; then
33-
image="$namespace/$image"
34-
fi
35-
36-
build_cmd="$build_cmd --push --platform linux/amd64,linux/arm64"
37-
else
38-
build_cmd="$build_cmd -o type=docker --platform linux/${platform}"
39-
fi
17+
echo "notice: build all image need docker login. "
4018

41-
build_cmd="$build_cmd -t $image ."
19+
build_cmd=$(get_build_cmd "$image" "$platform")
4220
$build_cmd
43-
44-
# remove builder instance
45-
# if [ $platform == "all" ]; then
46-
# docker buildx rm multi-platform
47-
# fi
4821
}
4922

5023
image=$(jq -r '.image' versions.json)

image/postgres-operator/generate_image.sh

+7-35
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,26 @@
11
#!/usr/bin/env bash
22
set -Eeo pipefail
33

4+
source ../common/common.sh
45
build_image()
56
{
67
image=$1
78
platform=$2
89

9-
image_exists=$( docker image ls --format "{{.Repository}}:{{.Tag}}" | awk -v aaa=$image '{print $0} END{print aaa}' | grep -c $image )
10-
if [ "$image_exists" -ne 1 ]; then
11-
if [ "$forcebuildimage" = 1 ]; then
12-
echo "docker image $image exists, rebuilding the image ..."
13-
else
14-
echo "docker image $image exists, skiping ..."
15-
return
16-
fi
10+
# image check
11+
pre_build_image "$image" || error=true
12+
if [ "$error" ]; then
13+
return
1714
fi
1815

1916
echo "copy code ..."
2017
cp -r ../../platforms/kubernetes/postgres-operator/postgres .
2118

2219
echo "build docker image $image ..."
23-
build_cmd="docker buildx build --no-cache"
24-
#docker_version=$(docker version --format '{{index (split .Server.Version ".") 0}}')
20+
echo "notice: build all image need docker login. "
2521

26-
if [ $platform == "all" ]; then
27-
echo "build all image need docker login. "
28-
builder_exists=$(docker buildx ls | awk '{if ($1=="multi-platform") print $1}')
29-
if [ "$builder_exists" ]; then
30-
docker buildx rm multi-platform
31-
fi
32-
# create a new builder instance
33-
docker buildx create --use --name multi-platform --platform=linux/amd64,linux/arm64
34-
35-
temp=($(echo $image | tr "/" " "))
36-
if [ ${#temp[@]} == 1 ]; then
37-
image="$namespace/$image"
38-
fi
39-
40-
build_cmd="$build_cmd --push --platform linux/amd64,linux/arm64"
41-
else
42-
build_cmd="$build_cmd -o type=docker --platform linux/${platform}"
43-
fi
44-
45-
build_cmd="$build_cmd -t $image ."
22+
build_cmd=$(get_build_cmd "$image" "$platform")
4623
$build_cmd
47-
48-
# remove builder instance
49-
# if [ $platform == "all" ]; then
50-
# docker buildx rm multi-platform
51-
# fi
5224
}
5325

5426
image=$(jq -r '.image' versions.json)

0 commit comments

Comments
 (0)