I don't know if this is the right place for this, but I wanted to publish a solution I developed - mainly because it took me a little while to piece together, and I figure it may be useful to other people. I'm also keen to hear any feedback on the approach.
The problem
I have a Ruby Gem in a Bitbucket git repo, and I wanted to automate the process of testing and publishing new versions of the gem to RubyGems.org.
We've been having an awesome time rolling out Pipelines for all our Google App Engine hosted projects, but until now, our gem always had to be published manually. Could we put that into a CI/CD pipeline too?!
Configuring
bitbucket-pipelines.yml
image: ruby:2.6
pipelines:
pull-requests:
'**':
- step:
name: Run unit tests
caches:
- bundler
script:
- bundle install
- bundle audit check --update # <-- optional
- rake test
tags:
release-*:
- step:
name: Test and publish
caches:
- bundler
script:
- bundle install
- bundle audit check --update # <-- optional
- rake test
- mkdir -p /root/.gem
- touch /root/.gem/credentials
- chmod 0600 /root/.gem/credentials
- 'printf -- "---\n:rubygems_api_key: $GEM_HOST_API_KEY\n" > /root/.gem/credentials'
- rake build release:rubygem_push
definitions:
caches:
bundler: /usr/local/bundle
Explanation
This pipeline configuration defines two pipelines:
Key lines explained:
# Here we create the directory to store the RubyGem authentication file:
- mkdir -p /root/.gem
# We create the file and restrict access before we write sensitive data to it
- touch /root/.gem/credentials
# Also, if `credentials` is too open, RubyGems refuses to use it, much like ssh and ~/.ssh/id_rsa
- chmod 0600 /root/.gem/credentials
# Using echo is the obvious choice, but printf behaves more reliably across platforms
# The line has to be quoted and arguments must come after the --, otherwise Cthulhu is awakened
# This file should ultimately look identical to the one on your local machine.
- 'printf -- "---\n:rubygems_api_key: $GEM_HOST_API_KEY\n" > /root/.gem/credentials'
# `gem build && gem push XXX.gem` is the usual choice, but how do you know the filename to push?
# rake build && rake release:rubygem_push do the same thing, but take care of the changing filenames for you
- rake build release:rubygem_push
Notes
Hi Aidan, thanks so much for sharing!
I'm compiling some examples for our documentation, and I think this might be a useful link to add to them. Would that be ok?
Hi Aidan,
this is great content!! Why don't you turn this into an article??
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Linette, no problem at all! Feel free to use this however you like.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Umm, mostly because I don't know how!
When I created it, I think I only had 'Ask a question' and 'Start a discussion' as options.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.