2 Commits

Author SHA1 Message Date
cb91ecc9f6 feat(build.py): implement gitea workflow trigger
All checks were successful
Building alt images / build-process (push) Successful in 4m7s
2025-06-23 23:35:14 +03:00
761fda61c1 feat(.gitea/workflow): implement build of arbitrary images 2025-06-23 23:29:59 +03:00
9 changed files with 227 additions and 319 deletions

View File

@ -1,74 +0,0 @@
#!/usr/bin/env python3
import argparse
import json
import subprocess
vers_path = ".gitea/workflows/k8s_vers.json"
def run(args):
result = subprocess.run(
args,
capture_output = True,
text = True
)
if result.stderr is not None:
print(result.stderr)
return False
print(result.stdout)
return True
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--branch",
help="used to pick the appropriate VM template to clone",
choices=["sisyphus", "c10f2", "p10", "p11"],
)
parser.add_argument(
"--other-build-args",
help="other build arg as a string",
)
parser.add_argument(
"--workspace",
help="workspace, path to builing script",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
if args.branch is None or args.branch == "":
raise RuntimeError("Empty branch setting. Check your choise --branch.")
data = {}
with open(f"{args.workspace}/{vers_path}", "r") as f:
data = json.load(f)
i = 0
for br in data.get("branches"):
if br.get("name") == args.branch:
break
i += 1
vers_branch = data.get("branches")[i].get("versions")
if vers_branch == None :
raise RuntimeError(f"Unknown branch: {args.branch!r}. In {vers_path} no k8s-versions list.")
images_to_rebuild="k8s/kube-apiserver k8s/kube-scheduler k8s/kube-controller-manager k8s/kube-proxy k8s/kubelet k8s/coredns k8s/etcd"
for ver in vers_branch:
kube_ver = ver.get("kube-version")
dns_ver = ver.get("coredns")
if kube_ver != None and dns_ver != None:
packs_string=f"-b {args.branch} " + "--package-versions '{\"k8s/kube-apiserver\":" + f"\"{kube_ver}\",\"k8s/kube-scheduler\":\"{kube_ver}\",\"k8s/kube-controller-manager\":\"{kube_ver}\",\"k8s/kube-proxy\":\"{kube_ver}\",\"k8s/kubelet\":\"{kube_ver}\",\"k8s/coredns\":\"{dns_ver}\",\"k8s/etcd\":\"{kube_ver}\"" + "}'"
if ver.get("latest") != None and ver.get("latest"):
packs_string=f"-o k8s --latest {args.branch} "+packs_string
else:
packs_string=f"-i {images_to_rebuild} "+packs_string
subprocess.run(f"{args.workspace}/build.py {args.other_build_args} {packs_string}", shell=True)
subprocess.run("podman rmi -f --all", shell=True)
if __name__ == "__main__":
main()

View File

@ -1,72 +0,0 @@
{
"branches": [
{
"name": "p10",
"versions": [
{
"kube-version": "1.27",
"coredns": "1.10.1"
},
{
"kube-version": "1.28",
"coredns": "1.10.1",
"latest": true
}
]
},
{
"name": "p11",
"versions": [
{
"kube-version": "1.31",
"coredns": "1.11.3"
},
{
"kube-version": "1.32",
"coredns": "1.11.3"
},
{
"kube-version": "1.33",
"coredns": "1.33",
"latest": true
}
]
},
{
"name": "c10f2",
"versions": [
{
"kube-version": "1.31",
"coredns": "1.11.3"
},
{
"kube-version": "1.32",
"coredns": "1.11.3"
},
{
"kube-version": "1.33",
"coredns": "1.33",
"latest": true
}
]
},
{
"name": "sisyphus",
"versions": [
{
"kube-version": "1.31",
"coredns": "1.11.3"
},
{
"kube-version": "1.32",
"coredns": "1.11.3"
},
{
"kube-version": "1.33",
"coredns": "1.33",
"latest": true
}
]
}
]
}

View File

@ -32,20 +32,20 @@ do
if [ -n "$test" ]; then
command="podman run --rm --entrypoint=\"$entrypoint\" $url/$imgpath -c \"$test\""
else
if [ "$IM" = distroless-toybox ]; then
command="podman run -q --rm $url/$imgpath toysh -c true"
if [[ "$IM" == distroless-toybox ]]; then
command="podman run --rm $url/$imgpath toysh -c true"
fi
if [ "$IM" = distroless-true ]; then
command="podman run -q --rm $url/$imgpath \"true\""
command="podman run --rm $url/$imgpath \"true\""
fi
if [ "$IM" = distroless-gotop ]; then
command="podman run -q --rm $url/$imgpath \"--version\""
command="podman run --rm $url/$imgpath \"--version\""
fi
if [ "$IM" = flannel-cni-plugin ]; then
command="podman run -q --rm $url/$imgpath \"/flannel\""
command="podman run --rm $url/$imgpath \"/flannel\""
fi
if [ "$IM" == pause ]; then
command="podman run -q --rm $url/$imgpath \"/pause\" \"-v\""
command="podman run --rm $url/$imgpath \"/pause\" \"-v\""
fi
fi
echo $command
@ -58,7 +58,7 @@ do
then
if [[ -n $(cat $IM.log) ]]
then errors="TEST ERROR OF IMAGE $IM: $(cat $IM.log); $errors"
#else errors="TEST ERROR OF IMAGE $IM: test returned empty error, but exit status was nozero; $errors"
else errors="TEST ERROR OF IMAGE $IM: test returned empty error, but exit status was nozero; $errors"
fi
fi
haserr=false

View File

@ -73,7 +73,7 @@ jobs:
run: |
#build base with riskv64 for sisyphus if it is
if [[ "$BR" == "sisyphus" && "$ORG" == "base" ]]; then echo "${{ gitea.workspace }}/build.py --log-level debug -i base/base -b $BR --latest $BR --registry $BUILD_URL"; ${{ gitea.workspace }}/build.py -i base/base -b "$BR" --latest "$BR" --registry "$BUILD_URL"; fi
build_args="-b $BR --log-level debug --latest $BR -o $ORG --registry $BUILD_URL"
build_args="-b $BR --log-level debug --latest $BR -o $ORG --registry $BUILD_URL --package-versions {\"alt/etcd\":\"3.5.15\"}"
if [[ "$BR" == "sisyphus" ]]; then arches="--arches amd64 386 arm64 loong64 --skip-images base/base"; else arches="--arches amd64 386 arm64"; fi
echo "${{ gitea.workspace }}/build.py $build_args $arches"
${{ gitea.workspace }}/build.py $build_args $arches

View File

@ -1,104 +0,0 @@
name: Full building alt images
on:
push:
tags:
- "*_k8s"
jobs:
build-process:
runs-on: alt-sisyphus
outputs:
branch: ${{ env.BRANCH }}
org: ${{ env.ORG }}
url: ${{ env.URL }}
repo: ${{ env.REPO }}
buildres: ${{ steps.build-script.outcome }}
steps:
- name: Check workspace
run: |
repourl=$(echo $GU | cut -d '/' -f 3)
echo "URL=$repourl" >> ${GITHUB_ENV}
echo $repourl
reponame=$(echo $GR | cut -d '/' -f 1)
echo "REPO=$reponame" >> ${GITHUB_ENV}
echo $reponame
env:
GU: ${{ gitea.server_url }}
GR: ${{ gitea.repository }}
- name: Set repo for c10f2 (Temporary)
if: ${{ contains(github.ref_name, 'c10f2') }}
run: |
echo "event tag=${{ github.ref_name }}"
echo "10.4.0.3 update.altsp.su" >> /etc/hosts
echo "cat /etc/hosts"
cat /etc/hosts
- name: Update apt
uses: actions/init-alt-env@v1
- name: Install requires
run: |
echo "apt-get install -y python3-module-tomli python3-module-jinja2 python3-module-yaml python3-module-requests podman buildah jq curl"
apt-get install -y python3-module-tomli python3-module-jinja2 python3-module-yaml python3-module-requests podman buildah jq curl
echo "apt-get install -y qemu-user-static-binfmt-aarch64 qemu-user-static-binfmt-arm qemu-user-static-binfmt-riscv qemu-user-static-binfmt-loongarch"
apt-get install -y qemu-user-static-binfmt-aarch64 qemu-user-static-binfmt-arm qemu-user-static-binfmt-riscv qemu-user-static-binfmt-loongarch
- name: Parse target branch and tag from events context, save to env
env:
EV: ${{ toJson(gitea.event) }}
run: |
echo $EV | jq '.ref' -r | sed "s/refs\/tags\//BRANCH=/g" | cut -d '_' -f 1
echo $EV | jq '.ref' -r | sed "s/refs\/tags\//BRANCH=/g" | cut -d '_' -f 1 >> ${GITHUB_ENV}
org=$(echo $EV | jq '.ref' -r | sed 's/refs\/tags\///g' | cut -d '_' -f 2)
echo "ORG=$org" >> ${GITHUB_ENV}
echo "ORG=$org"
- name: Check out current repo
uses: actions/checkout@v4
- name: Change vendor label for c10f
if: ${{ contains(github.ref_name, 'c10f') }}
run: |
echo "sed -i 's/ALT Linux Team/BaseALT LLC/g' ${{ gitea.workspace }}/org/$ORG/*/Dockerfile.template ||:"
sed -i 's/ALT Linux Team/BaseALT LLC/g' ${{ gitea.workspace }}/org/$ORG/*/Dockerfile.template ||:
echo "sed -i 's/ALT Linux Team/BaseALT LLC/g' ${{ gitea.workspace }}/org/$ORG/*/distroless.toml ||:"
sed -i 's/ALT Linux Team/BaseALT LLC/g' ${{ gitea.workspace }}/org/$ORG/*/distroless.toml ||:
env:
ORG: ${{ env.ORG }}
- name: Login podman gitea
run: |
echo "podman login ${{ env.URL }}"
podman login --username $P_USER --password $P_PASS ${{ env.URL }}
env:
P_USER: ${{ secrets.PODMAN_USER }}
P_PASS: ${{ secrets.PODMAN_PASS }}
- name: Run building script
id: build-script
run: |
if [[ "$BR" == "sisyphus" ]]; then arches="--arches amd64 386 arm64 loong64"; else arches="--arches amd64 386 arm64"; fi
build_args="--registry $BUILD_URL $arches"
#run build script
${{ gitea.workspace }}/.gitea/workflows/k8s_buildscript.py --branch "$BR" --other-build-args "$build_args" --workspace "${{ gitea.workspace }}"
env:
ORG: ${{ env.ORG }}
BR: ${{ env.BRANCH }}
BUILD_URL: "gitea.basealt.ru/alt"
continue-on-error: true
- name: Send notification if build crashed
if: ${{ steps.build-script.outcome != 'success' }}
run: |
issueid=1
body="Building images finish with some errors."
curl -X 'POST' "$URL/api/v1/repos/$REPO/image-forge/issues/$issueid/comments?token=$T" -H 'accept: application/json' -H 'Content-Type: application/json' -d "{ \"body\": \"$body\" }" -s
echo "notification about test error is sent to issue $issueid"
env:
T: ${{ secrets.TOKEN }}
BR: ${{ env.BRANCH }}
URL: ${{ gitea.server_url }}
REPO: ${{ env.REPO }}
- name: Delete event tag
run: |
tagname=$(echo $EV | jq '.ref' -r | sed "s/refs\/tags\///g")
curl -X 'DELETE' "$URL/api/v1/repos/$REPO/image-forge/tags/$tagname?token=$T" -H 'accept: application/json' -s
echo "tag $tagname is deleted"
env:
T: ${{ secrets.TOKEN }}
BR: ${{ env.BRANCH }}
URL: ${{ gitea.server_url }}
REPO: ${{ env.REPO }}
EV: ${{ toJson(gitea.event) }}

View File

@ -0,0 +1,155 @@
name: Building alt images
on:
push:
tags:
- '**'
jobs:
build-process:
runs-on: alt-sisyphus
outputs:
branch: ${{ env.BRANCH }}
image: ${{ env.IMAGE }}
url: ${{ env.URL }}
repo: ${{ env.REPO }}
buildres: ${{ steps.build-script.outcome }}
test: ${{ env.TEST }}
steps:
- name: Check workspace
run: |
repourl=$(echo $GU | cut -d '/' -f 3)
echo "URL=$repourl" >> ${GITHUB_ENV}
echo "URL=$repourl"
reponame=$(echo $GR | cut -d '/' -f 1)
echo "REPO=$reponame" >> ${GITHUB_ENV}
echo "REPO=$reponame"
env:
GU: ${{ gitea.server_url }}
GR: ${{ gitea.repository }}
- name: Set repo for c10f2 (Temporary)
if: ${{ contains(github.ref_name, 'c10f2') }}
run: |
echo "event tag=${{ github.ref_name }}"
echo "10.4.0.3 update.altsp.su" >> /etc/hosts
echo "cat /etc/hosts"
cat /etc/hosts
- name: Update apt
uses: actions/init-alt-env@v1
- name: Install requires
run: |
echo "apt-get install -y python3-module-tomli python3-module-jinja2 python3-module-yaml python3-module-requests podman buildah jq curl"
apt-get install -y python3-module-tomli python3-module-jinja2 python3-module-yaml python3-module-requests podman buildah jq curl
echo "apt-get install -y qemu-user-static-binfmt-aarch64 qemu-user-static-binfmt-arm qemu-user-static-binfmt-ppc qemu-user-static-binfmt-riscv qemu-user-static-binfmt-loongarch"
apt-get install -y qemu-user-static-binfmt-aarch64 qemu-user-static-binfmt-arm qemu-user-static-binfmt-ppc qemu-user-static-binfmt-riscv qemu-user-static-binfmt-loongarch
- name: Check out current repo
uses: actions/checkout@v4
- name: Login podman gitea
run: |
echo "podman login ${{ env.URL }}"
podman login --username $P_USER --password $P_PASS ${{ env.URL }}
env:
P_USER: ${{ secrets.PODMAN_USER }}
P_PASS: ${{ secrets.PODMAN_PASS }}
- name: Check files in the repository
run: |
ls -a ${{ gitea.workspace }}
- name: Parse target branch and tag from events context, save to env
env:
EV: ${{ toJson(gitea.event) }}
run: |
branch="$(echo $EV | jq '.ref' -r | sed "s|refs/tags/||g" | cut -d '_' -f 1)"
echo "BRANCH=$branch"
rest="$(echo $EV | jq '.ref' -r | sed "s|refs/tags/||g" | cut -d '_' -f 2-)"
IFS='_' read -ra parts <<< "$rest"
images=()
declare -A versions
for part in "${parts[@]}"; do
if [[ "$part" == *@* ]]; then
image="${part%@*}"
version="${part#*@}"
images+=("$image")
versions["$image"]="$version"
else
image="$part"
images+=("$image")
fi
done
image_args="${images[*]}"
package_versions="{"
first=1
for image in "${!versions[@]}"; do
[[ $first -eq 0 ]] && package_versions+=","
package_versions+="\"$image\": \"${versions[$image]}\""
first=0
done
package_versions+="}"
echo "IMAGES=$image_args"
echo "PACKAGE_VERSIONS=$package_versions"
echo "BRANCH=$branch" >> ${GITHUB_ENV}
echo "IMAGES=$image_args" >> $GITHUB_ENV
echo "PACKAGE_VERSIONS=$package_versions" >> $GITHUB_ENV
- name: Change vendor label for c10f
if: ${{ contains(github.ref_name, 'c10f') }}
run: |
echo "sed -i 's/ALT Linux Team/BaseALT LLC/g' ${{ gitea.workspace }}/org/$ORG/*/Dockerfile.template ||:"
sed -i 's/ALT Linux Team/BaseALT LLC/g' ${{ gitea.workspace }}/org/$ORG/*/Dockerfile.template ||:
echo "sed -i 's/ALT Linux Team/BaseALT LLC/g' ${{ gitea.workspace }}/org/$ORG/*/distroless.toml ||:"
sed -i 's/ALT Linux Team/BaseALT LLC/g' ${{ gitea.workspace }}/org/$ORG/*/distroless.toml ||:
env:
ORG: ${{ env.ORG }}
- name: Run building script
id: build-script
run: |
echo "${{ gitea.workspace }}/build.py \
--log-level debug \
--skip-stages push \
--branch $BRANCH \
--registry gitea.basealt.ru/alt \
--arches amd64 \
-i $IMAGES \
--package-versions \"$PACKAGE_VERSIONS\""
${{ gitea.workspace }}/build.py \
--log-level debug \
--skip-stages push \
--branch $BRANCH \
--registry gitea.basealt.ru/alt \
--arches amd64 \
-i $IMAGES \
--package-versions "$PACKAGE_VERSIONS"
env:
BRANCH: ${{ env.BRANCH }}
IMAGES: ${{ env.IMAGES }}
PACKAGE_VERSIONS: ${{ env.PACKAGE_VERSIONS }}
continue-on-error: true
# - name: Send notification if build crashed
# if: ${{ steps.build-script.outcome != 'success' }}
# run: |
# issueid=1
# body="Building image $IM finish with some errors."
# curl -X 'POST' "$URL/api/v1/repos/$REPO/image-forge/issues/$issueid/comments?token=$T" -H 'accept: application/json' -H 'Content-Type: application/json' -d "{ \"body\": \"$body\" }" -s
# echo "notification about test error is sent to issue $issueid"
# env:
# T: ${{ secrets.TOKEN }}
# BR: ${{ env.BRANCH }}
# URL: ${{ gitea.server_url }}
# REPO: ${{ env.REPO }}
# IM: ${{ env.IMAGE }}
- name: Delete event tag
run: |
tagname=$(echo $EV | jq '.ref' -r | sed "s|refs/tags/||g")
curl -X 'DELETE' "$URL/api/v1/repos/$REPO/image-forge/tags/$tagname" -H "Authorization: token $T" -H 'accept: application/json' -s
echo "tag $tagname is deleted"
env:
T: ${{ secrets.TOKEN }}
BR: ${{ env.BRANCH }}
URL: ${{ gitea.server_url }}
REPO: ${{ env.REPO }}
EV: ${{ toJson(gitea.event) }}

115
build.py
View File

@ -4,12 +4,14 @@ import argparse
import functools
import json
import logging
import os
import re
import subprocess
import textwrap
from datetime import datetime
from dataclasses import dataclass
from graphlib import TopologicalSorter
from http import HTTPStatus
from pathlib import Path
from typing import Optional, Union
@ -19,8 +21,6 @@ import yaml
from jinja2 import Environment, BaseLoader
logger = logging.getLogger(__name__)
clean_images_counter = 0
clean_images_limit_count = 0
ORG_DIR = Path("org")
@ -343,7 +343,6 @@ class DockerBuilder:
def __init__(
self,
registry,
overwrite_registry,
branch,
organization,
overwrite_organization,
@ -358,7 +357,6 @@ class DockerBuilder:
self.org_dir = ORG_DIR
self.images_dir = ORG_DIR / organization
self.registry = registry
self.overwrite_registry = overwrite_registry
self.branch = branch
self.organization = organization
if overwrite_organization:
@ -515,15 +513,6 @@ class DockerBuilder:
tag = f":{tag}"
return f"{registry}{self.overwrite_organization}/{image.base_name}{tag}"
def render_push_registry(self, image: Image, tag: str):
if self.overwrite_registry:
registry = self.overwrite_registry.rstrip("/") + "/"
else:
registry = ""
if tag:
tag = f":{tag}"
return f"{registry}{self.overwrite_organization}/{image.base_name}{tag}"
def run(self, cmd, *args, **kwargs):
if "check" not in kwargs:
kwargs["check"] = True
@ -791,44 +780,22 @@ class DockerBuilder:
return
tags = self.tags.tags(self.branch, image, self.tasks)
manifests = [self.render_full_tag(image, t) for t in tags]
for t in tags:
manifest = self.render_full_tag(image, t)
push_manifest = self.render_push_registry(image, t)
print(f"Source manifest {manifest}")
print(f"Push manifest {push_manifest}")
for manifest in manifests:
print(f"Push manifest {manifest}")
cmd = [
"podman",
"manifest",
"push",
manifest,
f"docker://{push_manifest}",
f"docker://{manifest}",
]
if sign is not None:
cmd.append(f"--sign-by={sign}")
self.run(cmd)
if clean_images_limit_count > 0:
global clean_images_counter
if clean_images_limit_count <= clean_images_counter:
cmd = [
"podman",
"rmi",
"--all",
"-f",
]
self.run(cmd,
check=False,
stderr=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
)
clean_images_counter = 0
else:
clean_images_counter += 1
class ImagesInfo:
@ -900,22 +867,9 @@ def parse_args():
"-r",
"--registry",
default="gitea.basealt.ru",
help="source registry",
)
parser.add_argument(
"--clean-images-limit",
default="5",
type=int,
help="limit count of local images, '0' - no clean mode",
)
parser.add_argument(
"--overwrite-registry",
default="gitea.basealt.ru",
help="destination registry",
)
parser.add_argument(
"--overwrite-organization",
help="destination organization",
)
parser.add_argument(
"-l",
@ -996,6 +950,23 @@ def parse_args():
choices=log_levels,
help="log messages above specified level",
)
parser.add_argument(
"--run-workflow",
action="store_true",
help="run gitea workflow to build oci images",
)
parser.add_argument(
"--workflow-repo",
default="stepchenkoas/image-forge",
help="path to gitea repository where to activate workflow",
)
parser.add_argument(
"--workflow-branch",
default="master",
help="branch in gitea repository where to activate workflow",
)
args = parser.parse_args()
args.stages = set(args.stages) - set(args.skip_stages)
@ -1003,9 +974,6 @@ def parse_args():
args.branches = set(args.branches) - set(args.skip_branches)
args.images = set(args.images) - set(args.skip_images)
global clean_images_limit_count
clean_images_limit_count = args.clean_images_limit
return args
@ -1022,6 +990,42 @@ def main():
logger.info("PKG_VERSIONS=%s", PKG_VERSIONS)
if args.run_workflow:
gitea_token = os.environ.get("GITEA_TOKEN")
if gitea_token is None:
raise RuntimeError("Gitea authorization token is not provided through the environment variable GITEA_TOKEN")
api_url = f"https://gitea.basealt.ru/api/v1/repos/{args.workflow_repo}/tags"
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"token {gitea_token}",
}
tag_name = list(args.branches)[0]
for image in args.images:
tag_name += "_" + image
if PKG_VERSIONS is not None:
if (version := PKG_VERSIONS.get(image)) is not None:
tag_name += "@" + version
logger.debug("tag_name=\"%s\"", tag_name)
json = {
"message": "workflow trigger",
"target": args.workflow_branch,
"tag_name": tag_name,
}
response = requests.post(api_url, json=json, headers=headers)
if response.status_code != HTTPStatus.CREATED:
print(f"{response.status_code=}")
raise RuntimeError(
f"failed to run workflow: {response.text!r}"
)
return
arches = args.arches
images_info = ImagesInfo()
tags = Tags(args.tags, args.latest)
@ -1029,7 +1033,6 @@ def main():
for branch in args.branches:
db = DockerBuilder(
args.registry,
args.overwrite_registry,
branch,
organization,
args.overwrite_organization,

View File

@ -1,14 +1,14 @@
["alt/distroless-toybox"]
skip-branches = [ "p10", "c10f2", "c10f1" ]
["base/distroless-toybox"]
skip-branches = [ "p10", "c10f2", "c10f1" ]
["alt/openjdk21"]
skip-arches = [ "386" ]
["alt/zot"]
skip-arches = [ "386" ]
["alt/ansible"]
skip-branches = [ "c10f2", "c10f1", "p10" ]
["alt/buildkit"]
skip-branches = [ "c10f1", "p10" ]
skip-branches = [ "c10f1", "p10" ]

View File

@ -16,6 +16,6 @@ RUN useradd -g qemu -m -d /home/qemu -s /bin/bash -c "qemu user" -u 107 qemu > /
"kubevirt-container-disk"
) }}
RUN cp -f /usr/share/kube-virt/virt-handler/nsswitch.conf /etc/
RUN cp -f /usr/share/kube-virt/virt-handler/nsswitch.conf /etc/ && cp -f /usr/share/kube-virt/virt-handler/virt_launcher.cil /
ENTRYPOINT ["/usr/bin/virt-handle"]