Sfoglia il codice sorgente

Add sync mechanism

Jordon Leach 1 anno fa
parent
commit
817bc3506f
3 ha cambiato i file con 248 aggiunte e 0 eliminazioni
  1. 58 0
      .github/workflows/sync-extensions.yml
  2. 3 0
      manifest.json
  3. 187 0
      scripts/sync

+ 58 - 0
.github/workflows/sync-extensions.yml

@@ -0,0 +1,58 @@
+name: Sync and Release Extensions
+
+on:
+  push:
+    branches:
+      - main
+    paths:
+      - manifest.json
+
+env:
+  ACTIONS_RUNNER_DEBUG: false
+  CI_COMMIT_MESSAGE: CI Build Artifacts
+
+jobs:
+  sync:
+    if: github.repository_owner == 'rancher'
+    name: Sync and Release Extensions
+    runs-on: ubuntu-latest
+    permissions: write-all
+    steps:
+      - name: Checkout
+        uses: actions/checkout@v3
+
+      - name: Configure Git
+        run: |
+          git config user.name github-actions
+          git config user.email github-actions@github.com
+      
+      - name: Setup Helm
+        uses: azure/setup-helm@v3
+        with:
+          version: v3.12.1
+
+      - name: Setup yq
+        uses: chrisdickinson/setup-yq@v1.0.1
+        with:
+          yq-version: v4.34.2
+
+      - name: Run sync script
+        shell: bash
+        id: sync_script
+        run: |
+          chmod +x ./scripts/sync
+          ./scripts/sync
+
+      - name: Commit build
+        run: |
+          git add ./{assets,charts,extensions,icons,index.yaml}
+          git commit -a -m "${{ env.CI_COMMIT_MESSAGE }}"
+          git push
+
+      - name: Run chart-releaser
+        uses: helm/chart-releaser-action@v1.5.0
+        with:
+          charts_dir: ./charts/*
+        env:
+          CR_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
+          CR_SKIP_EXISTING: true

+ 3 - 0
manifest.json

@@ -0,0 +1,3 @@
+{
+  "extensions": {}
+}

+ 187 - 0
scripts/sync

@@ -0,0 +1,187 @@
+#!/usr/bin/env bash
+
+SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
+BASE_DIR="$( cd $SCRIPT_DIR && cd .. & pwd)"
+
+CYAN="\033[96m"
+YELLOW="\033[93m"
+RESET="\033[0m"
+BOLD="\033[1m"
+NORMAL="\033[22m"
+CHECK="\xE2\x9C\x94"
+
+ORG=rancher
+BRANCH=main
+CLEAN=false
+
+usage() {
+  echo "Usage: $0 [<options>]"
+  echo " options:"
+  echo "  [-c | --clean]                   Clean the current extension assets including: ./{charts, assets, extensions, icons}"
+  echo "  [-o | --org] <name>              Specify the organization of the current repository (defaults to 'rancher')"
+  echo "  [-b | --branch] <name>           Specify the destination branch of the extension assets within the current repository (defaults to 'main')"
+  exit 1
+}
+
+while [[ $# -gt 0 ]]; do
+  case "$1" in
+    -h|--help)
+      usage
+      ;;
+    -o|--org)
+      if [[ -z $2 || $2 == -* ]]; then
+        echo "Error: Missing argument for $1 option"
+        usage
+      fi
+      ORG="${2}"
+      shift 2
+      ;;
+    -c|--clean)
+      CLEAN=true
+      shift
+      ;;
+    -b|--branch)
+      if [[ -z $2 || $2 == -* ]]; then
+        echo "Error: Missing argument for $1 option"
+        usage
+      fi
+      BRANCH="${2}"
+      shift 2
+      ;;
+    *)
+      echo "Error: Unknown option $1"
+      usage
+      ;;
+  esac
+done
+
+shift $((OPTIND-1))
+
+if [[ $CLEAN == true ]]; then
+  rm -rf ./charts
+  rm -rf ./assets
+  rm -rf ./extensions
+  rm -rf ./icons
+fi
+
+echo -e "${CYAN}${BOLD}Syncing Extensions${RESET}"
+
+EXTS=$(jq -r ".extensions | keys[]" manifest.json)
+
+TMP=${BASE_DIR}/tmp
+rm -rf ${TMP}
+mkdir -p ${TMP}
+
+HELM_INDEX=${BASE_DIR}/index.yaml
+CHART_TMP=${TMP}/_charts
+
+REPOSITORY=${ORG}/partner-extensions
+
+echo "GitHub Repository: ${REPOSITORY}"
+echo "GitHub Branch:     ${BRANCH}"
+
+for NAME in ${EXTS}
+do
+  echo -e "${CYAN} + Syncing: ${BOLD}${NAME}${RESET}"
+
+  # Make diretories for assets, charts, and extensions
+  mkdir -p ./assets/${NAME}
+  mkdir -p ./charts/${NAME}
+  mkdir -p ./extensions/${NAME}
+
+  # Get repository name, branch, and versions 
+  REPO=$(jq -r ".extensions.${NAME}.repo" manifest.json)
+  EXT_BRANCH=$(jq -r ".extensions.${NAME}.branch" manifest.json)
+  VERSIONS=$(jq -r ".extensions.${NAME}.versions[]" manifest.json)
+  VFORMAT=$(echo $VERSIONS | tr '\n' ' ')
+
+  echo -e "     Repository: ${REPO}"
+  echo -e "     Branch: ${EXT_BRANCH}"
+  echo -e "     Versions  : ${VFORMAT}"
+  echo ""
+
+  echo -e "  .. Cloning repository"
+  rm -rf ./tmp/${NAME}
+  pushd tmp > /dev/null
+  git clone https://github.com/${REPO}.git ${NAME}
+  cd ${NAME}
+  git checkout ${EXT_BRANCH}
+  pwd
+  popd > /dev/null
+
+  for VERSION in ${VERSIONS}
+  do
+    echo -e "${CYAN}     Syncing: ${BOLD}${NAME}@${VERSION}${RESET}"
+
+    if [ -d "./charts/${NAME}/${VERSION}" ]; then
+      echo "      Version already synced"
+    else
+      echo "     + Copying version ${VERSION}"
+      # cp ./tmp/${NAME}/extensions/${NAME}/${NAME}-${VERSION}.tgz ./assets/${NAME}
+      cp -R ./tmp/${NAME}/extensions/${NAME}/${VERSION} ./extensions/${NAME}/${VERSION}
+      cp -R ./tmp/${NAME}/charts/${NAME}/${VERSION} ./charts/${NAME}/${VERSION}
+
+      # Need to patch the charts
+      echo "     + Patching Helm chart"
+      CR_FILE=./charts/${NAME}/${VERSION}/templates/cr.yaml
+      ENDPOINT=https://raw.githubusercontent.com/${REPOSITORY}/${BRANCH}/extensions/${NAME}/${VERSION}
+      sed -i.bak -e 's@endpoint:.*@endpoint: '"$ENDPOINT"'@' ${CR_FILE}
+      rm -f ${CR_FILE}.bak
+
+      echo "     + Fetching and patching icon"
+
+      # Check the chart file for an icon
+      CHART_FILE=./charts/${NAME}/${VERSION}/Chart.yaml
+
+      ICON=$(sed -nr 's/icon: (.*)/\1/p' ${CHART_FILE})
+      
+      if [ -n "${ICON}" ]; then
+        # Downloading icon
+        ICON_FILE=$(basename $ICON)
+        echo "     + Fetching icon: ${ICON}"
+        ICON_REL=icons/${NAME}/${VERSION}-${ICON_FILE}
+        mkdir -p ${BASE_DIR}/icons/${NAME}
+        rm -f ${BASE_DIR}/${ICON_REL}
+        wget --quiet -O ${BASE_DIR}/${ICON_REL} ${ICON}
+
+        NEW_ICON=https://raw.githubusercontent.com/${REPOSITORY}/${BRANCH}/${ICON_REL}
+
+        sed -i.bak -e 's@icon:.*@icon: '"$NEW_ICON"'@' ${CHART_FILE}
+        rm -rf ${CHART_FILE}.bak
+
+        PKG_FILE=${BASE_DIR}/extensions/${NAME}/${VERSION}/plugin/package.json
+        sed -i.bak -e 's@\"icon\": \".*\"@\"icon\": \"'"$NEW_ICON"'\"@' ${PKG_FILE}
+        rm -rf ${PKG_FILE}.bak
+      fi
+
+      echo "     + Packaging Helm chart"
+      helm package ./charts/${NAME}/${VERSION} -d ./assets/${NAME}
+
+      echo "     + Updating Helm index"
+
+      # --------------------------------------------------------------------------------
+      # Update the helm index just for this chart 
+      # --------------------------------------------------------------------------------
+      if [ -f "${HELM_INDEX}" ]; then
+        UPDATE="--merge ${HELM_INDEX}"
+      fi
+
+      # Base URL referencing assets directly from GitHub
+      BASE_URL="assets/${NAME}"
+
+      rm -rf ${CHART_TMP}
+      mkdir -p ${CHART_TMP}
+      cp ${BASE_DIR}/assets/${NAME}/${NAME}-${VERSION}.tgz ${CHART_TMP}
+
+      helm repo index ${CHART_TMP} --url ${BASE_URL} ${UPDATE}
+
+      cp ${CHART_TMP}/index.yaml ${HELM_INDEX}
+    fi
+  done
+
+  echo ""
+done
+
+# Clean up
+rm -rf ${CHART_TMP}
+rm -rf ${TMP}