What is the correct way to addrepo with $releasever?

Hi all!

I’m setting up some install scripts (in my case, for a Docker build), and I need to add some repos. I would prefer not to put the version number in the scripts installing packages. Is there a way to do

# Dockerfile
FROM registry.opensuse.org/opensuse/leap:15.5.5
...
RUN sh -e install.sh
...

and in the file use maybe

zypper addrepo https://download.opensuse.org/repositories/devel:tools/$releasever/devel:tools.repo
zypper --gpg-auto-import-keys refresh
zypper install ...

I think it doesn’t work that way, but I’m sure there is a way to use zypper addrepo with the $releasever variable … :thinking:

Why not? Did you try it?

$releasever isn’t an environment variable, so the behavior is probably going to be a little different. If you were to run the command from the command-line in that manner, bash is going to use variable substitution in the environment to replace the string with what’s in the environment.

So my first instinct is that you’d probably have to escape it to get it added in as a literal string.

Then, of course, there’s getting the variable set to the value you want inside the docker container.

From the zypper man page:

$releasever, $releasever_major, $releasever_minor
Use this variable to refer to the version of your openSUSE or SUSE Linux. The value is obtained from the /product/version XML-node in /etc/products.d/baseproduct.

The base images in registry.opensuse.org (at least the 15.5.5.126 image that I checked; 15.5.5 doesn’t exist) included this file and the default repos configured with $releasever in them.

I’d probably just add the file on a host and then copy it into the image with a COPY directive, rather than using zypper ar - because that won’t work (the URL needs to be valid, but because $releasever isn’t an environment variable, it won’t be adde din that way. Using ar attempts to add the URL as specified.)

It appears I may have spoken too soon - while I do think it makes sense to use repo files, it looks like this may work:

zypper addrepo -f "https://download.opensuse.org/repositories/devel:tools/\$releasever/devel:tools.repo" devel_tools

The output I get for that command is:

localhost:~ # zypper addrepo -f "https://download.opensuse.org/repositories/devel:tools/\$releasever/devel:tools.repo" devel_tools
Adding repository 'devel_tools' .................................................................................................................................................................................................................................................[done]
Repository 'devel_tools' successfully added

URI         : https://download.opensuse.org/repositories/devel:tools/15.6/devel:tools.repo
Enabled     : Yes
GPG Check   : Yes
Autorefresh : Yes
Priority    : 99 (default priority)

Repository priorities are without effect. All enabled repositories share the same priority.

This did not work if I didn’t specify a repo alias at the end, nor did it if the URI was not in quotes.

The corresponding .repo file contains:

localhost:~ # cat /etc/zypp/repos.d/devel_tools.repo 
[devel_tools]
enabled=1
autorefresh=1
baseurl=https://download.opensuse.org/repositories/devel:tools/$releasever/devel:tools.repo

So the variable is preserved rather than being replaced with the version.

That said, I would probably still use the file copy method. The fact that it wouldn’t add without an alias name tells me that this might be an unexpected behavior, since using a version number instead of $releasever doesn’t require an alias be specified.

ETA: It does also work without the -f parameter.

Sorry, I missed that. Of course it should be escaped from the shell. But the string as it is shown is correct (and certainly wanted when all other repo URLs on the system have the same).

And the escaping from the shell should either be done by putting the whole between ' (which is not a bad idea in any case when one wants to forward such long strings which a great variety of different characters to a program through the shell), or use " but then one has to escape $ signs extra because " doesn’t.

I can confirm that using single ' delimiters does work if you remove the escape for the $ in $releasever:

localhost:~> sudo zypper addrepo -f 'https://download.opensuse.org/repositories/devel:tools/$releasever/devel:tools.repo' devel_tools
Adding repository 'devel_tools' ..........................................[done]
Repository 'devel_tools' successfully added

URI         : https://download.opensuse.org/repositories/devel:tools/15.6/devel:tools.repo
Enabled     : Yes
GPG Check   : Yes
Autorefresh : Yes
Priority    : 99 (default priority)

Repository priorities are without effect. All enabled repositories share the same priority.

Good callout there. I think I’d still be more likely to use the file method for a Docker container. Then there’s less chance of an error being made in modifying the Dockerfile.

1 Like