I am late to using GitHub Actions for CI, and immediately ran into an issue trying to figure out how to conditionally install a different version of Bundler for a certain version of Rails in radar/distance_of_time_in_words#104. Bundler 2.x doesn’t work with Rails 4, and needs to be downgraded.
I decided to use default Bundler as much as possible, and run gem install bundler -v 1.17.3 for a certain version of Rails.
Use if:
My first solution was to use an if: step.
This worked because bundler was installed by default.
Use another if:
Using if: to install a non-default version of Bundler will suddenly break when some future run uses a newer, default, version of Bundler that breaks everything. We can craft another if: to install a specific version or, for now, display the default version of Bundler.
This worked using the negation operator !. I do find the lack of else: regrettable.
Use Bash Parameter Expansion
My final and favorite solution is to use bash parameter expansion. In Bash you can write ${BUNDLER:-2.1.4} which uses the value of $BUNDLER when available, and 2.1.4 otherwise.
I find this pattern quite elegant. To summarize.
Set a variable in matrix, e.g. bundler-version: 1.17.3.
Assign it to an environment variable via env, e.g. BUNDLER: $.
Use it with a default value in run, e.g. gem install bundler -v ${BUNDLER:-2.1.4}.