variables:
  - &golang_image 'docker.io/golang:1.22.2'
  - &node_image 'docker.io/node:21-alpine'
  - &xgo_image 'docker.io/techknowlogick/xgo:go-1.22.1'
  - &xgo_version 'go-1.21.2'
  - &buildx_plugin 'docker.io/woodpeckerci/plugin-docker-buildx:3.2.1'
  - &platforms_release 'linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/386,linux/amd64,linux/ppc64le,linux/riscv64,linux/s390x,freebsd/arm64,freebsd/amd64,openbsd/arm64,openbsd/amd64'
  - &platforms_server 'linux/arm/v7,linux/arm64/v8,linux/amd64,linux/ppc64le,linux/riscv64'
  - &platforms_preview 'linux/amd64'
  - &platforms_alpine 'linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/amd64,linux/ppc64le'
  - &build_args 'CI_COMMIT_SHA=${CI_COMMIT_SHA},CI_COMMIT_BRANCH=${CI_COMMIT_BRANCH},CI_COMMIT_TAG=${CI_COMMIT_TAG}'

  # vars used on push / tag events only
  - publish_logins: &publish_logins # Default DockerHub login
      - registry: https://index.docker.io/v1/
        username: woodpeckerbot
        password:
          from_secret: docker_password
      # Additional Quay.IO login
      - registry: https://quay.io
        username: 'woodpeckerci+wp_ci'
        password:
          from_secret: QUAY_IO_TOKEN
  - &publish_repos_server 'woodpeckerci/woodpecker-server,quay.io/woodpeckerci/woodpecker-server'
  - &publish_repos_agent 'woodpeckerci/woodpecker-agent,quay.io/woodpeckerci/woodpecker-agent'
  - &publish_repos_cli 'woodpeckerci/woodpecker-cli,quay.io/woodpeckerci/woodpecker-cli'
  - path: &when_path # web source code
      - 'web/**'
      # api source code
      - 'server/api/**'
      # go source code
      - '**/*.go'
      - 'go.*'
      # schema changes
      - 'pipeline/schema/**'
      # Dockerfile changes
      - 'docker/**'
      # pipeline config changes
      - '.woodpecker/docker.yaml'

when:
  - event: [pull_request, tag]
  - event: push
    branch:
      - renovate/*
  - event: push
    branch: ${CI_REPO_DEFAULT_BRANCH}
    path: *when_path

steps:
  vendor:
    image: *golang_image
    pull: true
    commands:
      - go mod vendor
    when:
      - event: pull_request
        evaluate: 'CI_COMMIT_PULL_REQUEST_LABELS contains "build_pr_images"'
      - event: pull_request
        path: *when_path
      - branch:
          - ${CI_REPO_DEFAULT_BRANCH}
          - renovate/*
        event: [push, tag]
        path: *when_path

  ###############
  # S e r v e r #
  ###############
  build-web:
    image: *node_image
    directory: web/
    commands:
      - corepack enable
      - pnpm install --frozen-lockfile
      - pnpm build
    when:
      - event: pull_request
        evaluate: 'CI_COMMIT_PULL_REQUEST_LABELS contains "build_pr_images"'
      - event: pull_request
        path: *when_path
      - branch:
          - ${CI_REPO_DEFAULT_BRANCH}
          - renovate/*
        event: [push, tag]
        path: *when_path

  cross-compile-server-preview:
    depends_on:
      - vendor
      - build-web
    image: *xgo_image
    pull: true
    commands:
      - apt update
      - apt install -y tree
      - make cross-compile-server
    environment:
      PLATFORMS: linux|amd64
      TAGS: bindata sqlite sqlite_unlock_notify netgo
      XGO_VERSION: *xgo_version
    when:
      - event: pull_request
        evaluate: 'CI_COMMIT_PULL_REQUEST_LABELS contains "build_pr_images"'
      - event: pull_request
        path: *when_path
      - event: push
        path: *when_path
        branch: renovate/*

  cross-compile-server:
    depends_on:
      - vendor
      - build-web
    image: *xgo_image
    pull: true
    commands:
      - apt update
      - apt install -y tree
      - make cross-compile-server
    environment:
      PLATFORMS: linux|arm/v7;linux|arm64/v8;linux|amd64;linux|ppc64le;linux|riscv64
      TAGS: bindata sqlite sqlite_unlock_notify netgo
      XGO_VERSION: *xgo_version
    when:
      branch:
        - ${CI_REPO_DEFAULT_BRANCH}
      event: [push, tag]
      path: *when_path

  publish-server-preview:
    depends_on:
      - cross-compile-server-preview
    image: *buildx_plugin
    settings:
      repo: woodpeckerci/woodpecker-server
      dockerfile: docker/Dockerfile.server.multiarch
      platforms: *platforms_preview
      tag: pull_${CI_COMMIT_PULL_REQUEST}
      logins: *publish_logins
    when: &when-preview
      evaluate: 'CI_COMMIT_PULL_REQUEST_LABELS contains "build_pr_images"'
      event: pull_request

  publish-server-alpine-preview:
    depends_on:
      - cross-compile-server-preview
    image: *buildx_plugin
    settings:
      repo: woodpeckerci/woodpecker-server
      dockerfile: docker/Dockerfile.server.alpine.multiarch
      platforms: *platforms_preview
      tag: pull_${CI_COMMIT_PULL_REQUEST}-alpine
      logins: *publish_logins
    when: *when-preview

  build-server-dryrun:
    depends_on:
      - vendor
      - build-web
      - cross-compile-server-preview
    image: *buildx_plugin
    settings:
      dry_run: true
      repo: woodpeckerci/woodpecker-server
      dockerfile: docker/Dockerfile.server.multiarch
      platforms: *platforms_preview
      tag: pull_${CI_COMMIT_PULL_REQUEST}
    when: &when-dryrun
      - evaluate: 'not (CI_COMMIT_PULL_REQUEST_LABELS contains "build_pr_images")'
        event: pull_request
        path: *when_path
      - event: push
        path: *when_path
        branch: renovate/*

  publish-next-server:
    depends_on:
      - cross-compile-server
    image: *buildx_plugin
    settings:
      repo: *publish_repos_server
      dockerfile: docker/Dockerfile.server.multiarch
      platforms: *platforms_server
      tag: [next, 'next-${CI_COMMIT_SHA:0:10}']
      logins: *publish_logins
    when: &when-publish-next
      branch: ${CI_REPO_DEFAULT_BRANCH}
      event: push
      path: *when_path

  publish-next-server-alpine:
    depends_on:
      - cross-compile-server
    image: *buildx_plugin
    settings:
      repo: *publish_repos_server
      dockerfile: docker/Dockerfile.server.alpine.multiarch
      platforms: *platforms_alpine
      tag: [next-alpine, 'next-${CI_COMMIT_SHA:0:10}-alpine']
      logins: *publish_logins
    when: *when-publish-next

  release-server:
    depends_on:
      - cross-compile-server
    image: *buildx_plugin
    settings:
      repo: *publish_repos_server
      dockerfile: docker/Dockerfile.server.multiarch
      platforms: *platforms_server
      # remove 'latest' on older version branches to avoid accidental downgrade
      tag: [latest, '${CI_COMMIT_TAG}']
      logins: *publish_logins
    when: &when-release
      event: tag

  release-server-alpine:
    depends_on:
      - cross-compile-server
    image: *buildx_plugin
    settings:
      repo: *publish_repos_server
      dockerfile: docker/Dockerfile.server.alpine.multiarch
      platforms: *platforms_alpine
      # remove 'latest-alpine' on older version branches to avoid accidental downgrade
      tag: [latest-alpine, '${CI_COMMIT_TAG}-alpine']
      logins: *publish_logins
    when: *when-release

  #############
  # A g e n t #
  #############

  publish-agent-preview:
    depends_on:
      - vendor
    image: *buildx_plugin
    settings:
      repo: woodpeckerci/woodpecker-agent
      dockerfile: docker/Dockerfile.agent.multiarch
      platforms: *platforms_preview
      tag: pull_${CI_COMMIT_PULL_REQUEST}
      build_args: *build_args
      logins: *publish_logins
    when: *when-preview

  build-agent-dryrun:
    depends_on:
      - vendor
    image: *buildx_plugin
    settings:
      dry_run: true
      repo: woodpeckerci/woodpecker-agent
      dockerfile: docker/Dockerfile.agent.multiarch
      platforms: *platforms_preview
      tag: pull_${CI_COMMIT_PULL_REQUEST}
      build_args: *build_args
    when: *when-dryrun

  publish-next-agent:
    depends_on:
      - vendor
      # we also depend on cross-compile-server as we would have to hight
      # ram usage otherwise
      - cross-compile-server
    image: *buildx_plugin
    settings:
      repo: *publish_repos_agent
      dockerfile: docker/Dockerfile.agent.multiarch
      platforms: *platforms_release
      tag: [next, 'next-${CI_COMMIT_SHA:0:10}']
      logins: *publish_logins
      build_args: *build_args
    when:
      branch: ${CI_REPO_DEFAULT_BRANCH}
      event: push
      path: *when_path

  publish-next-agent-alpine:
    depends_on:
      - vendor
      # we also depend on cross-compile-server as we would have to hight
      # ram usage otherwise
      - cross-compile-server
    image: *buildx_plugin
    settings:
      repo: *publish_repos_agent
      dockerfile: docker/Dockerfile.agent.alpine.multiarch
      platforms: *platforms_alpine
      tag: [next-alpine, 'next-${CI_COMMIT_SHA:0:10}-alpine']
      logins: *publish_logins
      build_args: *build_args
    when: *when-publish-next

  release-agent:
    depends_on:
      - vendor
      # we also depend on cross-compile-server as we would have to hight
      # ram usage otherwise
      - cross-compile-server
    image: *buildx_plugin
    settings:
      repo: *publish_repos_agent
      dockerfile: docker/Dockerfile.agent.multiarch
      platforms: *platforms_release
      # remove 'latest' on older version branches to avoid accidental downgrade
      tag: [latest, '${CI_COMMIT_TAG}']
      logins: *publish_logins
      build_args: *build_args
    when: *when-release

  release-agent-alpine:
    depends_on:
      - vendor
      # we also depend on cross-compile-server as we would have to hight
      # ram usage otherwise
      - cross-compile-server
    image: *buildx_plugin
    settings:
      repo: *publish_repos_agent
      dockerfile: docker/Dockerfile.agent.alpine.multiarch
      platforms: *platforms_alpine
      # remove 'latest-alpine' on older version branches to avoid accidental downgrade
      tag: [latest-alpine, '${CI_COMMIT_TAG}-alpine']
      logins: *publish_logins
      build_args: *build_args
    when: *when-release

  #########
  # C L I #
  #########

  publish-cli-preview:
    depends_on:
      - vendor
    image: *buildx_plugin
    settings:
      repo: woodpeckerci/woodpecker-cli
      dockerfile: docker/Dockerfile.cli.multiarch
      platforms: *platforms_preview
      tag: pull_${CI_COMMIT_PULL_REQUEST}
      build_args: *build_args
      logins: *publish_logins
    when: *when-preview

  build-cli-dryrun:
    depends_on:
      - vendor
    image: *buildx_plugin
    settings:
      dry_run: true
      repo: woodpeckerci/woodpecker-cli
      dockerfile: docker/Dockerfile.cli.multiarch
      platforms: *platforms_preview
      tag: pull_${CI_COMMIT_PULL_REQUEST}
      build_args: *build_args
    when: *when-dryrun

  publish-next-cli:
    depends_on:
      - vendor
      # we also depend on publish-next-agent as we would have to hight
      # ram usage otherwise
      - publish-next-agent
    image: *buildx_plugin
    settings:
      repo: *publish_repos_cli
      dockerfile: docker/Dockerfile.cli.multiarch
      platforms: *platforms_release
      tag: [next, 'next-${CI_COMMIT_SHA:0:10}']
      logins: *publish_logins
      build_args: *build_args
    when: *when-publish-next

  publish-next-cli-alpine:
    depends_on:
      - vendor
      # we also depend on publish-next-agent as we would have to hight
      # ram usage otherwise
      - publish-next-agent
    image: *buildx_plugin
    settings:
      repo: *publish_repos_cli
      dockerfile: docker/Dockerfile.cli.alpine.multiarch
      platforms: *platforms_alpine
      tag: [next-alpine, 'next-${CI_COMMIT_SHA:0:10}-alpine']
      logins: *publish_logins
      build_args: *build_args
    when: *when-publish-next

  release-cli:
    depends_on:
      - vendor
      # we also depend on release-agent as we would have to hight
      # ram usage otherwise
      - release-agent
    image: *buildx_plugin
    settings:
      repo: *publish_repos_cli
      dockerfile: docker/Dockerfile.cli.multiarch
      platforms: *platforms_release
      # remove 'latest' on older version branches to avoid accidental downgrade
      tag: [latest, '${CI_COMMIT_TAG}']
      logins: *publish_logins
      build_args: *build_args
    when: *when-release

  release-cli-alpine:
    depends_on:
      - vendor
      # we also depend on release-agent as we would have to hight
      # ram usage otherwise
      - release-agent
    image: *buildx_plugin
    settings:
      repo: *publish_repos_cli
      dockerfile: docker/Dockerfile.cli.alpine.multiarch
      platforms: *platforms_alpine
      # remove 'latest-alpine' on older version branches to avoid accidental downgrade
      tag: [latest-alpine, '${CI_COMMIT_TAG}-alpine']
      logins: *publish_logins
      build_args: *build_args
    when: *when-release