Oskar Dudycz

Pragmatic about programming

How to create a custom GitHub Action?

2021-05-12 oskar dudyczDevOps

cover

As you may know, I’m a fan of documentation. I wrote a few practical tips on this topic on the blog:

I believe that high-grade documentation lives and evolves with the project. Documentation makes sense when it reflects the current state of the code. There is nothing worse than outdated documentation; If we have to look at the code anyway, it’s a waste of time.

Today I would like to show you a little bit of our kitchen. It would be an understatement to say that the EventStoreDB documentation wasn’t the best. With painstaking work, we have slowly reached a place where you can say that the documentation is “OK”. EventStoreDB is a large, complex project. There are many use cases to consider for each change. Until now, the documentation was created after the functionality was added. Programmers were adding a piece of information into the Changelog. Then someone from our DevAdvocacy team was adding docs to that. It wasn’t perfect, because us DevAdvocates were not always able to keep pace.

Additionally, we might not know all the details of the implementation without asking questions or analyzing the code. Further, a strong focus on documentation takes our time for other initiatives (e.g. better samples, materials, etc.). Therefore, the step we wanted to do was move the documentation to the code repositories. This allows them to be included in the normal Code Review process.

We had already preconfigured scripts for copying documentation from other repositories (see: https://github.com/EventStore/documentation/blob/master/import/import-client-docs.js). However, it was not as trivial as it might seem. A lot of things had to be standardized and taken into account. For example, the URL should match the previous one, so that:

  • SEO was correct.
  • old URLs were opening.
  • Internal linking worked.

Documentation for different versions of EventStoreDB varies. If we wanted the documentation to be where the code was, it still had to be placed on the appropriate release branches. This creates some difficulties because our documentation (despite significant improvements) still has gaps. We do not yet have the comfort of documenting only the latest version, as we often document things that touch upon several versions.

I think that with mechanical, repetitive work, it is easy to make stupid mistakes. We would need to remember when cherry-picking between several branches, and issuing separate pull requests etc. I am irritated when someone says “it’s easy, a programmer should remember this”. Maybe it’s simple, perhaps one could remember it, but what for? If we sum up such cases where we have to remember something, it turns out that we do nothing else at work other than going through a checklist in our head. Boring, repetitive situations have to be automated.

That’s why we decided to implement automation. Last week, I added a custom GitHub action, which will automatically do a cherry-pick of changes from a merged pull request. For each cherry-pick: {target_branch} label on the pull request, it will create a separate PR with a copy of the changes.

GitHub allows you to do your own actions using Docker or JavaScript. I used the latter approach. The action code itself is relatively simple, but for how much swearing and nerves it cost me… that’s a secret! GitHub actions enable a lot of scenarios, but they have terrible documentation. Hence my idea to share this example with you.

We can define our own action in the repository where the code is located. We can also create a dedicated repository that will store shared actions. We chose the latter solution because:

  • It’s easier to maintain.
  • Changes put there will be immediately visible the next time you run the action in another repository. No additional changes needed, etc.
  • You can have a code shared between different actions.

What problems did I encounter besides the poor GitHub documentation? For example, if we do an action using JavaScript, we have to keep node_modules in the repository. Currently, GitHub will not install the dependencies itself when starting the pipeline. This makes sense because we have the known dependencies on every call. Nevertheless, it is most of all a pain in the ass. As you know, node_modules can take up a lot of space. We have to be strict about their weight. Additionally, our .gitignore rules can play a joke on us. For example, I lost a lot of time when it turned out that the rule ignored files from one package. One of the libraries I used had a dependency on the NPM package named debug. That was irritating.

GitHub Action code is available here:

As you can see, the code is relatively simple. It uses an SDK shared from GitHub called Octokit (https://docs.github.com/en/rest/overview/libraries). I am pretty happy with this action because apart from the logic, it can also comment and inform about the status. It makes the process way easier. For a sample comment, see here: https://github.com/EventStore/EventStore/pull/2903#pullrequestreview-633505402.

How to use this action?

Just define the workflow in the repository (https://github.com/EventStore/EventStore/blob/master/.github/workflows/cherry-pick-pr-for-label.yml):

name: Cherry pick PR commits for label
on:
  # you can use pull_request, but...
  # pull_request_target runs with the approver permission
  # which is critical to enable also for forks
  pull_request_target:
    types: [closed]
jobs:
  cherry_pick_pr_for_label:
    name: Cherry pick PR commits for label
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Cherry Pick PR for label
        # you setup here the separate repository
        # where action is located:
        # - repository: EventStore/Automations
        # - relative path: /cherry-pick-pr-for-label
        # - branch: @master
        uses: EventStore/Automations/cherry-pick-pr-for-label@master
        with:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Interestingly. I did not find it anywhere in the documentation…

What’s even more interesting: you can use this action in your repository. For example, to sync code across branches. Of course, long-lived code branches are a pain. However, we all know that sometimes you cannot do without them.

Your own GitHub actions give you a lot of possibilities. You can create very interesting automations. You have to work through these first problems. I hope this article and these examples will make it easier for you, and I encourage you to play with this code. Let me know what potential uses you see yourself.

Read also other articles around DevOps process:

Cheers!

Oskar

👋 If you found this article helpful and want to get notification about the next one, subscribe to Architecture Weekly.

✉️ Join over 11500 subscribers, get the best resources to boost your skills, and stay updated with Software Architecture trends!

Loading...
Event-Driven by Oskar Dudycz

cover

Through my window, I see the result of good plans but poor execution. Opposite my flat, there is a partially completed construction place. Buildings were supposed to be eye-catching Mediterranean style apartments. Delivery date? Two years ago. Actual? More and more unknown.

Some time ago, I heard that using Event Sourcing makes creating Event-Driven Architecture easier. The arguments were correct, that if we’re already publishing events to trigger business workflows, then at some point, we may want to also store events to not lose information. Agreed. However, I also heard that keeping the state as events will simplify things. We’ll have a source of truth with a record of the system behaviour. This will allow, e.g. to confront the results of the operations with the recorded state. I’d agree with that, with one distinction. It’s easier as long as you already know Event Sourcing.

Many people in the DDD community claim that the essential is to properly break down the system into autonomous parts called bounded contexts. Once we have it, the rest is secondary and will sort itself out. For sure.

Many seasoned programmers speak similarly about new technologies. They claim that they can translate past experience into new technologies. That’s true that by analogy, they can catch the big picture quicker. But isn’t it a bold assumption to say that Win.Forms specialist will learn Angular quickly?

The end result may differ a lot from the initial ideas. I saw the plan of those buildings next to me. Now I can see the effects of the execution. Or actually, the lack.

I believe that we should carefully acknowledge not only the point of view of our authorities but also their seating point. If we want to find out how to form a wall, do we ask an architect or a foreman? An architect may know the theory, but the practice is what we’re looking for. On the other hand, if you want to know where to put the wall, you prefer the architect to do measurements. At least if you don’t want to have the roof falling to your head.

After I had torn a ligament in my knee, I went to two qualified orthopedists. One said I should have surgery and do a reconstruction. The second stated that there is no need for that; rehabilitation should be enough. Guess which one had a specialization in surgery and which in rehabilitation?

People usually give us advice from the point where they’re currently standing. They are entitled to a biased view. An architect who rarely does programming will tend to downplay the value of implementation and tactical patterns. Midlevel developers will focus on technicalities instead of the global system impact. The team manager or consultant will emphasize the importance of soft skills (or esoteric techniques known only to them).

The truth is that we need all of them. The excellent plan will fall on the bad execution. The best execution for the wrong case will be just a waste of time. We should carefully evaluate the advice considering what we need and what an expert can give us.

Therefore, when we’re reading an article, watching a talk, let’s also pay attention to the place where the person is standing. The perspective from there may be much different from where we are right now. That can be good, as it may push us in the right direction. But it may also be misleading, as we accidentally take biases of this person without understanding the tradeoffs. Personally, I prefer to follow not only people from pedestal but also those that are closer to my position. A bit further in the journey, but not too far. That helps me to calibrate my view as those people are more relative to my daily struggles.

Polish historical leader Józef Piłsudzki reportedly used to say: “Right is like an ass, everyone has its own”.

Cheers!

Oskar