Make an environment variable accessible from a formula

Viewed 1113

I try to write a private homebrew tap for gitlab thanks to this article: https://wheniwork.engineering/creating-a-private-homebrew-tap-with-gitlab-8800c453d893

Here is my formula:

class Pouet < Formula
  desc "Pouet command line utility"
  homepage "https://gitlab.mycompany.com/martin/Pouet"
  url "https://gitlab.mycompany.com/api/v4/projects/0000017/repository/archive?private_token=#{ENV['HOMEBREW_GITLAB_TOKEN']}"
  version "1.0"
  sha256 "ffdb029ada6d03fe3fa323577693cc233ef3d2300784a8d79077cecf6df7a470"
def install
    system "echo", ENV["HOMEBREW_GITLAB_TOKEN"]
  end
  test do
    pouet --help
  end
end

I defined HOMEBREW_GITLAB_TOKEN in my environment so it is accessible from the shell from which I launch the brew command.

Unfortunately the download fails because the HOMEBREW_GITLAB_TOKEN variable is not known from the brew process.

Any idea?

2 Answers

Homebrew has multiple levels of environment variable filtering. The first that gets encountered relates to the previous version of your question where you had the variable GITLAB_HOMEBREW_TOKEN. Homebrew's initial filtering allows only specific environment variables to get passed through to brew commands to avoid contaminating the build environment. So, GITLAB_HOMEBREW_TOKEN wouldn't get passed because it didn't start with HOMEBREW_ (or match any other whitelisted variables).

The second level of filtering is the one you're currently hitting where "sensitive" environment variables get filtered if they match the following Ruby regular expression:

/(cookie|key|token|password)/i

Because your variable has the keyword TOKEN in the variable name it gets filtered out before it reaches your Formula.

In short, you need to use a different environment variable name that starts with HOMEBREW_, such as:

HOMEBREW_GITLAB_SECRET=secretsquirrel brew install Pouet

and you'll have access to this in your Formula's Ruby code.

See also https://docs.brew.sh/Formula-Cookbook#using-environment-variables for the background on the types of environment variable filtering that exists.

Remove the $ from the env variable name

system "echo", ENV["GITLAB_HOMEBREW_TOKEN"]

See:

[1] pry(main)> ENV['LC_NAME']
=> "pl_PL.UTF-8"
[2] pry(main)> ENV['$LC_NAME']
=> nil
Related