Drone template not triggering build

Following is how are .drone.yml looks like (and template also listed below) this an example configuration very much how we want in our production. The reason we are using a template is that our staging and production have similar configurations with values different in them(Hence circuit template). And we wanted to remove duplication using the template circuit.yaml.
But currently, we are unable to do so I don’t define the test.yaml(template) and have test step imported without template (and have the circuit template define to avoid the duplicate declaration of staging and production build) the drone build fails with

"template converter: template name given not found

If I define the test step as a template. I see the test step working but on creating the tag I see the following error

{"commit":"28ac7ad3a01728bd1e9ec2992fee36fae4b7c117","event":"tag","level":"info","msg":"trigger: skipping build, no matching pipelines","pipeline":"test","ref":"refs/tags/v1.4.0","repo":"meetme2meat/drone-example","time":"2022-01-07T19:16:15+05:30"}

---
kind: template
load: test.yaml
data:
  commands:
    - echo "machine github.com login $${GITHUB_LOGIN} password $${GITHUB_PASSWORD}" > /root/.netrc
    - chmod 600 /root/.netrc
    - go clean -testcache
    - echo "Running test"
    - go test -race ./...
  

---
kind: template
load: circuit.yaml
data:
  deploy: deploy
  create_tags:
    commands:
      - echo "Deploying version $DRONE_SEMVER"
      - echo -n "$DRONE_SEMVER,latest" > .tags
  backend_image:
    version: ${DRONE_SEMVER}
    tags:
      - '${DRONE_SEMVER}'
      - latest

And the template is below

// test.yaml
kind: pipeline
type: docker
name: test
steps:
  - name: test
    image: golang:latest
    environment:
      GITHUB_LOGIN:
        from_secret: github_username
      GITHUB_PASSWORD:
        from_secret: github_token
    commands:
      {{range .input.commands }}
      - {{ . }}
      {{end}}
    volumes:
      - name: deps
        path: /go

  - name: build
    image: golang:alpine
    commands:
      - go build -v -o out .
    volumes:
      - name: deps
        path: /go

volumes:
  - name: deps
    temp: {}

trigger:
  branch:
    - main
  event:
    - push
    - pull_request
// circuit.yaml
kind: pipeline
type: docker
name: {{ .input.deploy }}
steps:
  - name: create-tags
    image: alpine
    commands:
    {{range .input.create_tags.commands }}
    - {{ . }}
    {{end}}

  - name: build
    image: plugins/docker
    environment:
      GITHUB_LOGIN:
        from_secret: github_username
      GITHUB_PASSWORD:
        from_secret: github_token
      VERSION: {{ .input.backend_image.version }}
      SERVICE: circuits
    settings:
      auto_tag: false
      repo: ghcr.io/meetme2meat/drone-ci-example
      registry: ghcr.io
      dockerfile: Dockerfile
      tags:
        {{range .input.backend_image.tags }}
        - {{.}}
        {{end}}
      username:
        from_secret: github_username
      password:
        from_secret: github_token
trigger:
  event:
    - tag

you cannot define multiple templates in your .drone.yml file, however, you can return multiple pipelines from a single template.

@bradrydzewski
how can one return multiple pipelines from a single template? is it possible to retrieve one out of many defined in one template?

for some reason, the documentation is not updated here with such info: Yaml | Drone

what are the limitations of developing and supporting one template per pipeline? as I tend to see such questions rising quite enough, and that way seems a bit more user friendly and easier to understand, too.

[edit]
would one be able to reuse the same template with different variables set multiple times in same .drone.yml file?

A template is just a shared .drone.yml file with (optional) parametrization. Therefore anything you can do inside a .drone.yml file you can do in a template. For example, below is a multi-pipeline .drone.yml file. It is also a valid template.

kind: pipeline
type: docker
name: backend

steps:
- name: build
  image: golang
  commands:
  - go build
  - go test

---
kind: pipeline
type: docker
name: frontend

steps:
- name: build
  image: node
  commands:
  - npm install
  - npm test

Let’s assume you use the above yaml to create a template, named foo.yaml. You can then use this above template for your repository, which defines two pipelines that will be executed every time you run a build.

kind: template
load: foo.yaml
data: {}

@brad okay, that seems reasonable, thanks for clarification. :slight_smile:

Although, I would hope to use such template for multiple branches, e.g. once for staging, and once for production, but with different variables set per environment. and both times the template would be used in the same .drone.yml file, since e.g. my both environments have similar pipelines, just with minor differences in variables. it would be used in the same repository, also per multiple repos in the organization, too.

Would that be a somewhat feasible feature to have for templates?

@made2140 definitely a reasonable ask. The reason that we don’t have the capabilities being discussed in this thread is because the maintainers have been very busy working on other items (such as mac support, etc). I would love to see these capabilities in the future, and like all things, they will get added to the todo list and we will work on them as soon as resourcing and prioritization aligns.

1 Like