---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: operator-release
spec:
  params:
  - name: package
    description: package to release
    default: github.com/tektoncd/operator
  - name: gitRevision
    description: the git revision to release
  - name: imageRegistry
    description: The target image registry
    default: gcr.io
  - name: imageRegistryPath
    description: The path (project) in the image registry
    default: tekton-releases
  - name: versionTag
    description: The X.Y.Z version that the artifacts should be tagged with
  - name: releaseBucket
    description: bucket where the release is stored. The bucket must be project specific.
    default: gs://tekton-releases-nightly/operator
  - name: releaseAsLatest
    description: Whether to tag and publish this release as Operator's latest
    default: "true"
  - name: platforms
    description: Platforms to publish for the images (e.g. linux/amd64,linux/arm64)
    default: linux/amd64,linux/arm64,linux/s390x,linux/ppc64le
  - name: serviceAccountPath
    description: The path to the service account file within the release-secret workspace
  - name: kubeDistro
    description: The kubernetes platform (e.g. kubernetes or openshift ... ) targeted by a pipeline run
    default: kubernetes
  - name: TektonCDPipelinesVersion
    description: Version of TektonCD Pipelines Release to be packaged with this operator build
    default: "nightly"
  - name: TektonCDTriggersVersion
    description: Version of TektonCD Triggers Release to be packaged with this operator build
    default: "nightly"
  - name: TektonCDDashboardVersion
    description: Version of TektonCD Dashboard Release to be packaged with this operator build
    default: "nightly"
  - name: TektonCDResultsVersion
    description: Version of TektonCD Results Release to be packaged with this operator build
    # TODO: Set default to nightly. Aug 10, 2021 - no nightly releases available for Results
    default: "latest"
  workspaces:
    - name: workarea
      description: The workspace where the repo will be cloned.
    - name: release-secret
      description: The secret that contains a service account authorized to push to the imageRegistry and to the output bucket
  results:
    - name: commit-sha
      description: the sha of the commit that was released
      value: $(tasks.git-clone.results.commit)
    - name: release-file
      description: the URL of the release file
      value: $(tasks.report-bucket.results.release)
    - name: release-file-no-tag
      description: the URL of the release file
      value: $(tasks.report-bucket.results.release-no-tag)
  tasks:
    - name: git-clone
      taskRef:
        name: git-clone
      workspaces:
        - name: output
          workspace: workarea
          subpath: git
      params:
        - name: url
          value: https://$(params.package)
        - name: revision
          value: $(params.gitRevision)
    - name: precheck
      runAfter: [git-clone]
      taskRef:
        name: prerelease-checks
      params:
      - name: package
        value: $(params.package)
      - name: versionTag
        value: $(params.versionTag)
      - name: releaseBucket
        value: $(params.releaseBucket)
      workspaces:
      - name: source-to-release
        workspace: workarea
        subpath: git
    - name: fetch-component-releases
      taskRef:
        name: operator-fetch-component-releases
      runAfter: [precheck]
      workspaces:
      - name: source
        workspace: workarea
        subpath: git
      params:
      - name: PIPELINES_VERSION
        value: $(params.TektonCDPipelinesVersion)
      - name: TRIGGERS_VERSION
        value: $(params.TektonCDTriggersVersion)
      - name: DASHBOARD_VERSION
        value: $(params.TektonCDDashboardVersion)
      - name: RESULTS_VERSION
        value: $(params.TektonCDResultsVersion)
      - name: TARGET_PLATFORM
        value: $(params.kubeDistro)
    - name: unit-tests
      runAfter: [precheck]
      taskRef:
        name: golang-test
      params:
        - name: package
          value: $(params.package)
        - name: flags
          value: -v -mod=vendor
      workspaces:
        - name: source
          workspace: workarea
          subpath: git
    - name: build
      runAfter: [precheck, fetch-component-releases]
      taskRef:
        name: golang-build
      params:
        - name: package
          value: $(params.package)
        - name: packages
          value: ./cmd/$(params.kubeDistro)/...
      workspaces:
        - name: source
          workspace: workarea
          subpath: git
    - name: publish-images
      runAfter: [build, unit-tests]
      taskRef:
        name: publish-operator-release
      params:
        - name: package
          value: $(params.package)
        - name: versionTag
          value: $(params.versionTag)
        - name: imageRegistry
          value: $(params.imageRegistry)
        - name: imageRegistryPath
          value: $(params.imageRegistryPath)
        - name: releaseAsLatest
          value: $(params.releaseAsLatest)
        - name: platforms
          value: $(params.platforms)
        - name: serviceAccountPath
          value: $(params.serviceAccountPath)
        - name: kubeDistro
          value: $(params.kubeDistro)
      workspaces:
        - name: source
          workspace: workarea
          subpath: git
        - name: output
          workspace: workarea
          subpath: bucket
        - name: release-secret
          workspace: release-secret
    - name: publish-to-bucket
      runAfter: [publish-images]
      taskRef:
        name: gcs-upload
      workspaces:
        - name: credentials
          workspace: release-secret
        - name: source
          workspace: workarea
          subpath: bucket
      params:
        - name: location
          value: $(params.releaseBucket)/previous/$(params.versionTag)
        - name: path
          value: $(params.versionTag)
        - name: serviceAccountPath
          value: $(params.serviceAccountPath)
    - name: publish-to-bucket-latest
      runAfter: [publish-images]
      when:
        - input: "$(params.releaseAsLatest)"
          operator: in
          values: ["true"]
      taskRef:
        name: gcs-upload
      workspaces:
        - name: credentials
          workspace: release-secret
        - name: source
          workspace: workarea
          subpath: bucket
      params:
        - name: location
          value: $(params.releaseBucket)/latest
        - name: path
          value: $(params.versionTag)
        - name: serviceAccountPath
          value: $(params.serviceAccountPath)
    - name: report-bucket
      runAfter: [publish-to-bucket]
      params:
        - name: releaseBucket
          value: $(params.releaseBucket)
        - name: versionTag
          value: $(params.versionTag)
      taskSpec:
        params:
          - name: releaseBucket
          - name: versionTag
        results:
          - name: release
            description: The full URL of the release file in the bucket
          - name: release-no-tag
            description: The full URL of the release file (no tag) in the bucket
        steps:
          - name: create-results
            image: alpine
            script: |
              BASE_URL=$(echo "$(params.releaseBucket)/previous/$(params.versionTag)")
              # If the bucket is in the gs:// return the corresponding public https URL
              BASE_URL=$(echo ${BASE_URL} | sed 's,gs://,https://storage.googleapis.com/,g')
              echo "${BASE_URL}/release.yaml" > $(results.release.path)
              echo "${BASE_URL}/release.notag.yaml" > $(results.release-no-tag.path)
