git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 0/2] replacing ci/config/allow-ref with a repo variable

From
Jeff King <peff@peff.net>
Date
Sep 11, 2023, 09:36 UTC
Message-ID
<20230911093616.GA1605460@coredump.intra.peff.net>
In-Reply-To
<fcdc682b-cf6c-4db9-9970-be136f48de58@gmail.com>
On Thu, Sep 07, 2023 at 11:04:33AM +0100, Phillip Wood wrote:
> I scanned the github documentation the other day and wondered if it would be
> possible to use with fromJson with a json array to do a prefx match on each
> element. It all sounds like it is getting a bit complicated though.

I poked around and I don't think this is possible. You can use contains() to do a full match of items in a json array, but I don't see any other way to iterate. What we'd really want is a "map" function, to do something like:

  map { startsWith(github.ref_name, $1) } fromJSON(vars.CI_CONFIG.allow_prefix)

But no such "map" exists (and I guess we'd need to collapse the result down to "is any item true", similar to perl's "grep" function).

Do you need multiple prefixes, or would one each of allow/reject be sufficient? I tried this and it works:

jobs:
  ci-config:
    name: config
    if: |
      (fromJSON(vars.CI_CONFIG).allow == '' ||
       contains(fromJSON(vars.CI_CONFIG).allow, github.ref_name)) &&
      (fromJSON(vars.CI_CONFIG).reject == '' ||
       !contains(fromJSON(vars.CI_CONFIG).reject, github.ref_name)) &&
      (fromJSON(vars.CI_CONFIG).allow-prefix == '' ||
       startsWith(github.ref_name, fromJSON(vars.CI_CONFIG).allow-prefix)) &&
      (fromJSON(vars.CI_CONFIG).reject-prefix == '' ||
       !startsWith(github.ref_name, fromJSON(vars.CI_CONFIG).reject-prefix))
And then various values of CI_CONFIG seem to work:
  - allow explicit branch names ("one" and "two" are run, everything
    else is skipped)
    { "allow": ["one", "two"] }
  - reject explicit names (everything except "three" is skipped)
    { "reject": ["three"] }
  - allow by prefix ("ok/* is run, everything else is skipped)
    { "allow-prefix": "ok/" }
  - reject by prefix (everything except "nope/*" is run)
    { "reject-prefix": "nope/" }
  - every key must approve to run, but missing keys default to running.
    So a reject overrides allow ("ok/one" and "ok/two" run, but
    "ok/three" does not)
    {
      "allow-prefix": "ok/",
      "reject", "ok/three"
    }

I suppose one hacky way to support multiple prefixes is to just unroll the loop ourselves (since missing keys are ignored). I didn't test it, but something like:

      (fromJSON(vars.CI_CONFIG).allow-prefix[0] == '' ||
       startsWith(github.ref_name, fromJSON(vars.CI_CONFIG).allow-prefix[0])) &&
      (fromJSON(vars.CI_CONFIG).allow-prefix[1] == '' ||
       startsWith(github.ref_name, fromJSON(vars.CI_CONFIG).allow-prefix[1])) &&
      (fromJSON(vars.CI_CONFIG).allow-prefix[2] == '' ||
       startsWith(github.ref_name, fromJSON(vars.CI_CONFIG).allow-prefix[2])) &&
       ...etc...
Would allow:
  {
    "allow-prefix": [
      "ok/",
      "also-ok/"
    ]
  }

Looking at the ci-config branch of phillipwood/git.git, I see this in your allow-refs:

      refs/heads/main|refs/heads/master|refs/heads/maint|refs/heads/next|refs/heads/seen|refs/tags/gitgui-*|refs/tags/pr-[0-9]*|refs/tags/v[1-9].*)

So you do use multiple prefixes, though all in refs/tags/. Do you actually push tags for which you do want to run CI, or would it be sufficient to just reject "refs/tags/" completely (though that implies using the fully qualified ref and not the short name; it would also be easy to add a boolean "allow tags" config option).

-Peff
Previous: Phillip WoodNext: Phillip Wood
Message 14 of 18 in “replacing ci/config/allow-ref with a repo variable”
  1. 0/2 replacing ci/config/allow-ref with a repo variableJeff King, Aug 30, 2023
  2. 1/2 ci: allow branch selection through "vars"Jeff King, Aug 30, 2023
  3. Johannes SchindelinSep 3, 2023
  4. Jeff KingSep 5, 2023
  5. Johannes SchindelinSep 5, 2023
  6. Jeff KingSep 7, 2023
  7. 2/2 ci: deprecate ci/config/allow-ref scriptJeff King, Aug 30, 2023
  8. Junio C HamanoAug 30, 2023
  9. Phillip WoodSep 1, 2023
  10. Jeff KingSep 1, 2023
  11. Phillip WoodSep 4, 2023
  12. Jeff KingSep 5, 2023
  13. Phillip WoodSep 7, 2023
  14. Jeff KingSep 11, 2023
  15. Phillip WoodSep 13, 2023
  16. Jeff KingSep 14, 2023
  17. Jeff KingSep 14, 2023
  18. Phillip WoodSep 14, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.