From b098fa7d0b5b031f0a3a352ff9eedbdce7eab4a9 Mon Sep 17 00:00:00 2001 From: "bo.jiang" Date: Sun, 19 Nov 2023 00:34:47 +0800 Subject: [PATCH] update kubespray artifact patch CI Signed-off-by: bo.jiang --- .../workflows/manual-kubespray-release.yaml | 23 ++++- artifacts/gen_artifact_patch_md.py | 84 +++++++++++++++++++ hack/sync-manifest.sh | 2 +- 3 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 artifacts/gen_artifact_patch_md.py diff --git a/.github/workflows/manual-kubespray-release.yaml b/.github/workflows/manual-kubespray-release.yaml index 37e21bec8..cdd2f2886 100644 --- a/.github/workflows/manual-kubespray-release.yaml +++ b/.github/workflows/manual-kubespray-release.yaml @@ -194,7 +194,8 @@ jobs: mkdir manifest && cp charts/kubean/templates/manifest.cr.yaml manifest/manifest-${{ needs.check-inputs.outputs.tag }}.yml echo "manifest cr output:" cat manifest/manifest-${{ needs.check-inputs.outputs.tag }}.yml - + rm -rf kubespray/ + - name: Push Kubean manifest to another repository env: REPO_OWNER: ${{ github.repository_owner }} @@ -203,6 +204,26 @@ jobs: run: | ./hack/sync-manifest.sh + - name: Generate artifacts patch markdown doc + env: + IMAGE_REPO: ${{ needs.check-inputs.outputs.image_repo }} + run: | + python artifacts/gen_artifact_patch_md.py + mv artifacts.md docs/overrides/artifacts.md + + - name: Push artifact patch md + id: push_directory + uses: cpina/github-action-push-to-another-repository@v1.7.2 + env: + SSH_DEPLOY_KEY: ${{ secrets.SYNC_RLS_PRIVATE_KEY }} + with: + source-directory: . + destination-github-username: ${{ github.repository_owner }} + destination-repository-name: kubean + user-email: kubean-robot@kubean-io + commit-message: Update artifacts.md, See ORIGIN_COMMIT from $GITHUB_REF + target-branch: main + show-artifacts: needs: [check-inputs, build-kubespray-image, build-sprayjob-image, build-airgap-patch-image, gen-manifest] runs-on: ubuntu-latest diff --git a/artifacts/gen_artifact_patch_md.py b/artifacts/gen_artifact_patch_md.py new file mode 100644 index 000000000..8d074307c --- /dev/null +++ b/artifacts/gen_artifact_patch_md.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python + +import subprocess +import yaml +import os +from jinja2 import Template +from datetime import datetime + +IMAGE_REPO = os.getenv("IMAGE_REPO", default="kubean-io") + +KUBEAN_PATCH_TEMPLATE = ''' +{% for release, infos in releases.items() %} +### ▶️ release-{{ release }} +> ⚓ kube_version range: [ {{ infos[0].kube_version_range[infos[0].kube_version_range|length-1] }} ~ {{ infos[0].kube_version_range[0] }} ] + + + + + + + + + {% for info in infos %} + + + + + + + + + + + + + {% endfor %} +
Commit DateArtifacts
📅 {{ info.commit_date }} + 📝 manifest-{{ release }}-{{ info.commit_short_sha }}.yml +
📦 {{ image_registry }}/{{ repo_name }}/spray-job:{{ release }}-{{ info.commit_short_sha }}
📦 {{ image_registry }}/{{ repo_name }}/airgap-patch:{{ release }}-{{ info.commit_short_sha }}
+{% endfor %} +''' + + +if __name__ == '__main__': + + repo_name="kubean-manifest" + subprocess.getoutput(f"git clone https://github.com/{IMAGE_REPO}/{repo_name}.git") + manifests_path=f'{repo_name}/manifests' + + manifest_files = subprocess.getoutput(f"ls {manifests_path}") + + release_keys=['2.21', '2.22', '2.23'] + releases = {key: [] for key in release_keys} + for manifest in manifest_files.splitlines(): + for key in release_keys: + if key in manifest: + with open(f'{manifests_path}/{manifest}', 'r') as stream: + data = yaml.safe_load(stream) + commit_short_sha = data.get('metadata', {}).get('annotations', {}).get('kubean.io/sprayCommit') + commit_timestamp = int(data.get('metadata', {}).get('annotations', {}).get('kubean.io/sprayTimestamp')) + commit_date = datetime.fromtimestamp(commit_timestamp) + components = data.get('spec', {}).get('components', {}) + kube_info = next(item for item in data.get('spec', {}).get('components', {}) if item['name'] == 'kube') + kube_version_range = kube_info.get('versionRange', []) + releases[key].append({ + 'commit_short_sha': commit_short_sha, + 'commit_timestamp': commit_timestamp, + 'commit_date': commit_date, + 'kube_version_range': kube_version_range}) + + # print(f'releases: {releases}') + + for key in release_keys: + releases[key].sort(key=lambda item:item['commit_timestamp'], reverse=True) + + t = Template(KUBEAN_PATCH_TEMPLATE) + md_contents = t.render(releases=releases, image_registry='ghcr.io', repo_name=IMAGE_REPO) + # print(md_contents) + + artifact_md = open('artifacts.md', 'w') + artifact_md.write(md_contents) + artifact_md.close() + + subprocess.getoutput(f"rm -rf {repo_name}") diff --git a/hack/sync-manifest.sh b/hack/sync-manifest.sh index 156ea6bd8..fa14f45c5 100755 --- a/hack/sync-manifest.sh +++ b/hack/sync-manifest.sh @@ -45,7 +45,7 @@ chmod 600 "$DEPLOY_KEY_FILE" SSH_KNOWN_HOSTS_FILE="$HOME/.ssh/known_hosts" ssh-keyscan -H "${GITHUB_SERVER}" > "$SSH_KNOWN_HOSTS_FILE" -export GIT_SSH_COMMAND="ssh -i "$DEPLOY_KEY_FILE" -o UserKnownHostsFile=$SSH_KNOWN_HOSTS_FILE" +export GIT_SSH_COMMAND="ssh -i ${DEPLOY_KEY_FILE} -o UserKnownHostsFile=$SSH_KNOWN_HOSTS_FILE" GIT_CMD_REPOSITORY="git@${GITHUB_SERVER}:${REPO_OWNER}/${DST_REPO}.git" git clone --single-branch --depth 1 --branch "$DST_BRANCH" "$GIT_CMD_REPOSITORY" "$TMP_DIR"