cd-pipeline.yml 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # Copyright Broadcom, Inc. All Rights Reserved.
  2. # SPDX-License-Identifier: APACHE-2.0
  3. name: '[CI/CD] CD Pipeline'
  4. on: # rebuild any PRs and main branch changes
  5. push:
  6. branches:
  7. - main
  8. paths:
  9. - 'bitnami/**'
  10. - '!**.md'
  11. # Remove all permissions by default.
  12. permissions: {}
  13. jobs:
  14. get-chart:
  15. runs-on: ubuntu-latest
  16. name: 'Get modified charts'
  17. permissions:
  18. contents: read
  19. outputs:
  20. chart: ${{ steps.get-chart.outputs.chart }}
  21. result: ${{ steps.get-chart.outputs.result }}
  22. if: ${{ github.repository_owner == 'bitnami' }}
  23. steps:
  24. - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
  25. with:
  26. path: charts
  27. fetch-depth: 2 # to be able to obtain files changed in the latest commit
  28. - id: get-chart
  29. name: 'Get modified charts'
  30. run: |
  31. cd charts
  32. files_changed="$(git show --pretty="" --name-only)"
  33. # Adding || true to avoid "Process exited with code 1" errors
  34. charts_dirs_changed="$(echo "$files_changed" | xargs dirname | grep -o "bitnami/[^/]*" | sort | uniq || true)"
  35. # Using grep -c as a better alternative to wc -l when dealing with empty strings."
  36. num_charts_changed="$(echo "$charts_dirs_changed" | grep -c "bitnami" || true)"
  37. num_version_bumps="$(echo "$files_changed" | grep "bitnami/[^/]*/Chart.yaml" | xargs git show | grep -c "+version" || true)"
  38. if [[ "$num_charts_changed" -ne "$num_version_bumps" ]]; then
  39. # Changes done in charts but version not bumped -> ERROR
  40. charts_changed_str="$(echo ${charts_dirs_changed[@]})"
  41. echo "error=Detected changes in charts without version bump in Chart.yaml. Charts changed: ${num_charts_changed} ${charts_changed_str}. Version bumps detected: ${num_version_bumps}" >> $GITHUB_OUTPUT
  42. echo "result=fail" >> $GITHUB_OUTPUT
  43. elif [[ "$num_charts_changed" -eq "1" ]]; then
  44. # Changes done in only one chart -> OK
  45. chart_name=$(echo "$charts_dirs_changed" | sed "s|bitnami/||g")
  46. echo "chart=${chart_name}" >> $GITHUB_OUTPUT
  47. echo "result=ok" >> $GITHUB_OUTPUT
  48. else
  49. # Changes done in more than chart -> FAIL
  50. charts_changed_str="$(echo ${charts_dirs_changed[@]})"
  51. echo "error=Changes detected in more than one chart directory: ${charts_changed_str}. The publish process will be stopped. Please create different commits for each chart." >> $GITHUB_OUTPUT
  52. echo "result=fail" >> $GITHUB_OUTPUT
  53. fi
  54. - id: show-error
  55. name: 'Show error'
  56. if: ${{ steps.get-chart.outputs.result == 'fail' }}
  57. uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea
  58. with:
  59. script: |
  60. core.setFailed('${{ steps.get-chart.outputs.error }}')
  61. vib-publish:
  62. runs-on: ubuntu-latest
  63. needs: get-chart
  64. if: ${{ needs.get-chart.outputs.result == 'ok' }}
  65. name: VIB Publish
  66. permissions:
  67. contents: read
  68. env:
  69. CSP_API_URL: https://console.tanzu.broadcom.com
  70. CSP_API_TOKEN: ${{ secrets.CSP_API_TOKEN }}
  71. VIB_PUBLIC_URL: ${{ vars.VIB_PUBLIC_URL }}
  72. steps:
  73. - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
  74. name: Checkout Repository
  75. with:
  76. path: charts
  77. - uses: vmware-labs/vmware-image-builder-action@v0
  78. name: Publish ${{ needs.get-chart.outputs.chart }}
  79. with:
  80. pipeline: ${{ needs.get-chart.outputs.chart }}/vib-publish.json
  81. config: charts/.vib/
  82. env:
  83. VIB_PUBLIC_URL: ${{ vars.VIB_PUBLIC_URL }}
  84. VIB_ENV_S3_URL: s3://${{ secrets.AWS_S3_BUCKET }}/bitnami
  85. VIB_ENV_S3_ACCESS_KEY_ID: ${{ secrets.AWS_PUBLISH_ACCESS_KEY_ID }}
  86. VIB_ENV_S3_SECRET_ACCESS_KEY: ${{ secrets.AWS_PUBLISH_SECRET_ACCESS_KEY }}
  87. VIB_ENV_S3_ROLE_ARN: ${{ secrets.AWS_PUBLISH_ROLE_ARN }}
  88. # Set docker credentials
  89. VIB_ENV_CHARTS_REGISTRY: oci://registry-1.docker.io/bitnamicharts
  90. VIB_ENV_CHARTS_REGISTRY_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
  91. VIB_ENV_CHARTS_REGISTRY_PASSWORD: ${{ secrets.DOCKERHUB_TOKEN }}
  92. update-deprecated-index:
  93. runs-on: ubuntu-latest
  94. needs:
  95. - vib-publish
  96. name: Update branch deprecated-index
  97. steps:
  98. - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093
  99. with:
  100. path: ~/artifacts
  101. # If we perform a checkout of the main branch, we will find conflicts with the submodules
  102. - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
  103. with:
  104. ref: 'deprecated-index'
  105. path: 'deprecated-index'
  106. # The token is persisted in the local git config and enables scripts to run authenticated git commands.
  107. token: ${{ secrets.BITNAMI_BOT_TOKEN }}
  108. - name: Install helm
  109. run: |
  110. HELM_TARBALL="helm-v3.8.1-linux-amd64.tar.gz"
  111. curl -SsLfO "https://get.helm.sh/${HELM_TARBALL}" && sudo tar xf "$HELM_TARBALL" --strip-components 1 -C /usr/local/bin
  112. # Install file plugin
  113. helm plugin add https://github.com/zoobab/helm_file_repo
  114. - id: update-deprecated-index
  115. name: Fetch chart and update depreacted-index
  116. env:
  117. AWS_ACCESS_KEY_ID: ${{ secrets.AWS_PUBLISH_ACCESS_KEY_ID }}
  118. AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_PUBLISH_SECRET_ACCESS_KEY }}
  119. AWS_ASSUME_ROLE_ARN: ${{ secrets.AWS_PUBLISH_ROLE_ARN }}
  120. AWS_MAX_ATTEMPTS: 3
  121. AWS_DEFAULT_REGION: us-east-1
  122. run: |
  123. # Configure AWS account
  124. export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s" $(aws sts assume-role --role-arn ${AWS_ASSUME_ROLE_ARN} --role-session-name GitHubCharts --query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" --output text))
  125. # Extract chart release metadata from the publish report file
  126. vib_publish_report_file=$(find ~/artifacts -name "report.json" -print -quit)
  127. chart_name=$(jq -re '.actions|map(select(.action_id == "helm-publish"))[0] | .application.name' $vib_publish_report_file)
  128. chart_version=$(jq -re '.actions|map(select(.action_id == "helm-publish"))[0] | .application.version' $vib_publish_report_file)
  129. # Download published asset
  130. mkdir download
  131. aws s3 cp s3://${{ secrets.AWS_S3_BUCKET }}/bitnami/${chart_name}-${chart_version}.tgz download/
  132. cd deprecated-index
  133. git config user.name "Bitnami Bot"
  134. git config user.email "bitnami.bot@broadcom.com"
  135. attempts=0
  136. max_attempts=5
  137. is_index_updated=0
  138. while [[ $attempts -lt $max_attempts && $is_index_updated -eq 0 ]]; do
  139. attempts=$((attempts + 1))
  140. # Pull changes from remote
  141. git fetch origin deprecated-index
  142. current_commit_id=$(git rev-parse origin/deprecated-index)
  143. git reset --hard $(git commit-tree origin/deprecated-index^{tree} -m "Update index.yaml")
  144. # Rebuild index
  145. helm repo index --url https://charts.bitnami.com/bitnami --merge bitnami/index.yaml ../download
  146. # Compare size of files
  147. if [[ $(stat -c%s bitnami/index.yaml) -gt $(stat -c%s ../download/index.yaml) ]]; then
  148. echo "New index.yaml file is shorter than the current one"
  149. exit 1
  150. fi
  151. # Adding tmp file as a helm repo
  152. if ! helm repo add cache file://../download/ ; then
  153. echo "New index.yaml file can't be indexed"
  154. exit 1
  155. fi
  156. cp ../download/index.yaml bitnami/index.yaml
  157. # Push changes
  158. git add bitnami/index.yaml && git commit --signoff --amend --no-edit
  159. git push origin deprecated-index --force-with-lease=deprecated-index:${current_commit_id} && is_index_updated=1 || echo "Failed to push during attempt $attempts"
  160. done
  161. if [[ $is_index_updated -ne 1 ]]; then
  162. echo "Could not update the index after $max_attempts attempts"
  163. exit 1
  164. fi
  165. notify:
  166. name: Send notification
  167. needs:
  168. - vib-publish
  169. - update-deprecated-index
  170. if: ${{ always() && (needs.vib-publish.result == 'failure' || needs.update-deprecated-index.result == 'failure') }}
  171. uses: bitnami/support/.github/workflows/gchat-notification.yml@main
  172. with:
  173. workflow: ${{ github.workflow }}
  174. job-url: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
  175. secrets:
  176. webhook-url: ${{ secrets.GCHAT_CONTENT_ALERTS_WEBHOOK_URL }}