#@ load("@ytt:data", "data")

#@ def concourse_resource_image():
#@   return data.values.docker_registry + "/cepler-concourse-resource"
#@ end

#@ def pipeline_image():
#@   return data.values.docker_registry + "/cepler-pipeline"
#@ end

#@ def task_image_config():
type: registry-image
source:
  username: #@ data.values.docker_registry_user
  password: #@ data.values.docker_registry_password
  repository: #@ pipeline_image()
#@ end

groups:
- name: release
  jobs:
  - minor
  - major
  - test-unit
  - test-integration
  - build-concourse-resource-edge
  - rc
  - release
  - auto-bump-patch
  - publish
- name: image
  jobs:
    - build-pipeline-image

jobs:
- name: build-pipeline-image
  serial: true
  plan:
  - {get: pipeline-image-def, trigger: true}
  - task: build
    privileged: true
    config:
      platform: linux
      image_resource:
        type: registry-image
        source:
          repository: vito/oci-build-task
      inputs:
      - name: pipeline-image-def
      outputs:
      - name: image
      params:
        CONTEXT: pipeline-image-def/ci/image
      run:
        path: build
  - put: pipeline-image
    params:
      image: image/image.tar

- name: minor
  public: true
  plan:
  - in_parallel:
    - { get: repo }
    - { get: pipeline-tasks }
    - { get: version, params: { bump: minor, pre: rc } }
  - in_parallel:
    - { put: version, params: { file: version/number } }
    - do:
      - task: set-dev-version
        config:
          image_resource: #@ task_image_config()
          platform: linux
          inputs:
          - name: version
          - name: repo
          - name: pipeline-tasks
          outputs:
          - name: repo
          run:
            path: pipeline-tasks/ci/tasks/set-dev-version.sh
          params:
            BRANCH: #@ data.values.git_branch
      - put: repo
        params:
          repository: repo
          rebase: true

- name: major
  public: true
  plan:
  - in_parallel:
    - { get: repo }
    - { get: pipeline-tasks }
    - { get: version, params: { bump: minor, pre: rc } }
  - in_parallel:
    - { put: version, params: { file: version/number } }
    - do:
      - task: set-dev-version
        config:
          image_resource: #@ task_image_config()
          platform: linux
          inputs:
          - name: version
          - name: repo
          - name: pipeline-tasks
          outputs:
          - name: repo
          run:
            path: pipeline-tasks/ci/tasks/set-dev-version.sh
          params:
            BRANCH: #@ data.values.git_branch
      - put: repo
        params:
          repository: repo
          rebase: true

- name: test-unit
  plan:
  - in_parallel:
    - { get: repo, trigger: true }
    - { get: pipeline-tasks }
  - in_parallel:
    - task: test-unit
      config:
        platform: linux
        image_resource: #@ task_image_config()
        inputs:
        - name: pipeline-tasks
        - name: repo
        caches:
        - path: cargo-home
        - path: cargo-target-dir
        run:
          path: pipeline-tasks/ci/tasks/test-unit.sh

- name: build-concourse-resource-edge
  plan:
  - { get: repo, trigger: true }
  - task: build-concourse-resource-image
    privileged: true
    config:
      platform: linux
      image_resource:
        type: registry-image
        source:
          repository: vito/oci-build-task
      inputs:
      - name: repo
      outputs:
      - name: image
      params:
        CONTEXT: repo
        DOCKERFILE: repo/images/concourse/Dockerfile
      run:
        path: build
  - put: concourse-resource-image-edge
    params:
      image: image/image.tar

- name: test-integration
  plan:
  - in_parallel:
    - { get: repo, trigger: true }
    - { get: pipeline-tasks }
  - in_parallel:
    - task: test-integration
      config:
        platform: linux
        image_resource: #@ task_image_config()
        inputs:
        - name: pipeline-tasks
        - name: repo
        caches:
        - path: cargo-home
        - path: cargo-target-dir
        run:
          path: pipeline-tasks/ci/tasks/test-integration.sh

- name: rc
  serial: true
  plan:
  - in_parallel:
    - get: repo
      trigger: true
      passed:
      - test-integration
      - test-unit
    - { get: version, params: { pre: rc } }
  - put: version
    params: { file: version/number }

- name: release
  plan:
  - in_parallel:
    - { get: version, passed: [rc], params: {bump: final} }
    - { get: repo, passed: [rc] }
    - { get: pipeline-tasks }
  - task: prepare
    config:
      image_resource: #@ task_image_config()
      platform: linux
      inputs:
      - name: version
      - name: repo
      - name: pipeline-tasks
      outputs:
      - name: prepared-repo
      params:
        BRANCH: #@ data.values.git_branch
      run:
        path: pipeline-tasks/ci/tasks/prepare-repo.sh
  - in_parallel:
    - do:
      - put: repo
        params:
          repository: prepared-repo/git
          merge: true
      - put: version
        params: { bump: final }
    - task: build-osx-release
      privileged: true
      config:
        platform: linux
        image_resource:
          type: registry-image
          source:
            username: #@ data.values.osxcross_docker_username
            password: #@ data.values.osxcross_docker_password
            repository: #@ data.values.osxcross_repository
        inputs:
        - name: version
        - name: pipeline-tasks
        - name: prepared-repo
        outputs:
        - name: x86_64-apple-darwin
        caches:
        - path: cargo-home
        - path: cargo-target-dir
        params:
          TARGET: x86_64-apple-darwin
          REPO: prepared-repo/git
          OUT: x86_64-apple-darwin
        run:
          path: pipeline-tasks/ci/tasks/build-release.sh
    - task: build-static-release
      privileged: true
      config:
        platform: linux
        image_resource:
          type: registry-image
          source: { repository: clux/muslrust, tag: stable }
        inputs:
        - name: version
        - name: pipeline-tasks
        - name: prepared-repo
        outputs:
        - name: x86_64-unknown-linux-musl
        caches:
        - path: cargo-home
        - path: cargo-target-dir
        params:
          TARGET: x86_64-unknown-linux-musl
          REPO: prepared-repo/git
          OUT: x86_64-unknown-linux-musl
        run:
          path: pipeline-tasks/ci/tasks/build-release.sh
  - in_parallel:
    - do:
      - task: github-release
        config:
          image_resource: #@ task_image_config()
          platform: linux
          inputs:
          - name: x86_64-apple-darwin
          - name: x86_64-unknown-linux-musl
          - name: version
          - name: prepared-repo
          - name: pipeline-tasks
          outputs:
          - name: gh-release
          params:
            BRANCH: #@ data.values.git_branch
          run:
            path: pipeline-tasks/ci/tasks/github-release.sh
      - put: github-release
        params:
          name:   gh-release/name
          tag:    gh-release/tag
          body:   gh-release/notes.md
          globs: [gh-release/artifacts/*]
    - do:
      - task: build-concourse-resource-image
        privileged: true
        config:
          platform: linux
          image_resource:
            type: registry-image
            source:
              repository: vito/oci-build-task
          inputs:
          - name: prepared-repo
          outputs:
          - name: image
          params:
            CONTEXT: prepared-repo/git
            DOCKERFILE: prepared-repo/git/images/concourse/Dockerfile
          run:
            path: build
      - put: concourse-resource-image
        params:
          image: image/image.tar
          additional_tags: version/number

- name: auto-bump-patch
  plan:
  - in_parallel:
    - { get: repo }
    - { get: pipeline-tasks }
    - { get: version, trigger: true, params: { bump: patch, pre: rc }, passed: [release] }
  - in_parallel:
    - { put: version, params: { file: version/number } }
    - do:
      - task: set-dev-version
        config:
          image_resource: #@ task_image_config()
          platform: linux
          inputs:
          - name: version
          - name: repo
          - name: pipeline-tasks
          outputs:
          - name: repo
          run:
            path: pipeline-tasks/ci/tasks/set-dev-version.sh
          params:
            BRANCH: #@ data.values.git_branch
      - put: repo
        params:
          repository: repo
          rebase: true

- name: publish
  public: true
  plan:
  - in_parallel:
    - { get: repo, passed: [release] }
    - { get: version, passed: [release], trigger: true }
    - { get: pipeline-tasks }
  - task: publish-to-crates
    config:
      image_resource: #@ task_image_config()
      platform: linux
      inputs:
      - name: version
      - name: pipeline-tasks
      - name: repo
      params:
        CRATES_API_TOKEN: #@ data.values.crates_api_token
      caches:
      - path: cargo-home
      - path: cargo-target-dir
      run:
        path: pipeline-tasks/ci/tasks/publish-to-crates.sh
  - task: verify-publish
    config:
      image_resource: #@ task_image_config()
      platform: linux
      inputs:
      - name: version
      - name: pipeline-tasks
      run:
        path: pipeline-tasks/ci/tasks/verify-publish.sh

resources:
- name: repo
  type: git
  source:
    ignore_paths: ["ci/*[^md]"]
    uri: #@ data.values.git_uri
    branch: #@ data.values.git_branch
    private_key: #@ data.values.github_private_key

- name: concourse-resource-image
  type: registry-image
  source:
    tag: latest
    username: #@ data.values.docker_registry_user
    password: #@ data.values.docker_registry_password
    repository: #@ concourse_resource_image()

- name: concourse-resource-image-edge
  type: registry-image
  source:
    tag: edge
    username: #@ data.values.docker_registry_user
    password: #@ data.values.docker_registry_password
    repository: #@ concourse_resource_image()

- name: pipeline-tasks
  type: git
  source:
    uri: #@ data.values.git_uri
    branch: #@ data.values.git_branch
    paths: [ci/tasks/*, Makefile]
    private_key: #@ data.values.github_private_key

- name: pipeline-image
  type: registry-image
  source:
    tag: latest
    username: #@ data.values.docker_registry_user
    password: #@ data.values.docker_registry_password
    repository: #@ pipeline_image()

- name: pipeline-image-def
  type: git
  source:
    uri: #@ data.values.git_uri
    branch: #@ data.values.git_branch
    paths: [ci/image/Dockerfile]
    private_key: #@ data.values.github_private_key

- name: version
  type: semver
  source:
    driver: git
    file: number
    uri: #@ data.values.git_uri
    branch: #@ data.values.git_version_branch
    private_key: #@ data.values.github_private_key

- name: github-release
  type: github-release
  source:
    user:         #@ data.values.github_owner
    repository:   #@ data.values.github_repo
    access_token: #@ data.values.github_access_token
