Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TBT-137 Be able to use environment variables across different repos #325

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions lib/travis/scheduler/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
require 'travis/scheduler/record/subscription'
require 'travis/scheduler/record/trial'
require 'travis/scheduler/record/user'
require 'travis/scheduler/record/account_env_vars'
6 changes: 6 additions & 0 deletions lib/travis/scheduler/record/account_env_vars.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class AccountEnvVars < ActiveRecord::Base
serialize :value, Travis::EncryptedColumn.new
belongs_to :owner, polymorphic: true
end
28 changes: 27 additions & 1 deletion lib/travis/scheduler/serialize/worker/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,28 @@ class Job < Struct.new(:job, :config)
def env_vars
vars = repository.settings.env_vars
vars = vars.public unless secure_env?
vars.map { |var| env_var(var) }

mapped_vars = vars.map { |var| env_var(var) }

account_vars = account_env_vars
Travis.logger.info "Mapped account env vars: #{account_vars}"

repo_var_hash = mapped_vars.map { |v| [v[:name], v] }.to_h
account_var_hash = account_vars.map { |v| [v[:name], v] }.to_h

final_vars_hash = repo_var_hash.merge(account_var_hash)

final_vars = final_vars_hash.values
Travis.logger.info "Merged env vars: #{final_vars}"

final_vars
end

def account_env_vars
Travis.logger.info "Fetching account env vars for owner: #{job.owner_id} with owner type: #{job.owner_type}"
vars = AccountEnvVars.where(owner_id: job.owner_id, owner_type: job.owner_type)
Travis.logger.info "Results for owner: #{job.owner_id}, variables: #{vars}"
vars.map { |var| account_env_var(var) }
end

def secure_env?
Expand Down Expand Up @@ -84,6 +105,11 @@ def env_var(var)
{ name: var.name, value: var.value.decrypt, public: var.public, branch: var.branch }
end

def account_env_var(var)
{ name: var.name, value: var.value, public: var.public, branch: nil }
end


def has_secure_vars?(key)
job.config.key?(key) &&
job.config[key].respond_to?(:key?) &&
Expand Down