Commit 7478d355 authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

feat: migrate to CI/CD component

⚠️ requires GitLab 16.6 or later
parent b111980a
Loading
Loading
Loading
Loading
+43 −20
Original line number Diff line number Diff line
@@ -5,13 +5,36 @@ images that can run on any cloud with [Cloud Native Buildpacks](https://buildpac

## Usage

In order to include this template in your project, add the following to your `gitlab-ci.yml`:
This template can be used both as a [CI/CD component](https://docs.gitlab.com/ee/ci/components/#use-a-component-in-a-cicd-configuration) 
or using the legacy [`include:project`](https://docs.gitlab.com/ee/ci/yaml/index.html#includeproject) syntax.

### Use as a CI/CD component

Add the following to your `gitlab-ci.yml`:

```yaml
include:
  # 1: include the component
  - component: gitlab.com/to-be-continuous/cnb/gitlab-ci-cnb.yml@2.2.1
    # 2: set/override component inputs
    inputs:
      builder-image: "registry.hub.docker.com/heroku/buildpacks:20" # ⚠ this is only an example
```

### Use as a CI/CD template (legacy)

Add the following to your `gitlab-ci.yml`:

```yaml
include:
  # 1: include the template
  - project: 'to-be-continuous/cnb'
    ref: '2.2.1'
    file: '/templates/gitlab-ci-cnb.yml'
    file: 'templates/gitlab-ci-cnb.yml'

variables:
  # 2: set/override template variables
  CNB_BUILDER_IMAGE: "registry.hub.docker.com/heroku/buildpacks:20" # ⚠ this is only an example
```

## Understanding the CNB template
@@ -20,16 +43,16 @@ include:

The CNB template uses some global configuration used throughout all jobs.

| Name                  | Description                            | Default value     |
| Input / Variable | Description                            | Default value     |
| --------------------- | -------------------------------------- | ----------------- |
| `CNB_BUILDER_IMAGE`   | The CNB builder image used to build your application image<br/>_depending on your needs, [choose the most appropriate one](https://paketo.io/docs/concepts/builders/#what-paketo-builders-are-available)_ | `registry.hub.docker.com/paketobuildpacks/builder:base` |
| `CNB_PLATFORM_API`    | The CNB [platform API version](https://github.com/buildpacks/spec/blob/main/platform.md#platform-api-version) | `0.9` |
| `builder-image` / `CNB_BUILDER_IMAGE` | The CNB builder image used to build your application image<br/>_depending on your needs, [choose the most appropriate one](https://paketo.io/docs/concepts/builders/#what-paketo-builders-are-available)_ | `registry.hub.docker.com/paketobuildpacks/builder:base` |
| `platform-api` / `CNB_PLATFORM_API` | The CNB [platform API version](https://github.com/buildpacks/spec/blob/main/platform.md#platform-api-version) | `0.9` |

### Available Builders

Depending on your needs and preferences, you are free to choose whichever CNB compliant builder (simply override the `CNB_BUILDER_IMAGE` variable):

| Name                  | Builder Image                    | Description     |
| Input / Variable | Builder Image                    | Description     |
| --------------------- | -------------------------------- | ----------------- |
|	[Google](https://github.com/GoogleCloudPlatform/buildpacks) | `gcr.io/buildpacks/builder:v1`   | Ubuntu 18 base image with buildpacks for .NET, Go, Java, Node.js, and Python |
|	[Heroku](https://github.com/heroku/builder) | `registry.hub.docker.com/heroku/buildpacks:20`           | Base builder for Heroku-20 stack, based on ubuntu:20.04 base image |
@@ -62,10 +85,10 @@ In practice:

The **snapshot** and **release** images are defined by the following variables:

| Name                   | Description        | Default value                                     |
| Input / Variable | Description        | Default value                                     |
| ---------------------- | ------------------ | ------------------------------------------------- |
| `CNB_SNAPSHOT_IMAGE`   | CNB snapshot image | `$CI_REGISTRY_IMAGE/snapshot:$CI_COMMIT_REF_SLUG` |
| `CNB_RELEASE_IMAGE`    | CNB release image  | `$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME`          |
| `snapshot-image` / `CNB_SNAPSHOT_IMAGE` | CNB snapshot image | `$CI_REGISTRY_IMAGE/snapshot:$CI_COMMIT_REF_SLUG` |
| `release-image` / `CNB_RELEASE_IMAGE` | CNB release image  | `$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME`          |

As you can see, the CNB template is configured by default to use the GitLab container registry.
You may perfectly override this and use another container registry, but be aware of a few things:
@@ -87,7 +110,7 @@ But when using other registry(ies), you'll have also to **configure appropriate
If you use the **same registry** for both snapshot and release images, you shall use the following configuration
variables:

| Name                             | Description                            |
| Input / Variable | Description                            |
| -------------------------------- | -------------------------------------- |
| :lock: `CNB_REGISTRY_USER`    | container registry username for image registry |
| :lock: `CNB_REGISTRY_PASSWORD`| container registry password for image registry  |
@@ -96,7 +119,7 @@ variables:

If you use **different registries** for snapshot and release images, you shall use separate configuration variables:

| Name                                     | Description                            |
| Input / Variable | Description                            |
| ---------------------------------------- | -------------------------------------- |
| :lock: `CNB_REGISTRY_SNAPSHOT_USER`   | container registry username for snapshot image registry |
| :lock: `CNB_REGISTRY_SNAPSHOT_PASSWORD`| container registry password for snapshot image registry |
@@ -127,10 +150,10 @@ It is bound to the `package-build` stage, and uses the following variables:

It uses the following variable:

| Name            | Description                                                                        | Default value |
| Input / Variable | Description                                                                        | Default value |
|-----------------|------------------------------------------------------------------------------------|---------------|
| `CNB_APP_DIR`   | Absolute root directory in final image     | `/workspace`  |
| `CNB_SRC_APP_DIR` | Relative path to the application source code base directory in your repository     | `.`           |
| `app-dir` / `CNB_APP_DIR` | Absolute root directory in final image     | `/workspace`  |
| `src-app-dir` / `CNB_SRC_APP_DIR` | Relative path to the application source code base directory in your repository     | `.`           |


<!--
@@ -151,7 +174,7 @@ If you selected an alternate Builder and Buildpacks (by overriding the `CNB_BUIL

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

| Name               | Description                                            | Example                                 |
| Input / Variable | Description                                            | Example                                 |
| ------------------ | ------------------------------------------------------ | --------------------------------------- |
| `cnb_image`        | snapshot image name **with tag**                       | `registry.gitlab.com/acme/website/snapshot:main` |
| `cnb_image_digest` | snapshot image name **with digest** (no tag)           | `registry.gitlab.com/acme/website/snapshot@sha256:b7914a91...` |
@@ -178,15 +201,15 @@ Examples or User-Provided variables:

This job pushes (_promotes_) the built image as the _release_ image using [skopeo](https://github.com/containers/skopeo).

| Name                  | Description                                                                 | Default value     |
| Input / Variable | Description                                                                 | Default value     |
| --------------------- | --------------------------------------------------------------------------- | ----------------- |
| `CNB_SKOPEO_IMAGE`    | The Docker image used to run [skopeo](https://github.com/containers/skopeo) | `quay.io/skopeo/stable:latest` |
| `CNB_PUBLISH_ARGS`    | Additional [`skopeo copy` arguments](https://github.com/containers/skopeo/blob/master/docs/skopeo-copy.1.md#options) | _(none)_          |
| `CNB_PROD_PUBLISH_STRATEGY`| Defines the publish to production strategy. One of `manual` (i.e. _one-click_), `auto` or `none` (disabled). | `manual` |
| `skopeo-image` / `CNB_SKOPEO_IMAGE` | The Docker image used to run [skopeo](https://github.com/containers/skopeo) | `quay.io/skopeo/stable:latest` |
| `publish-args` / `CNB_PUBLISH_ARGS` | Additional [`skopeo copy` arguments](https://github.com/containers/skopeo/blob/master/docs/skopeo-copy.1.md#options) | _(none)_          |
| `prod-publish-strategy` / `CNB_PROD_PUBLISH_STRATEGY` | Defines the publish to production strategy. One of `manual` (i.e. _one-click_), `auto` or `none` (disabled). | `manual` |

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

| Name               | Description                                           | Example                                 |
| Input / Variable | Description                                           | Example                                 |
| ------------------ | ----------------------------------------------------- | --------------------------------------- |
| `cnb_image`        | release image name **with tag**                       | `registry.gitlab.com/acme/website:main` |
| `cnb_image_digest` | release image name **with digest** (no tag)           | `registry.gitlab.com/acme/website@sha256:b7914a91...` |
+2 −2
Original line number Diff line number Diff line
@@ -27,13 +27,13 @@ if [[ "$curVer" ]]; then
  log_info "Bump version from \\e[33;1m${curVer}\\e[0m to \\e[33;1m${nextVer}\\e[0m (release type: $relType)..."

  # replace in README
  sed -e "s/ref: '$curVer'/ref: '$nextVer'/" README.md > README.md.next
  sed -e "s/ref: *'$curVer'/ref: '$nextVer'/" -e "s/ref: *\"$curVer\”/ref: \”$nextVer\”/" -e "s/component: *\(.*\)@$curVer/component: \1@$nextVer/" README.md > README.md.next
  mv -f README.md.next README.md

  # replace in template and variants
  for tmpl in templates/*.yml
  do
    sed -e "s/\"$curVer\"/\"$nextVer\"/" "$tmpl" > "$tmpl.next"
    sed -e "s/command: *\[\"--service\", \"\(.*\)\", \"$curVer\"\]/command: [\"--service\", \"\1\", \"$nextVer\"]/" "$tmpl" > "$tmpl.next"
    mv -f "$tmpl.next" "$tmpl"
  done
else
+50 −12
Original line number Diff line number Diff line
@@ -14,6 +14,43 @@
# Floor, Boston, MA  02110-1301, USA.
# =========================================================================================
# default workflow rules: Merge Request pipelines
spec:
  inputs:
    builder-image:
      description: |-
        The CNB builder image used to build your application image

        _depending on your needs, [choose the most appropriate one](https://paketo.io/docs/concepts/builders/#what-paketo-builders-are-available)_
      default: registry.hub.docker.com/paketobuildpacks/builder:base
    app-dir:
      description: Absolute root directory in final image
      default: /workspace
    src-app-dir:
      description: Relative path to the application source code base directory in your repository
      default: .
    platform-api:
      description: The CNB [platform API version](https://github.com/buildpacks/spec/blob/main/platform.md#platform-api-version)
      default: '0.9'
    snapshot-image:
      description: CNB snapshot image
      default: $CI_REGISTRY_IMAGE/snapshot:$CI_COMMIT_REF_SLUG
    release-image:
      description: CNB release image
      default: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
    skopeo-image:
      description: The docker image used to publish docker image with Skopeo
      default: quay.io/skopeo/stable:latest
    publish-args:
      description: Additional [`skopeo copy` arguments](https://github.com/containers/skopeo/blob/master/docs/skopeo-copy.1.md#options)
      default: ''
    prod-publish-strategy:
      description: Defines the publish to production strategy.
      options:
      - none
      - manual
      - auto
      default: manual
---
workflow:
  rules:
    # prevent branch pipeline when an MR is open (prefer MR pipeline)
@@ -56,30 +93,31 @@ workflow:

variables:
  # variabilized tracking image
  TBC_TRACKING_IMAGE: "registry.gitlab.com/to-be-continuous/tools/tracking:master"
  TBC_TRACKING_IMAGE: registry.gitlab.com/to-be-continuous/tools/tracking:master

  CNB_BUILDER_IMAGE: "registry.hub.docker.com/paketobuildpacks/builder:base"
  CNB_SKOPEO_IMAGE: "quay.io/skopeo/stable:latest"
  CNB_BUILDER_IMAGE: $[[ inputs.builder-image ]]
  CNB_SKOPEO_IMAGE: $[[ inputs.skopeo-image ]]

  # Default config uses the internal GitLab registry
  CNB_SNAPSHOT_IMAGE: "$CI_REGISTRY_IMAGE/snapshot:$CI_COMMIT_REF_SLUG"
  CNB_RELEASE_IMAGE: "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME"
  CNB_SNAPSHOT_IMAGE: $[[ inputs.snapshot-image ]]
  CNB_RELEASE_IMAGE: $[[ inputs.release-image ]]

  # default CNB platform API version
  CNB_PLATFORM_API: "0.9"
  CNB_PLATFORM_API: $[[ inputs.platform-api ]]

  # absolute root directory in final image
  CNB_SRC_APP_DIR: "."
  CNB_SRC_APP_DIR: $[[ inputs.src-app-dir ]]
  # default final image hierarchy root dir
  CNB_APP_DIR: "/workspace"
  CNB_APP_DIR: $[[ inputs.app-dir ]]

  # default: one-click publish
  CNB_PROD_PUBLISH_STRATEGY: manual
  CNB_PROD_PUBLISH_STRATEGY: $[[ inputs.prod-publish-strategy ]]
  CNB_PUBLISH_ARGS: $[[ inputs.publish-args ]]
  
  # default production ref name (pattern)
  PROD_REF: '/^(master|main)$/'
  PROD_REF: /^(master|main)$/
  # default integration ref name (pattern)
  INTEG_REF: '/^develop$/'
  INTEG_REF: /^develop$/

stages:
  - build