Performing repetitive tasks is time-consuming and can be tedious. Luckily, we have tools that we can leverage to do repetitive tasks for us and save time. Recently, I decided to automate the release process in one of my repositories. In this submission, I will share my journey of automating the release process using GitHub Actions and Python.
I maintain a repository that depends on new releases of
Detekt CLI. When there is a new version of
Detekt CLI, I receive email and make a new release of my repository whenever I have free time. With this approach, I used to make new releases by following the steps below:
- Receive an e-mail about the new release of Detekt.
- Update version in
README.mdwhenever I have free time.
- Commit changes.
- Create a new tag.
- Push changes.
- Publish the release.
Although the timing of new releases is critical, it is clear that the time difference from step 1 to step 2 can vary depending on my situation that might not make users happy. Besides the timing of new releases and keeping the users of my repository happy, personal time is also important which can be spent on more exciting things than just doing some work manually that can be automated.
As we know the problem and we have the steps written down, we can think of a way to approach this problem. The solution I wanted to make needed the following features:
- Check new releases of
Detekt CLIevery 24 hours.
- If the latest release of
Detekt CLIis different than the latest tag of my repository,
- Update the
Detekt CLIversion in
- Commit changes
- Create a new tag
- Create a release branch
- Push changes
- Update the
- When the new release branch is created, create a new pull request.
Creating a new release branch and a new pull request is something that I did not do when I used to make releases manually. I added the aforementioned steps to ensure that I can review changes so that broken code is not merged to the master branch. For checking new releases and running my Python script, I created two workflows where the first workflow runs the script every 24 hours, and the second workflow creates a pull request when a new release branch is created. Besides the workflow which runs the release script every 24 hours, I noticed that
workflow_dispatch trigger in GitHub actions can be used for running a workflow manually.
According to the release history of
Detekt, there are approximately two to three new releases each month. With the simple solution I created by using GitHub Actions and Python, my goal is to save at least 20 to 30 minutes each month. Besides, I can use the solution in other projects that can save a more significant amount of time in total.
name: Create Release on: schedule: - cron: "0 0 * * *" workflow_dispatch: jobs: check_release: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 with: python-version: 3.8 - name: Install dependencies run: | git config --local user.email "email@example.com" git config --local user.name "GitHub Action" python -m pip install --upgrade pip if [ -f .release-manager/requirements.txt ]; then pip install -r .release-manager/requirements.txt; fi - name: Run script run: | python .release-manager/create_release.py
GitHub Action: Detekt All
GitHub Action for running detekt checks to enforce best practices. Detekt is a static code analysis tool for Kotlin.
Version of the action is aligned with detekt versions.
name: detekt on: push: branches: [ master ] jobs: detekt: runs-on: ubuntu-latest steps: - name: "checkout" uses: actions/checkout@v2 - name: "detekt" uses: firstname.lastname@example.org
Usage with CLI parameters
name: detekt on: push: branches: [ master ] jobs: detekt: runs-on: ubuntu-latest steps: - name: "checkout" uses: actions/checkout@v2 - name: "detekt" uses: email@example.com with: args: --fail-fast --config detekt.yml
You can check available CLI parameters here
The purpose of this submission is to encourage you to leverage existing tools and save time by automating tasks. As you can see, once you write down the steps, automation is easy with the tools you use. I believe by checking existing actions, you can create workflow that works for your need.
Thanks for reading!