33

I'm working on a GitHub Action workflow that uses an array for input.

I used this solution to simulate an array:

      - uses: actions/my-custom-ci
        with:
          subdirectories: src/main/java src/test/java

But I want to use a solution like this:

      - uses: actions/my-custom-ci
        with:
          subdirectories: 
                - src/main/java 
                - src/test/java

Is it possible to use an array input for custom GitHub Actions? If yes, how can we use an array input for custom GitHub Actions?

2 Answers 2

37

At the time of writing this answer, GitHub Actions doesn't support array types as input for actions. It only supports string | number | boolean (schema: with ref: definitions/env). So your approach is a valid workaround for now.

Just note that GitHub runners have jq installed by default, and GitHub Actions offers methods like fromJSON, toJSON and join, which may help you create a cleaner solution in case you want to generate a dynamic input of your custom action.

You can check google-github-actions/get-secretmanager-secrets's implementation where they accept multiple inputs specified by line breaks, not as a yaml array:

    - id: 'secrets'
      uses: 'google-github-actions/get-secretmanager-secrets@v1'
      with:
        secrets: |-
          token:my-project/docker-registry-token
          anotherOne:my-project/a-secret
          anotherOneToo:my-project/another-secret

Definitely, this might not be what you want to achieve. And it might not be worth refactoring your action. But it's a workaround for now.

Sign up to request clarification or add additional context in comments.

Comments

1

I went with OP's method of a string input that is split into an array. I use a comma-separated string.

Call it in a step:

- name: delete artifacts
  uses: my-org/my-actions/actions/utility/delete-artifact@v0
  with:
    name: "scripts, event"
    continue-on-error: true

The Action implementation:

# action.yaml
inputs:
  name:
    description: The name of the artifact(s) to delete. Comma separate multiple names.
    required: true

runs:
  using: node20
  main: dist/index.js
// index.ts
import { deleteArtifact }      from './main';
import { getInput, setFailed } from '@actions/core';

async function deleteArtifacts() {
  // validate Action input is not empty
  let names: string | string[] = getInput('name')

  if (names) {
    names = names.split(',').map(name => name.trim());
  }
  else {
    setFailed('No artifact names provided');
  }

  // if the input is valid, loop over the inputs and delete the artifacts
  for (const name of names) {
    deleteArtifact(name);
  }
}

deleteArtifacts();
// main.ts
import { DefaultArtifactClient } from '@actions/artifact';
import { info }                  from '@actions/core';

export async function deleteArtifact(name: string) {
  const artifact: DefaultArtifactClient = new DefaultArtifactClient();

  try {
    info(`Deleting artifact: ${name}`);
    await artifact.deleteArtifact(name);
  }
  catch (error) {
    info(`${(error as {message: string}).message}`);
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.