Commit 09af4af5 authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

feat(package): propagate output variables (dotenv)

parent cfa6ad7e
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -356,6 +356,22 @@ The general version format will be `<x.y.z>-<label>`:
    * :warning: when `HELM_PUBLISH_SNAPSHOT_ENABLED` is enabled, the chart is additionally packaged (and published) with a label suffixed with `snapshot`<br/>
    _(ex: `snapshot` on production branch and `review-feature-12-snapshot` on branch `review/feature-12`)_

#### Package output variables

The `helm-package` job produces _output variables_ that are propagated to downstream jobs (using [dotenv artifacts](https://docs.gitlab.com/ee/ci/pipelines/job_artifacts.html#artifactsreportsdotenv)):

* `$helm_package_file`: the packaged chart (tgz file),
* `$helm_package_name`: the chart/package name,
* `$helm_package_version`: the package [version](#chart-version-management).

If `HELM_PUBLISH_SNAPSHOT_ENABLED` is set to `true`, extra variables are produced:

* `$helm_snapshot_package_name`: the snapshot package name,
* `$helm_snapshot_package_version`: the snapshot package [version](#chart-version-management),
* `$helm_snapshot_package_remote_url`: the remote url where the snapshot package was published.

Those variables may be freely used in downstream jobs.

### `helm-publish` job

This job publishes the packaged chart to a [chart repository](https://helm.sh/docs/topics/chart_repository/) or [OCI-based registry](https://helm.sh/docs/topics/registries/). It uses the following variables:
+21 −13
Original line number Diff line number Diff line
@@ -455,17 +455,16 @@ stages:
      helm_opts="$helm_opts --namespace $namespace"
    fi

    package=$(ls -1 ./helm_packages/*.tgz 2>/dev/null || echo "")
    package=${package:-$HELM_DEPLOY_CHART}
    if [ -z "${package}" ]; then
    _pkg=${helm_package_file:-$HELM_DEPLOY_CHART}
    if [ -z "${_pkg}" ]; then
      log_error "No Chart to deploy! Please use \\e[32m\$HELM_DEPLOY_CHART\\e[0m to deploy a chart from a repository"
      log_error "Or check the provided variables to package your own chart!"
      exit 1
    fi
    log_info "--- using \\e[32mpackage\\e[0m: \\e[33;1m${package}\\e[0m"
    log_info "--- using \\e[32mpackage\\e[0m: \\e[33;1m${_pkg}\\e[0m"

    # shellcheck disable=SC2086
    helm $helm_opts $HELM_DEPLOY_ARGS $environment_name $package
    helm $helm_opts $HELM_DEPLOY_ARGS $environment_name $_pkg

    # maybe execute post deploy script
    postscript="$HELM_SCRIPTS_DIR/helm-post-deploy.sh"
@@ -561,6 +560,7 @@ stages:
  }

  function helm_package() {
    rm -f helm-package.env
    # determine chart version to publish (semantic-release integration)
    if [[ "${SEMREL_INFO_ON}" && "${DOCKER_SEMREL_RELEASE_DISABLED}" != "true" ]]
    then
@@ -576,6 +576,8 @@ stages:
      base_version=$(awk -F':' '/^version:/ {print $2}' "$HELM_CHART_DIR/Chart.yaml")
      base_version=${base_version// /}
    fi
    chart_name=$(awk -F':' '/^name:/ {print $2}' "$HELM_CHART_DIR/Chart.yaml")
    chart_name=${chart_name// /}

    # also determine version label
    prod_ref_expr=${PROD_REF#/}
@@ -594,6 +596,9 @@ stages:
    # shellcheck disable=SC2086
    helm ${TRACE+--debug} $HELM_PACKAGE_ARGS --version ${base_version}${version_label} $HELM_CHART_DIR --destination helm_packages

    helm_package=$(ls -1 ./helm_packages/*.tgz 2>/dev/null || echo "")
    echo -e "helm_package_file=${helm_package}\\nhelm_package_version=${base_version}${version_label}\\nhelm_package_name=${chart_name}" > helm-package.env

    if [[ "$HELM_PUBLISH_SNAPSHOT_ENABLED" == "true" ]]
    then
      log_info "snapshot enabled: also package and publish chart with version: \\e[33;1m${base_version}${version_label}-snapshot\\e[0m"
@@ -602,17 +607,18 @@ stages:
      helm ${TRACE+--debug} $HELM_PACKAGE_ARGS --version ${base_version}${version_label}-snapshot $HELM_CHART_DIR --destination /tmp/helm_snapshot
      snapshot_package=$(ls -1 /tmp/helm_snapshot/*.tgz 2>/dev/null || echo "")
      helm_publish "$snapshot_package"
      echo -e "helm_snapshot_package_name=${chart_name}\\nhelm_snapshot_package_version=${base_version}${version_label}-snapshot\\nhelm_snapshot_package_remote_url=${HELM_PUBLISH_URL}" >> helm-package.env
    fi
  }

  function helm_publish() {
    helm_package=${1:-$(ls -1 ./helm_packages/*.tgz 2>/dev/null || echo "")}
    if [[ -z "$helm_package" ]]; then
    _pkg=${1:$helm_package_file}
    if [[ -z "$_pkg" ]]; then
      log_error "No package found to deploy"
      exit 1
    fi
    helm_package_name=$(basename "$helm_package")
    log_info "--- Publishing Helm package ${helm_package_name} to: ${HELM_PUBLISH_URL}..."
    _pkg_name=$(basename "$_pkg")
    log_info "--- Publishing Helm package ${_pkg_name} to: ${HELM_PUBLISH_URL}..."

    # method to lowercase
    HELM_PUBLISH_METHOD=$(echo "$HELM_PUBLISH_METHOD" | tr '[:upper:]' '[:lower:]')
@@ -649,13 +655,13 @@ stages:
        # enable OCI support prior to v3.8.0
        export HELM_EXPERIMENTAL_OCI=1
        # shellcheck disable=SC2086
        helm ${TRACE+--debug} push "$helm_package" "$HELM_PUBLISH_URL"
        helm ${TRACE+--debug} push "$_pkg" "$HELM_PUBLISH_URL"
      else
        log_info "Installing cm-push plugin (version ${HELM_CM_PUSH_PLUGIN_VERSION:-latest})..."
        # shellcheck disable=SC2086
        helm ${TRACE+--debug} plugin install ${HELM_CM_PUSH_PLUGIN_VERSION:+--version "$HELM_CM_PUSH_PLUGIN_VERSION"} https://github.com/chartmuseum/helm-push || true
        # shellcheck disable=SC2086
        helm ${TRACE+--debug} cm-push --username "$username" --password "$password" "$helm_package" "$HELM_PUBLISH_URL"
        helm ${TRACE+--debug} cm-push --username "$username" --password "$password" "$_pkg" "$HELM_PUBLISH_URL"
      fi
      ;;
    post)
@@ -664,10 +670,10 @@ stages:
        log_info "--- installing curl (required to publish Helm charts)..."
        apk add --no-cache curl
      fi
      curl --fail --request POST --form "chart=@$helm_package" --user "$username:$password" "$HELM_PUBLISH_URL"
      curl --fail --request POST --form "chart=@$_pkg" --user "$username:$password" "$HELM_PUBLISH_URL"
      ;;
    put)
      wget -v --method=PUT --user="$username" --password="$password" --body-file="$helm_package" "$HELM_PUBLISH_URL/$helm_package_name" -O -
      wget -v --method=PUT --user="$username" --password="$password" --body-file="$_pkg" "$HELM_PUBLISH_URL/$_pkg_name" -O -
      ;;
    custom)
      pubscript="$HELM_SCRIPTS_DIR/helm-publish.sh"
@@ -910,6 +916,8 @@ helm-package:
    expire_in: 1 week
    paths:
      - helm_packages/
    reports:
      dotenv: helm-package.env

# ==================================================
# Stage: publish