Back to Node III: GitHub Actions

Featured image

Part 3 in my quest to write a URL shortener in Typescript. If you haven’t read the previous parts, please check out Part 1 and Part 2.

Update: Part 4 is now available.

I was initially expecting this post to be really short. Using GitHub Actions I was going to build my container and store it in the GitHub package repository which there ia an action for. Great! I had a couple of small changes to my Compose file and then I should be good to go.

But that wasn’t the case.

Host Name Resolution Failure

I could run my Docker Compose command locally and everything worked as expected.

docker-compose --file docker-compose.test.yml run sut 

But when it was run as part of a GitHub Action I kept running into an issue where the test container could not talk to the app container with the following error:

FetchError: request to http://shorten:8000/ failed, reason: getaddrinfo ENOTFOUND shorten

Everything in my file looked to be correct and it was working on my local machine. I kept telling myself that Docker meant my local machine and the remote machine were the same, that’s why I use containers, this can’t be happening!

Check those Containers

I started making some changes to my local compose file like purposefully changing the username and password for the database to incorrect values. Surprisingly, the app still worked. All my tests were passing. Again, this can’t be happening!

Turns out I had a misunderstanding of how docker-compose run worked and had some old containers still running on my local box. I don’t now how long they had been running for but given I’d been trying to troubleshoot this on and off for a couple of days I’m going to go with days. As soon as I completely shut them down and pruned all my containers and images, I started getting the same error that I was experiencing on GitHub. Success (some)!

Of course I still wasn’t sure why this error was happening but at least I could recreate it.

Wait For It

So it turns out my app would attempt to connect to the DB once and if the db container wasn’t ready yet, the app would crash. Not ideal. This meant that the app container had already crashed by the time my test container wanted to use it. This crash also removes the hostname entry which is why the container wasn’t able to resolve the hostname. This also explains why it was working previously on my local machine as the DB was always running—it had never stopped running since I first started it.

Some re-jigging of the initial connection logic fixed the issue. But now I had a new integration test I needed to add: killing and restarting the DB.

GitHub Package Repoository ≠ Docker Hub

Turns out the Docker registry as part of the GitHub Package repository requires authentication to use. I’m not against keeping my images private but in this case I don’t have a need to.

To enable public access I needed to also push my image to the public Docker registry. Thankfully GitHub has me covered here as well.

- name: Push to Docker Hub
  uses: docker/build-push-action@v1
    username: ${{ secrets.DOCKER_USERNAME }}
    password: ${{ secrets.DOCKER_PASSWORD }}
    repository: ezzizzle/ziz-shortener
    tag_with_ref: true
    tags: latest
    add_git_labels: true

Now my image is available both in GitHub Packages and Docker Hub.

Next up is deploying to Kubernetes. I wasn’t expecting this to get to a Part IV.