Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifying tasks types via environment variable / overriding task types #1215

Open
anarazel opened this issue Aug 9, 2023 · 4 comments
Open
Labels

Comments

@anarazel
Copy link

anarazel commented Aug 9, 2023

Hi,

For an open source process it makes a lot of sense to allow everyone to use cirrus-ci in their forks using the "free" credits, but to use custom compute or persistent runners for the, commonly much higher, usage of the project itself. The switch would need to happen based on what's present in repository level environment variables. Right now that's somewhat painful to implement in .cirrus.yml however.

Part of the issue are some small differences in the way tasks are specified. E.g. for custom_engine_instance: image families are specified via image: family/docker-kvm whereas for gce_instance: they are specified as image_family: ubuntu-2204-lts-arm64.

My current best guess for how to implement this is something roughly like:

linux_instance: &linux_instance
  matrix:
    - only_if: $BUILD_METHOD == 'ours'
      gce_instance:
        image_family: $image_family

    - only_if: $BUILD_METHOD == 'cirrus'
      compute_engine_instance:
        image: family/$image_family

macos_instance: &macos_instance
  matrix:
    - only_if: $BUILD_METHOD == 'ours'
      persistent_worker:
        isolation:
          tart:
            image: $image

    - only_if: $BUILD_METHOD == 'cirrus'
      macos_instance:
        image: $image

task:
  name: some linux task
  env:
    image_family: some-image
  <<: *linux_instance

task:
  name: some macos task
  env:
    image: ghcr.io/cirruslabs/macos-ventura-base:latest
  <<: *macos_instance

Which does end up somewhat painful...

Thanks,

Andres

@fkorotkov
Copy link
Contributor

I think you can already achieve this using Starlark and CIRRUS_REPO_OWNER environment variable. You can extract the YAML achors in two different files like .ci/our_instances.yml and .ci/cirrus_instances.yml and put something like the following in your .cirrus.star file:

load("cirrus", "env", "fs")

def main(ctx):
  if env.get("CIRRUS_REPO_OWNER") == "OUR GITHUB ORGANIZATION":
    return fs.read(".ci/our_instances.yml")

  return fs.read(".ci/cirrus_instances.yml")

@anarazel
Copy link
Author

anarazel commented Aug 9, 2023

Unfortunately I think that doesn't work, presumably due to yaml anchors having to be defined before they're used, and the starlark configuration is appended to .cirrus.yml. I get
Error while parsing tasks: INVALID_ARGUMENT: yaml: unknown anchor 'linux_instance' referenced!
and .cirrus.star isn't even executed.

@anarazel
Copy link
Author

anarazel commented Aug 9, 2023

Nor does my sketch from above work: matrix can be defined only under a task, docker_builder or pipe

@fkorotkov
Copy link
Contributor

You can probably move .cirrus.yml into .ci/cirrus.yml and then do concatication in any order you'd like in .cirrus.start.

load("cirrus", "env", "fs")

def main(ctx):
  if env.get("CIRRUS_REPO_OWNER") == "OUR GITHUB ORGANIZATION":
    return fs.read(".ci/our_instances.yml") + "\n" + fs.read(".ci/cirrus.yml")

  return fs.read(".ci/cirrus_instances.yml") + "\n" + fs.read(".ci/cirrus.yml")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants