DEV Community

Cover image for Maybe this useless workflow isn’t so useless after all?
Thai Pangsakulyanont
Thai Pangsakulyanont

Posted on

Maybe this useless workflow isn’t so useless after all?

My Workflow

You probably know about The Most Useless Machine:

Let’s build it in GitHub Actions!

When I add the useless label to a PR or an issue, GitHub Actions removes it

Submission Category:

Wacky Wildcards

Yaml File or Link to Code

.github/workflows/useless.yml

name: Useless
on:
  issues:
    types: [labeled]
  pull_request:
    types: [labeled]
jobs:
  useless:
    if: github.event.label.name == 'useless'
    runs-on: ubuntu-latest
    steps:
      - name: Unlabel
        run: |
          curl -X DELETE "https://api.github.com/repos/$GITHUB_REPOSITORY/issues/${{ github.event.issue.number || github.event.pull_request.number }}/labels/useless?access_token=${{ secrets.GITHUB_TOKEN }}"

Additional Resources / Info

I did not build this workflow out of boredom, but out of neccessity. Sometimes I want to run a well-defined, manually-triggered job on a PR.

For example, if a test failed because I forgot to update the test snapshots, I might want to ask GitHub Actions to go ahead and update the snapshot file, and then commit it to my PR for me.

Or maybe I want to run the end-to-end browser-based test suite which can cost money. Therefore, running them on every single push may not be economically efficient. I want to run them just before merging to the main branch, as a pre-flight build.

And sometimes, situations may require me to run it multiple times. I want to be able to do that without having to push a new commit to trigger it. So, I thought it might be a good idea to designate a label to trigger GitHub Actions, and have it immediately unlabel the PR, so that it is ready for the next trigger.

Maybe this useless workflow isn’t so useless after all. Feel free to use this as a template for your workflows.

Top comments (12)

Collapse
 
michaelcurrin profile image
Michael Currin

Would the new workflow dispatch trigger be a good alternative? You can make an action run on that event and exclude pushes etc. And you can trigger via UI button

github.blog/changelog/2020-07-06-g...

Collapse
 
dtinth profile image
Thai Pangsakulyanont • Edited

Yup, I would use workflow_dispatch when I want to run tasks on the default branch.

(In fact, I have an article on this feature 😁)

However, if I want to run tasks in context of a PR (or an issue) I think I’d still prefer a label approach.

Collapse
 
michaelcurrin profile image
Michael Currin

Okay great thanks. I haven't used workflow dispatch yet so I'll check that out

Collapse
 
developerkaren profile image
Karen Efereyan

Congrats Thai. I have zero knowledge of github actions though. Any resource you'd recommend?

Collapse
 
dtinth profile image
Thai Pangsakulyanont • Edited

Thanks!

There are many helpful resources on DEV.to but in my opinion the most important resource is GitHub’s official documentation.

You can think of GitHub Actions as like having a brand new computer that can automate things for you based on what happens and what's inside your GitHub Repository. To automate things in your computer you would write shell scripts. So it’s good to also learn the common command-line tools.

For example, consider the command I showed in this post:

curl -X DELETE "https://api.github.com/repos/$GITHUB_REPOSITORY/issues/${{ github.event.issue.number || github.event.pull_request.number }}/labels/useless?access_token=${{ secrets.GITHUB_TOKEN }}"
Enter fullscreen mode Exit fullscreen mode

You can’t find that command in GitHub Docs, but crafting that command takes these knowledge:

As you can see just learning from the official documentation may not be enough.

But unlike other systems that requires some setup on a 3rd party service, with GitHub Actions, all you need is a repository and a workflow file. Getting my hands dirty is the most effective way for me to learn something. So, I recommend creating small projects/repositories to try out GitHub Actions on. Here are some of mine:

  • github-actions-web-page-screenshot... In this project I want to try to make GitHub Actions take a screenshot of a web page.
  • github-actions-tsc-problem-matcher... In this project I want to set up GitHub Actions so that it displays compile-time errors in the “Files Changed” tab.
  • action-rotating-light In this project I want to ban a certain word/emoji from the project. For example I picked 🚨 as the forbidden emoji because I use it to mark code as “still unfinished, need to come back and fix this before merging.”
Collapse
 
dezren profile image
dezren

this comment could be a post in itself, the documentation is great and very accurate but real examples of how we should use it seem a bit lacking. great posts thanks for making them

Collapse
 
michaelcurrin profile image
Michael Currin

I wrote a post on this:)

dev.to/michaelcurrin/beginner-s-gu...

Collapse
 
abdisalan_js profile image
Abdisalan

Oooo this is nice! Very creative way to trigger workflows

Collapse
 
mornir profile image
Jérôme Pott

lol "CSS Layout destroyer" in your description indeed overflows on mobile 🤣

Collapse
 
dtinth profile image
Thai Pangsakulyanont

One duty of a CSS layout destroyer is to fill in forms with non-breaking spaces instead of regular ones. 😂

Collapse
 
benwinding profile image
Ben Winding

This made me laugh lol

Collapse
 
jackwoz profile image
Jacek Wozniak

Very neat!!