Skip to content

Add gh variable get command #9103

Description

@arnested

Describe the feature or problem you’d like to solve

It's currently possible to set a variable using, e.g., gh variable set FOO --body=BAR.

And it's possible to list all variables using:

$ gh variable list
NAME           VALUE       UPDATED             
FOO            BAR         about 12 minutes ago

It's not possible, though, to read a single variable using, e.g., gh variable get FOO.

It is possible to achieve using:

$ gh variable list --json name,value --jq '.[] | select( .name | contains("FOO")).value'
BAR

I'm pretty sure I won't be able to remember that command when I need it. And I'm pretty sure it'll require a lot of brain cycles to read and understand it.

Proposed solution

I propose to add a gh variable get FOO.

Activity

  1. williammartin commented on May 21, 2024

    @williammartin
    Member

    Thanks for the enhancement request. It seems like an obvious addition, I wonder why it wasn't there to begin with. However, no one has asked for it! It looks like in one of the original PRs for the gh variable list command it was possible to filter by name, but I can't find any place that this was discussed for removal.

    Can you describe your use case a little bit more? It's useful to us to know how people are using the CLI commands to help us understand the bigger picture.

  2. added
    more-info-neededMore info needed from user/contributor
    and removed on May 21, 2024
  3. arnested commented on May 21, 2024

    @arnested
    ContributorAuthor

    Can you describe your use case a little bit more? It's useful to us to know how people are using the CLI commands to help us understand the bigger picture.

    We are currently building an internal "starter kit" where we use some shell scripts to set up the individual projects.

    These script store some data in variables (e.g., which Jira projects a repository is connected to) and this info is then used by GitHub Action workflows.

    It's no problem to set the variables using gh variable set but I would like to get them first to make sure we don't override data from a previous run (and to be able to reuse already set data in other scripts).

  4. williammartin commented on May 21, 2024

    @williammartin
    Member

    Thanks!

    It's no problem to set the variables using gh variable set but I would like to get them first to make sure we don't override data from a previous run (and to be able to reuse already set data in other scripts).

    So if I understand correctly there are two outcomes:

    1. Don't overwrite variables that may have been previously set by other scripts
    2. Use the value of previously set variables in your script going forward

    That makes sense. I wondered whether gh variable set --no-clobber would help with 1. and be less prone to race conditions, but if you also really need the variables then 2. seems worthwhile as well. Thanks for letting me know.

  5. arnested commented on May 21, 2024

    @arnested
    ContributorAuthor

    In theory, I have race conditions, yes. In practice, probably not.

    Both solutions would be useful.

    I guess the current lack of a get command could possibly be that variables was based on secret and there it's most likely by design it has been left out (for secrets it's not listing the values using list either).

  6. williammartin commented on May 21, 2024

    @williammartin
    Member

    I guess the current lack of a get command could possibly be that variables was based on secret and there it's most likely by design it has been left out (for secrets it's not listing the values using list either).

    Yeh that's my suspicion as well. In the absence of any other obvious reason appearing, this seems like a good addition to me. I'm going to label it help wanted in case you or anyone else is interested in contributing.

  7. added and removed
    more-info-neededMore info needed from user/contributor
    on May 21, 2024
  8. arnested commented on May 21, 2024

    @arnested
    ContributorAuthor

    Ok, I took a stab at it. It's heavily based on the gh variable list and gh variable delete commands. I think I managed to keep what was needed and remove unwanted stuff.

  9. added a commit that references this issue on May 23, 2024
    08a5589
  10. changed the title [-]gh variable get[/-] [+]Add `gh variable get` command[/+] on May 23, 2024
  11. williammartin commented on May 23, 2024

    @williammartin
    Member

    I created #9118 as a follow up on this if you're interested in having a look at it I can point you in the right direction but no worries if you're not interested. Thanks!

  12. arnested commented on May 26, 2024

    @arnested
    ContributorAuthor

    Thank you, @williammartin!

    I wanted to take a look at #9118 today, but I can see @babakks beat me to it. Thank you, @babakks!

  13. babakks commented on May 26, 2024

    @babakks
    Member

    @arnested Sorry, I didn't notice this comment here. I was just looking for help-wanted issues. By the way, kudos for adding the variable get command.

  14. arnested commented on May 26, 2024

    @arnested
    ContributorAuthor

    No need to be sorry, @babakks 😄

    I didn't really have the time anyway 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementa request to improve CLIgh-variablerelating to the gh variable commandhelp wantedContributions welcome

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions