|
| 1 | +--- |
| 2 | +title:"Creating and uploading Helm packages" |
| 3 | +description:"Manually create and upload Helm packages" |
| 4 | +group:deployments |
| 5 | +sub_group:helm |
| 6 | +redirect_from: |
| 7 | + -/docs/create-helm-artifacts-using-codefresh-pipeline/ |
| 8 | +toc:true |
| 9 | +--- |
| 10 | + |
| 11 | +Helm packages are just TAR files. Helm repositories are simple file hierarchies with an extra[index.yaml](https://helm.sh/docs/developing_charts/#the-chart-repository-structure){:target="\_blank"}. |
| 12 | +You can run custom commands and manually upload indexes and packages to a Helm repo. |
| 13 | + |
| 14 | +>This articles shows some non-standard Helm examples. |
| 15 | + For the basic use cases, or if you are just getting started with Helm, see our[Helm quick start guide]({{site.baseurl}}/docs/getting-started/helm-quick-start-guide/) and[Using Helm in CI pipelines]({{site.baseurl}}/docs/deployments/helm/using-helm-in-codefresh-pipeline/). |
| 16 | + |
| 17 | +##Package a Helm chart |
| 18 | +Below is an example of a freestyle step in a Codefresh pipeline that packages the Helm chart and then extracts the chart name from the command output. It also saves that package name in an environment variable for later use. |
| 19 | + |
| 20 | +`YAML` |
| 21 | +{% highlight yaml %} |
| 22 | +{% raw %} |
| 23 | +helm_package: |
| 24 | + image: devth/helm |
| 25 | + commands: |
| 26 | + - cf_export PACKAGE=$(helm package <mychart> | cut -d " " -f 8) |
| 27 | +{% endraw %} |
| 28 | +{% endhighlight %} |
| 29 | + |
| 30 | +The`helm package` command expects a path to an unpacked chart. Replace`<mychart>` in the example with the directory that holds your chart files. Note that this directory must have the same name as the chart name, as per Helm requirements.<br> |
| 31 | +See[Helm package docs](https://github.com/kubernetes/helm/blob/master/docs/helm/helm_package.md){:target="_blank"} and[Helm charts overview](https://github.com/kubernetes/helm/blob/master/docs/charts.md){:target="_blank"} for more information. |
| 32 | + |
| 33 | +{{site.data.callout.callout_info}} |
| 34 | +To use`cf_export`and make the variable available to other steps in the pipeline, see [Variables in pipelines]({{site.baseurl}}/docs/pipelines/variables). |
| 35 | +{{site.data.callout.end}} |
| 36 | + |
| 37 | +##Example 1: Push the chart to GCS based Helm Repository |
| 38 | +The first example pushes the packaged chart into a public cloud storage service, like AWS S3, Azure Storage, or Google Cloud Storage. We chose Google Cloud Storage (GCS) for this example. |
| 39 | +Our pipeline has three steps: |
| 40 | + |
| 41 | +{:start="1"} |
| 42 | +1. download_index: download the Helm`index.yaml` file from GCS, or create one of it's not there. |
| 43 | + |
| 44 | +{:start="2"} |
| 45 | +2. helm_package_merge: package the chart as described earlier, and also merge the new package into the downloaded`index.yaml` file, using the `helm repo index --merge` command. |
| 46 | + |
| 47 | +{:start="3"} |
| 48 | +3. push_gcs: upload the updated`index.yaml` file and the newly created package to GCS. |
| 49 | + |
| 50 | +`YAML` |
| 51 | +{% highlight yaml %} |
| 52 | +{% raw %} |
| 53 | +steps: |
| 54 | + download_index: |
| 55 | + image: appropriate/curl:latest |
| 56 | + commands: |
| 57 | + - 'curlhttps://storage.googleapis.com/$GOOGLE_BUCKET_NAME/index.yaml --output ./index.yaml --fail || :' |
| 58 | + - '[ ! -f ./index.yaml] && echo "apiVersion: v1">./index.yaml' |
| 59 | + helm_package_merge: |
| 60 | + image: devth/helm |
| 61 | + commands: |
| 62 | + - cf_export PACKAGE=$(helm package <mychart> | cut -d " " -f 8) |
| 63 | + - helm repo index --merge ./index.yaml . |
| 64 | + push_gcs: |
| 65 | + image: camil/gsutil |
| 66 | + commands: |
| 67 | + - echo -E $GOOGLE_CREDENTIALS > /gcs-creds.json |
| 68 | + - echo -e "[Credentials]\ngs_service_key_file = /gcs-creds.json\n[GSUtil]\ndefault_project_id = $GOOGLE_PROJECT_ID" > /root/.boto |
| 69 | + - gsutil cp ./index.yaml gs://$GOOGLE_BUCKET_NAME |
| 70 | + - gsutil cp $PACKAGE gs://$GOOGLE_BUCKET_NAME |
| 71 | +{% endraw %} |
| 72 | +{% endhighlight %} |
| 73 | + |
| 74 | + |
| 75 | +###Environment setup |
| 76 | + |
| 77 | +This pipeline references some predefined environment variables such as`GOOGLE_BUCKET_NAME`,`GOOGLE_PROJECT_ID` and`GOOGLE_CREDENTIALS`. |
| 78 | +For this example, we created a service account with appropriate permissions in Google Cloud, and saved the credentials into`GOOGLE_CREDENTIALS` as a Codefresh Secret. <br> |
| 79 | +For more information, see: |
| 80 | +[Authenticating with Google services](https://cloud.google.com/storage/docs/authentication#service_accounts){:target="_blank"}. <br> |
| 81 | +[Codefresh pipeline configuration and secrets](https://codefresh.io/docs/docs/codefresh-yaml/variables/#user-provided-variables){:target="_blank"}. |
| 82 | + |
| 83 | +##Example 2: Push the chart to Chart Museum |
| 84 | +Chart Museum is a Helm repository*server* that has an HTTP API, pluggable backends, authentication, and more. |
| 85 | +Read more about[Chart Museum](https://github.com/kubernetes-helm/chartmuseum){:target="_blank"}. |
| 86 | + |
| 87 | +In this example, we already have a Chart Museum server running, so we'll push the packaged chart to it. |
| 88 | + |
| 89 | +The steps will be: |
| 90 | + |
| 91 | +{:start="1"} |
| 92 | +1. helm_package: package the chart as described earlier. |
| 93 | + |
| 94 | +{:start="2"} |
| 95 | +2. get_repo_url: In order to avoid hard-coding the repository URL into the pipeline, we will retrieve it from the Codefresh Helm integration. |
| 96 | +In this case, we have added our repository with Codefresh as described in[Using external Helml repos in Codefresh pipelines]({{site.baseurl}}/docs/deployments/helm/helm-charts-and-repositories). |
| 97 | +Replace`<reponame>` in the example with the name you gave to your repository when you added it to Codefresh. |
| 98 | + |
| 99 | +{:start="3"} |
| 100 | +3. helm_push: call the Chart Museum HTTP api to just upload the package. Chart Museum will take care of the rest. |
| 101 | + |
| 102 | +`YAML` |
| 103 | +{% highlight yaml %} |
| 104 | +{% raw %} |
| 105 | +steps: |
| 106 | + helm_package: |
| 107 | + image: devth/helm |
| 108 | + commands: |
| 109 | + - cf_export PACKAGE=$(helm package <mychart> | cut -d " " -f 8) |
| 110 | + get_repo_url: |
| 111 | + image: codefresh/cli:latest |
| 112 | + commands: |
| 113 | + - cf_export HELM_URL=$(codefresh get ctx <reponame> -o=yaml | grep repositoryUrl | cut -d "'" -f 2) |
| 114 | + helm_push: |
| 115 | + image: appropriate/curl |
| 116 | + commands: |
| 117 | + - curl --data-binary "@$PACKAGE" $HELM_URL/api/charts |
| 118 | +{% endraw %} |
| 119 | +{% endhighlight %} |
| 120 | + |
| 121 | + |
| 122 | +##Related articles |
| 123 | +[Using Helm in a Codefresh pipeline]({{site.baseurl}}/docs/deployments/helm/using-helm-in-codefresh-pipeline/) |
| 124 | +[Using a managed Helm repository]({{site.baseurl}}/docs/deployments/helm/managed-helm-repository/) |
| 125 | +[Helm environment promotion]({{site.baseurl}}/docs/deployments/helm/helm-environment-promotion) |