|
| 1 | +name: Publish Helm Chart |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + component: |
| 7 | + description: 'Component to release' |
| 8 | + required: true |
| 9 | + type: choice |
| 10 | + options: |
| 11 | + - opensandbox-controller |
| 12 | + default: 'opensandbox-controller' |
| 13 | + app_version: |
| 14 | + description: 'App version (without v prefix, e.g., 0.1.0)' |
| 15 | + required: true |
| 16 | + default: '0.1.0' |
| 17 | + push: |
| 18 | + tags: |
| 19 | + - 'helm/**' # Format: helm/<component>/<app_version>, e.g., helm/opensandbox-controller/0.1.0 |
| 20 | + |
| 21 | +jobs: |
| 22 | + publish: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + permissions: |
| 25 | + contents: write |
| 26 | + steps: |
| 27 | + - name: Checkout code |
| 28 | + uses: actions/checkout@v4 |
| 29 | + with: |
| 30 | + fetch-depth: 0 |
| 31 | + |
| 32 | + - name: Configure Git |
| 33 | + run: | |
| 34 | + git config user.name "$GITHUB_ACTOR" |
| 35 | + git config user.email "$GITHUB_ACTOR@users.noreply.github.com" |
| 36 | +
|
| 37 | + - name: Install Helm |
| 38 | + uses: azure/setup-helm@v4 |
| 39 | + with: |
| 40 | + version: 'latest' |
| 41 | + |
| 42 | + - name: Parse tag and set variables |
| 43 | + id: parse_tag |
| 44 | + run: | |
| 45 | + if [[ "${{ github.ref }}" == refs/tags/helm/* ]]; then |
| 46 | + TAG_PATH="${{ github.ref }}" |
| 47 | + TAG_PATH="${TAG_PATH#refs/tags/}" |
| 48 | + |
| 49 | + COMPONENT=$(echo "$TAG_PATH" | cut -d'/' -f2) |
| 50 | + VERSION=$(echo "$TAG_PATH" | cut -d'/' -f3) |
| 51 | + |
| 52 | + # Remove 'v' prefix if present |
| 53 | + VERSION=${VERSION#v} |
| 54 | + |
| 55 | + echo "component=$COMPONENT" >> $GITHUB_OUTPUT |
| 56 | + echo "app_version=$VERSION" >> $GITHUB_OUTPUT |
| 57 | + else |
| 58 | + echo "component=${{ inputs.component }}" >> $GITHUB_OUTPUT |
| 59 | + echo "app_version=${{ inputs.app_version }}" >> $GITHUB_OUTPUT |
| 60 | + fi |
| 61 | +
|
| 62 | + - name: Set chart path |
| 63 | + id: chart_path |
| 64 | + run: | |
| 65 | + COMPONENT="${{ steps.parse_tag.outputs.component }}" |
| 66 | + |
| 67 | + if [ "$COMPONENT" == "opensandbox-controller" ]; then |
| 68 | + CHART_PATH="kubernetes/charts/opensandbox-controller" |
| 69 | + else |
| 70 | + echo "Error: Unknown component: $COMPONENT" |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + |
| 74 | + echo "path=$CHART_PATH" >> $GITHUB_OUTPUT |
| 75 | +
|
| 76 | + - name: Get chart version from Chart.yaml |
| 77 | + id: chart_version |
| 78 | + run: | |
| 79 | + CHART_PATH="${{ steps.chart_path.outputs.path }}" |
| 80 | + CHART_VERSION=$(grep '^version:' $CHART_PATH/Chart.yaml | awk '{print $2}') |
| 81 | + echo "version=$CHART_VERSION" >> $GITHUB_OUTPUT |
| 82 | + echo "Chart version: $CHART_VERSION" |
| 83 | +
|
| 84 | + - name: Update Chart.yaml with app version |
| 85 | + run: | |
| 86 | + APP_VERSION="${{ steps.parse_tag.outputs.app_version }}" |
| 87 | + CHART_PATH="${{ steps.chart_path.outputs.path }}" |
| 88 | + |
| 89 | + # Only update appVersion, keep chart version as-is in Chart.yaml |
| 90 | + sed -i "s/^appVersion:.*/appVersion: \"$APP_VERSION\"/" $CHART_PATH/Chart.yaml |
| 91 | + |
| 92 | + echo "Updated Chart.yaml:" |
| 93 | + cat $CHART_PATH/Chart.yaml |
| 94 | +
|
| 95 | + - name: Lint Helm chart |
| 96 | + run: | |
| 97 | + CHART_PATH="${{ steps.chart_path.outputs.path }}" |
| 98 | + helm lint $CHART_PATH |
| 99 | +
|
| 100 | + - name: Package Helm chart |
| 101 | + run: | |
| 102 | + CHART_PATH="${{ steps.chart_path.outputs.path }}" |
| 103 | + helm package $CHART_PATH |
| 104 | +
|
| 105 | + - name: Create GitHub Release |
| 106 | + uses: softprops/action-gh-release@v1 |
| 107 | + with: |
| 108 | + tag_name: helm/${{ steps.parse_tag.outputs.component }}/${{ steps.parse_tag.outputs.app_version }} |
| 109 | + name: Helm Chart ${{ steps.parse_tag.outputs.component }} ${{ steps.chart_version.outputs.version }} (App v${{ steps.parse_tag.outputs.app_version }}) |
| 110 | + body: | |
| 111 | + ## ${{ steps.parse_tag.outputs.component }} Helm Chart |
| 112 | + |
| 113 | + **Chart Version:** ${{ steps.chart_version.outputs.version }} |
| 114 | + **App Version:** ${{ steps.parse_tag.outputs.app_version }} |
| 115 | + |
| 116 | + ### Installation |
| 117 | + |
| 118 | + 直接从 GitHub Release 安装: |
| 119 | + |
| 120 | + ```bash |
| 121 | + helm install opensandbox \ |
| 122 | + https://github.com/${{ github.repository }}/releases/download/helm/${{ steps.parse_tag.outputs.component }}/${{ steps.parse_tag.outputs.app_version }}/${{ steps.parse_tag.outputs.component }}-${{ steps.chart_version.outputs.version }}.tgz \ |
| 123 | + --namespace opensandbox-system \ |
| 124 | + --create-namespace |
| 125 | + ``` |
| 126 | + |
| 127 | + 或者先下载后安装: |
| 128 | + |
| 129 | + ```bash |
| 130 | + # 下载 |
| 131 | + wget https://github.com/${{ github.repository }}/releases/download/helm/${{ steps.parse_tag.outputs.component }}/${{ steps.parse_tag.outputs.app_version }}/${{ steps.parse_tag.outputs.component }}-${{ steps.chart_version.outputs.version }}.tgz |
| 132 | + |
| 133 | + # 安装 |
| 134 | + helm install opensandbox ./${{ steps.parse_tag.outputs.component }}-${{ steps.chart_version.outputs.version }}.tgz \ |
| 135 | + --namespace opensandbox-system \ |
| 136 | + --create-namespace |
| 137 | + ``` |
| 138 | + |
| 139 | + ### What's Changed |
| 140 | + |
| 141 | + - Chart version: ${{ steps.chart_version.outputs.version }} |
| 142 | + - App version: ${{ steps.parse_tag.outputs.app_version }} |
| 143 | + files: | |
| 144 | + ${{ steps.parse_tag.outputs.component }}-*.tgz |
| 145 | + draft: false |
| 146 | + prerelease: false |
| 147 | + env: |
| 148 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments