Parametric machine_type_id

Hello there,

Is it possible to use a variable as machine_type_id for a workflow?

I would like to do something like this (where MACHINE_TYPE_ID is set to standard / elite machine type id):

  dynamic-stack-workflow:
    - step1
      (...)
    meta:
      bitrise.io:
        stack: linux-docker-android-20.04
        machine_type_id: ${{ MACHINE_TYPE_ID }}

But it gives me the following error:
Error saving app config: Invalid machine type for workflow: ‘dynamic-stack-workflow’.

Is this even possible?

1 Like

Hi @pierre-emmanuel.alti Thanks for posting your question!

No, unfortunately it is not possible to use a variable as machine_type_id for a workflow. The machine_type_id must be set to a specific machine type id, such as standard or elite.

Do you mind sharing why you want to use a variable instead? What is the end goal you are trying to achieve?

Thanks again,
Leilah

Hi Leilah, and thank you for your answer.

What I’m working on is what I call a dynamic stack step:

  • We are running a unit tests and quality checks pipeline for every pull request we open.
  • Our pipeline uses key based cache, and the keys are partly based on the name of the source branch.

It means that when we open a PR, the cache does not exist yet for this specific branch (it actually falls back to the main branch cache), so the first execution is a bit slower than when the cache is available.

I have made a step that uses your API to check if any cache key corresponds to this specific branch. This step can be run as the entry point of our check pipeline, it’s only taking a few seconds.

What I would like to do now, is to run the check build workflows on a elite or eliteXL machine if the cache does not exist. If the cache exists, I want to run it on a standard machine.

Firstly I want to share how much of a good idea this is for optimizing for shorter build times :clap:t4: :clap:t4: :clap:t4:

So based on my understanding, your step checks first is there is a cache associated with the branch. If not, the main branch cache will run and in that case you want to run it on the Elite machines. If thats correct, can you try the following in your step:

#!/bin/bash

# Get the name of the branch
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)

# Check if the branch name is in the key based cache
if bitrise cache:pull --key "$BRANCH_NAME"; then
  # Run the build on the Standard machine
  bitrise run --machine-type standard
else
  # Run the build on the EliteXL machine
  bitrise run --machine-type elitexl
fi"

Let me know if this was helpful or if you need additional help troubleshooting (in the case I completely misunderstood what you are trying to do ha ha).

Hi Leilah,

Thank you for your feedback and your answer. You got it, it’s exactly what I’m trying to do.

So I’ve tried your solution by running a simple workflow within a step by a bash script:

From the bitrise.yml file:

workflows:
  test-dispatch:
    steps:
    - script:
        inputs:
        - title: Dispatch run test
        - content: |-
            #!/bin/bash

            ## Attempt 1 like you mentioned
            bitrise run --machine-type standard

            ## Attempt 2, adding test-dispatch-run
            bitrise run --machine-type standard test-dispatch-run
        
  test-dispatch-run:
    steps:
    - script:
        (...)

But in both cases, I have the following error:

+ bitrise run --machine-type standard test-dispatch-run
Incorrect Usage: flag provided but not defined: -machine-type
NAME:
   bitrise run - Runs a specified Workflow.
USAGE:
   bitrise run [command options] [arguments...]
OPTIONS:
   --workflow value             workflow id to run.
   --config value, -c value     Path where the workflow config file is located.
   --inventory value, -i value  Path of the inventory file.
   --secret-filtering           Hide secret values from the log.
   --json-params value          Specify command flags with json string-string hash.
   --json-params-base64 value   Specify command flags with base64 encoded json string-string hash.
   --output-format value        Log format. Available values: json, console
   --path value, -p value       [Deprecated!!! Use 'config'] Path where the workflow config file is located.
   --config-base64 value        base64 encoded config data.
   --inventory-base64 value     base64 encoded inventory data.
   
flag provided but not defined: -machine-type

And indeed, even locally, I’ve tried to update Bitrise CLI to the latest version but it seems that this option --machine-type is not available.

I must have misunderstood something, or perhaps you’re using an internal version ?

Thanks in advance

1 Like

Hello,

The bitrise run command cannot be used in this way. Generally, the CLI cannot be used to select the machine type because it is running on a machine already.

However, one of the Bitrise APIs can be used instead

curl https://app.bitrise.io/app/[app]/build/start.json -L --data '{"build_params":{"branch":"pipelines","workflow_id":"primary","machine_type_id":"g2-m1.8core","stack":"osx-xcode-14.3.x-ventura"},"hook_info":{"build_trigger_token":"[token]","type":"bitrise"},"triggered_by":"curl"}'

vs

curl https://app.bitrise.io/app/[app]/build/start.json -L --data '{"build_params":{"branch":"pipelines","workflow_id":"primary","machine_type_id":"g2-m1-max.10core","stack":"osx-xcode-14.3.x-ventura"},"hook_info":{"build_trigger_token":"[token]","type":"bitrise"},"triggered_by":"curl"}'

The --machine-type parameter is not a valid CLI parameter.

I hope that this helps.

Best,