cd-pipeline.yaml 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. name: '[CI/CD] CD Pipeline'
  2. on: # rebuild any PRs and main branch changes
  3. push:
  4. branches:
  5. - master
  6. paths:
  7. - 'bitnami/airflow/**'
  8. - 'bitnami/apache/**'
  9. - 'bitnami/aspnet-core/**'
  10. - 'bitnami/cassandra/**'
  11. - 'bitnami/consul/**'
  12. - 'bitnami/discourse/**'
  13. - 'bitnami/dokuwiki/**'
  14. - 'bitnami/elasticsearch/**'
  15. - 'bitnami/external-dns/**'
  16. - 'bitnami/fluentd/**'
  17. - 'bitnami/ghost/**'
  18. - 'bitnami/grafana-loki/**'
  19. - 'bitnami/grafana/**'
  20. - 'bitnami/haproxy/**'
  21. - 'bitnami/haproxy-intel/**'
  22. - 'bitnami/harbor/**'
  23. - 'bitnami/influxdb/**'
  24. - 'bitnami/jenkins/**'
  25. - 'bitnami/joomla/**'
  26. - 'bitnami/kafka/**'
  27. - 'bitnami/keycloak/**'
  28. - 'bitnami/kibana/**'
  29. - 'bitnami/magento/**'
  30. - 'bitnami/mariadb/**'
  31. - 'bitnami/matomo/**'
  32. - 'bitnami/mediawiki/**'
  33. - 'bitnami/memcached/**'
  34. - 'bitnami/minio/**'
  35. - 'bitnami/mongodb/**'
  36. - 'bitnami/mysql/**'
  37. - 'bitnami/nginx/**'
  38. - 'bitnami/node/**'
  39. - 'bitnami/odoo/**'
  40. - 'bitnami/opencart/**'
  41. - 'bitnami/owncloud/**'
  42. - 'bitnami/phpbb/**'
  43. - 'bitnami/phpmyadmin/**'
  44. - 'bitnami/pinniped/**'
  45. - 'bitnami/postgresql/**'
  46. - 'bitnami/prestashop/**'
  47. - 'bitnami/rabbitmq/**'
  48. - 'bitnami/redis-cluster/**'
  49. - 'bitnami/redis/**'
  50. - 'bitnami/redmine/**'
  51. - 'bitnami/schema-registry/**'
  52. - 'bitnami/sealed-secrets/**'
  53. - 'bitnami/solr/**'
  54. - 'bitnami/spark/**'
  55. - 'bitnami/spring-cloud-dataflow/**'
  56. - 'bitnami/suitecrm/**'
  57. - 'bitnami/tomcat/**'
  58. - 'bitnami/wildfly/**'
  59. - 'bitnami/wordpress/**'
  60. - 'bitnami/wordpress-intel/**'
  61. - 'bitnami/zookeeper/**'
  62. - '!**.md'
  63. env:
  64. CSP_API_URL: https://console.cloud.vmware.com
  65. CSP_API_TOKEN: ${{ secrets.CSP_API_TOKEN }}
  66. VIB_PUBLIC_URL: https://cp.bromelia.vmware.com
  67. jobs:
  68. get-chart:
  69. runs-on: ubuntu-latest
  70. name: 'Get modified charts'
  71. outputs:
  72. chart: ${{ steps.get-chart.outputs.chart }}
  73. result: ${{ steps.get-chart.outputs.result }}
  74. steps:
  75. - uses: actions/checkout@v2
  76. with:
  77. path: charts
  78. fetch-depth: 2 # to be able to obtain files changed in the latest commit
  79. - id: get-chart
  80. name: 'Get modified charts'
  81. run: |
  82. cd charts
  83. files_changed="$(git show --pretty="" --name-only)"
  84. # Adding || true to avoid "Process exited with code 1" errors
  85. charts_dirs_changed="$(echo "$files_changed" | xargs dirname | grep -o "bitnami/[^/]*" | sort | uniq || true)"
  86. # Using grep -c as a better alternative to wc -l when dealing with empty strings."
  87. num_charts_changed="$(echo "$charts_dirs_changed" | grep -c "bitnami" || true)"
  88. num_version_bumps="$(echo "$files_changed" | grep Chart.yaml | xargs git show | grep -c "+version" || true)"
  89. if [[ "$num_charts_changed" -ne "$num_version_bumps" ]]; then
  90. # Changes done in charts but version not bumped -> ERROR
  91. echo "::set-output name=error::Detected changes in charts without version bump in Chart.yaml.\nCharts changed: ${num_charts_changed}\n${charts_dirs_changed}\nVersion bumps detected: ${num_version_bumps}"
  92. echo "::set-output name=result::fail"
  93. elif [[ "$num_charts_changed" -eq "1" ]]; then
  94. # Changes done in only one chart -> OK
  95. chart_name=$(echo "$charts_dirs_changed" | sed "s|bitnami/||g")
  96. echo "::set-output name=chart::${chart_name}"
  97. echo "::set-output name=result::ok"
  98. else
  99. # Changes done in more than chart -> FAIL
  100. echo -e "::set-output name=error::Changes detected in more than one chart directory:\n${charts_dirs_changed}\nThe publish process will be stopped. Please create different commits for each chart."
  101. echo "::set-output name=result::fail"
  102. fi
  103. - id: show-error
  104. name: 'Show error'
  105. if: ${{ steps.get-chart.outputs.result == 'fail' }}
  106. uses: actions/github-script@v3
  107. with:
  108. script: |
  109. core.setFailed('${{ steps.get-chart.outputs.error }}')
  110. vib-publish:
  111. runs-on: ubuntu-latest
  112. needs: get-chart
  113. if: ${{ needs.get-chart.outputs.result == 'ok' }}
  114. name: VIB Publish
  115. steps:
  116. - uses: actions/checkout@v2
  117. name: Checkout Repository
  118. with:
  119. path: charts
  120. - uses: vmware-labs/vmware-image-builder-action@main
  121. name: Verify and publish ${{ needs.get-chart.outputs.chart }}
  122. with:
  123. pipeline: ${{ needs.get-chart.outputs.chart }}/vib-publish.json
  124. config: charts/.vib/
  125. env:
  126. VIB_ENV_TARGET_PLATFORM: ${{ secrets.VIB_ENV_TARGET_PLATFORM }}
  127. VIB_ENV_ALTERNATIVE_TARGET_PLATFORM: ${{ secrets.VIB_ENV_ALTERNATIVE_TARGET_PLATFORM }}
  128. VIB_ENV_S3_URL: s3://${{ secrets.AWS_S3_BUCKET }}/bitnami
  129. VIB_ENV_S3_USERNAME: ${{ secrets.AWS_ACCESS_KEY_ID }}
  130. VIB_ENV_S3_PASSWORD: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  131. update-index:
  132. runs-on: ubuntu-latest
  133. needs:
  134. - vib-publish
  135. name: Update charts index
  136. steps:
  137. - uses: actions/download-artifact@v3
  138. with:
  139. path: ~/artifacts
  140. # If we perform a checkout of the master branch, we will find conflicts with the submodules
  141. - uses: actions/checkout@v2
  142. with:
  143. ref: 'index'
  144. path: index
  145. # The token is persisted in the local git config and enables scripts to run authenticated git commands.
  146. token: ${{ secrets.BITNAMI_BOT_TOKEN }}
  147. - name: Install and configure aws-cli and helm
  148. run: |
  149. # AWS CLI
  150. sudo DEBIAN_FRONTEND=noninteractive apt-get install -y awscli
  151. aws configure set region us-east-1
  152. aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }}
  153. aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  154. aws configure set source_profile default
  155. # helm
  156. HELM_TARBALL="helm-v3.8.1-linux-amd64.tar.gz"
  157. curl -SsLfO "https://get.helm.sh/${HELM_TARBALL}" && sudo tar xf "$HELM_TARBALL" --strip-components 1 -C /usr/local/bin
  158. - id: update-index
  159. name: Fetch chart and update index
  160. run: |
  161. # Extract chart release metadata from the publish result file
  162. vib_publish_result_file=$(find ~/artifacts -name "result.json" -print -quit)
  163. chart_name=$(jq -re '.actions|map(select(.action_id == "helm-publish"))[0] | .application.name' $vib_publish_result_file)
  164. chart_version=$(jq -re '.actions|map(select(.action_id == "helm-publish"))[0] | .application.version' $vib_publish_result_file)
  165. # Download published asset
  166. mkdir download
  167. aws s3 cp s3://${{ secrets.AWS_S3_BUCKET }}/bitnami/${chart_name}-${chart_version}.tgz download/
  168. cd index
  169. git config user.name "Bitnami Containers"
  170. git config user.email "bitnami-bot@vmware.com"
  171. attempts=0
  172. max_attempts=5
  173. is_index_updated=0
  174. while [[ $attempts -lt $max_attempts && $is_index_updated -eq 0 ]]; do
  175. attempts=$((attempts + 1))
  176. git fetch origin index
  177. git reset --hard origin/index
  178. # Rebuild index
  179. helm repo index --url https://charts.bitnami.com/bitnami --merge bitnami/index.yaml ../download
  180. cp ../download/index.yaml bitnami/index.yaml
  181. # Push changes
  182. git add bitnami/index.yaml && git commit -m "${chart_name}-${chart_version}: Update index.yaml" -s
  183. git push && is_index_updated=1 || echo "Failed to push during attempt $attempts"
  184. done
  185. if [[ $is_index_updated -ne 1 ]]; then
  186. echo "Could not update the index after $max_attempts attempts"
  187. exit 1
  188. fi
  189. # If the CD Pipeline does not succeed we should notify the interested agents
  190. slack-notif:
  191. runs-on: ubuntu-latest
  192. needs:
  193. - vib-publish
  194. - update-index
  195. if: always()
  196. name: Notify unsuccessful CD run
  197. steps:
  198. - name: Notify in Slack channel
  199. if: ${{ needs.vib-publish.result != 'success' || needs.update-index.result != 'success' }}
  200. uses: slackapi/slack-github-action@v1.19.0
  201. with:
  202. channel-id: ${{ secrets.CD_SLACK_CHANNEL_ID }}
  203. payload: |
  204. {
  205. "attachments": [
  206. {
  207. "color": "#CC0000",
  208. "fallback": "Unsuccessful bitnami/charts CD pipeline",
  209. "blocks": [
  210. {
  211. "type": "section",
  212. "text": {
  213. "type": "mrkdwn",
  214. "text": "*Unsuccessful `bitnami/charts` CD pipeline*"
  215. }
  216. },
  217. {
  218. "type": "section",
  219. "text": {
  220. "type": "mrkdwn",
  221. "text": "The CD pipeline for <${{ github.event.head_commit.url }}|bitnami/charts@${{ github.event.head_commit.id }}> did not succeed. Check the related <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|action run> for more information."
  222. }
  223. }
  224. ]
  225. }
  226. ]
  227. }
  228. env:
  229. SLACK_BOT_TOKEN: ${{ secrets.CD_SLACK_BOT_TOKEN }}