GitHub Actions Walkthrough: Part 1
Introduction
A few weeks ago, for a different project which used a separate front-end and back-end, I wanted to use Github Actions to auto-run my test suite, whenever I pushed a commit.
I was having issues getting the databases to talk to each other during the test run, but since I’d not used Github Actions much before, or any sort of ci/cd tooling, I wanted to get a ‘minimally working’ version of a Rails app on Github actions.
The steps below are very detailed, but represents a fair bit of the digging that I had to do to figure it all out. I’ve written the notes out here for myself and others, if it’s helpful, and I’ll do a part two soon, to explain how I got Github Actions working on an app that is ‘stateless’, and detatched from the backend.
Step 1: ‘rails new’
To start from scratch, generate a new rails app on your local machine. This is the configuration I am using for this walkthrough, making sure to set postgres
as the database, to make the GitHub Actions more real-world and realistic.
Step 2: Ensure local and remote repositories are connected
If you already have an existing repository on github, skip this step. Otherwise we will create a new repo.
- Create new repository attached to your github account at https://github.com/new
- Add repository name. Ensure that it matches the name of your rails app (for example, I will use
github_actions_walkthrough
) - Run github’s prescribed terminal commands.
- Refresh browser, make sure you can see your initial commit
Step 3.0 : Install rspec-rails
For the purposes of this guide, you can auto-generate tests, but if you’re trying to set up github actions on an existing rails project, you likely already have tests, so skip this step.
Add the rspec-rails
gem to your :development, :test
group in Gemfile
Step 3.1 : Use ‘rails generate scaffold’ to generate tests.
It could be helpful to add and commit work so far, but that’s up to you.
Step 4: Create .yml file that will be read by github actions
To trigger github actions, github will expect a yml file inside of .github/workflows/
Step 5: Fill new yml file with instructions for github actions:
Step 6: Make sure your tests work locally
Step 7: Commit it and see if your tests work
Push your new yml
file to GitHub, and check the actions
tab on the repository to watch the action get run. For me, I’d head to http://github.com/arnaldoaparicio/github_actions_walkthrough/actions/
Step 8: Realize your tests don’t work because config/database.yml
needs to be updated
The values in the test
development block in config/database.yml
need to include postgres user information to be used during the GitHub Actions run.
The values for host
, username
, and password
should match what you have in run_spec.yml
Push this all to GitHub, check the actions
tab output, and you should see a green check, and if you drill into the build and run tests
dropdown/collapsible menu item, you’ll see some output like:
Congrats!
Conclusion
Now, having read this guide, you know how to go from rails new
in your terminal, to a successful github actions “run” than checks that all of your tests are passing.
Sally forth and prosper.
Additional links
Here’s some of the resources and articles I was consulting and checking with as I worked through this GitHub Actions learning: