I have a private gitlab repo and I want to clone it. I have the password in an Environment variable. My gitlab repo URL with username and password variable is from the XML file.
sample.xml
<git>
<name>repo</name>
<link>https://user:#{ENV['password']}@gitlab.com/myrepo.git</link>
</git>
Ruby code:
@sample = Nokogiri::XML(File.open("sample.xml")
repo_link = @sample.xpath("/git/link/text()")
Git.clone(repo_link, 'repodir').checkout('master')
When I try the above code, #{ENV['password'] is not getting resolved and passed as it is to the GIT module.
Current: https://user:#{ENV['password']}@gitlab.com/myrepo.git
Expectation: https://user:<actual_password>@gitlab.com/myrepo.git
Kindly help with a way to fix this..
I can modify the XML file, Ruby code, or the GITLAB URL format. But the flow is fixed where the URL will be in XML and I have to parse it and clone the repo using Ruby.
Thanks in advance !!
#{}
means nothing in XML; an XML parser won't (and shouldn't) interpolate it...
You can use ERB templating, like this:
sample.xml
<% require 'cgi' %>
<git>
<name>repo</name>
<link>https://user:<%= CGI.escapeHTML ENV['password'] %>@gitlab.com/myrepo.git</link>
</git>
Important : I use CGI#escapeHTML
for making sure that your password doesn't break the XML.
Ruby code:
require 'erb'
xml = ERB.new(File.read('sample.xml'), nil, "<>").result
@sample = Nokogiri::XML(xml)
...
An other solution would be to put a placeholder in the XML, like this:
sample.xml
<% require 'cgi' %>
<git>
<name>repo</name>
<link>https://user:%{password}@gitlab.com/myrepo.git</link>
</git>
And replace it with the value in the Ruby code:
require 'cgi'
xml = File.read.sub(/%{password}/, CGI.escapeHTML ENV['password'])
@sample = Nokogiri::XML(xml)
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.