|
| 1 | +### Shell Prompt Integration |
| 2 | + |
| 3 | +The Buildkite CLI offers a shell prompt integration that displays your current Buildkite organization directly in your prompt. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +#### Zsh (Vanilla) |
| 8 | + |
| 9 | +1. Create a prompt function in `~/.buildkite/zsh_prompt.zsh`: |
| 10 | + |
| 11 | +```zsh |
| 12 | +_buildkite_ps1() { |
| 13 | + local org=$(bk use 2>&1 | grep "Using configuration for" | sed -E "s/Using configuration for \`(.*)\`/\1/") |
| 14 | + if [[ -n "$org" ]]; then |
| 15 | + echo -n " (bk:$org)" |
| 16 | + fi |
| 17 | +} |
| 18 | + |
| 19 | +# Modify your existing prompt to include the Buildkite organization |
| 20 | +PROMPT='%n@%m %1~$(_buildkite_ps1)%# ' |
| 21 | +``` |
| 22 | + |
| 23 | +2. Source the script in your `.zshrc`: |
| 24 | + |
| 25 | +```zsh |
| 26 | +source $HOME/.buildkite/zsh_prompt.zsh |
| 27 | +``` |
| 28 | + |
| 29 | +#### Zsh (Powerlevel10k) |
| 30 | + |
| 31 | +1. Add the following function to `~/.buildkite/zsh_prompt.zsh`: |
| 32 | + |
| 33 | +```zsh |
| 34 | +_buildkite_ps1() { |
| 35 | + # Cache the prompt output for 5 seconds to avoid running bk too frequently |
| 36 | + if [[ -z "$BK_PROMPT_CACHE" ]] || [[ $(($EPOCHSECONDS % 5)) -eq 0 ]]; then |
| 37 | + local org=$(bk use 2>&1 | grep "Using configuration for" | sed -E "s/Using configuration for \`(.*)\`/\1/") |
| 38 | + if [[ -n "$org" ]]; then |
| 39 | + BK_PROMPT_CACHE="%F{magenta}(bk:$org)%f" |
| 40 | + else |
| 41 | + BK_PROMPT_CACHE="%F{yellow}(bk:not configured)%f" |
| 42 | + fi |
| 43 | + fi |
| 44 | + echo -n "$BK_PROMPT_CACHE" |
| 45 | +} |
| 46 | + |
| 47 | +# Wrap the bk command to clear prompt cache when switching orgs |
| 48 | +bk() { |
| 49 | + command bk "$@" |
| 50 | + if [[ "$1" == "use" ]]; then |
| 51 | + unset BK_PROMPT_CACHE |
| 52 | + fi |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +2. Source this script in your `.zshrc`: |
| 57 | + |
| 58 | +```zsh |
| 59 | +source $HOME/.buildkite/zsh_prompt.zsh |
| 60 | +``` |
| 61 | + |
| 62 | +3. Add the Buildkite organization to your prompt elements in `~/.p10k.zsh`: |
| 63 | + |
| 64 | +```zsh |
| 65 | +typeset -g POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=( |
| 66 | + # ... other existing elements |
| 67 | + buildkite_org |
| 68 | +) |
| 69 | +``` |
| 70 | + |
| 71 | +#### Bash |
| 72 | + |
| 73 | +1. Create a prompt function in `~/.buildkite/bash_prompt.sh`: |
| 74 | + |
| 75 | +```bash |
| 76 | +_buildkite_ps1() { |
| 77 | + local org=$(bk use 2>&1 | grep "Using configuration for" | sed -E "s/Using configuration for \`(.*)\`/\1/") |
| 78 | + if [[ -n "$org" ]]; then |
| 79 | + echo -n " (bk:$org)" |
| 80 | + fi |
| 81 | +} |
| 82 | + |
| 83 | +# Modify your PS1 to include the Buildkite organization |
| 84 | +export PS1='\u@\h \w$(_buildkite_ps1)\$ ' |
| 85 | +``` |
| 86 | + |
| 87 | +2. Source the script in your `.bashrc`: |
| 88 | + |
| 89 | +```bash |
| 90 | +source $HOME/.buildkite/bash_prompt.sh |
| 91 | +``` |
| 92 | + |
| 93 | +#### Features |
| 94 | + |
| 95 | +- Displays current Buildkite organization in your shell prompt |
| 96 | +- Caches organization info to minimize performance impact |
| 97 | +- Works across different projects and directories |
| 98 | +- Supports quick organization switching with `bk use` |
| 99 | + |
| 100 | +#### Troubleshooting |
| 101 | + |
| 102 | +- Ensure you've run `bk configure` to set up your organization |
| 103 | +- Verify the `.bk.yaml` in your project's root directory |
| 104 | +- Check that you're using a locally built `bk` binary in development projects |
| 105 | + |
| 106 | +#### Performance Considerations |
| 107 | + |
| 108 | +The prompt integration uses a lightweight method to retrieve the current organization. However, to minimize any potential performance impact: |
| 109 | +- The script caches the organization name |
| 110 | +- The command is only run periodically or when switching organizations |
| 111 | +- You can customize the caching mechanism if needed |
0 commit comments