DEV Community

Cover image for Add this `.yml` file to your repo to give it a GitHub Actions-based shell
Thai Pangsakulyanont
Thai Pangsakulyanont

Posted on

Add this `.yml` file to your repo to give it a GitHub Actions-based shell

My Workflow

tl;dr: Adding this .yml file to your repository and you’ll get a GitHub Actions-based shell where you can run arbitrary commands from your browser.

Screenshot

Background: I occasionally find myself making large-scale automated codebase changes. For example, changing ESLint config or upgrading Prettier version, affecting hundreds of files.

For a large project, merge conflicts are bound to happen, as many engineers work on it simultaneously and merge their branches several times per day.

Because of this, when I work on PRs with automated codebase changes, I like to also put the script I use into the PR description. I would write something like this:

To re-generate the contents of this PR based on the latest master branch, run this script:



git fetch && \
  git checkout origin/master && \
  yarn add --dev --exact -W prettier && \
  yarn prettier --write . && \
  git add -A && \
  git commit -n -m 'Upgrade to latest prettier' && \
  git push -f origin HEAD:refs/heads/dtinth/prettier-upgrade


But even with this automated script, I still find it a bit inconvenient whenever I want to update this PR, because I might be working on something else. I would have to stash my changes, run the script, and switch back to my original branch. A context switching cost right there.

With the recently-announced support for manually triggered workflows, I can now ask GitHub Actions to do it for me, so I can continue working on my feature branch.

All I have to do is put this .yml file in the repository.

Submission Category:

Wacky Wildcards

Yaml File or Link to Code

.github/workflows/shell.yml



name: "GitHub Actions Shell"
on:
workflow_dispatch:
inputs:
command:
description: 'The command to run'
required: true
jobs:
run:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: ${{ github.event.inputs.command }}
env:
GIT_COMMITTER_NAME: GitHub Actions Shell
GIT_AUTHOR_NAME: GitHub Actions Shell
EMAIL: github-actions[bot]@users.noreply.github.com

Enter fullscreen mode Exit fullscreen mode




Additional Resources / Info

When you add the .yml file to your repository, you will see a new workflow appear in the Actions tab:

Screenshot

You can then click on the Run workflow button and type in the command that you want to run.

Screenshot

Here, GitHub Actions formatted the README file using Prettier and pushed it to the repository.

Screenshot

Other use cases include:

  • Running other manual maintenance tasks, e.g. updating website screenshots, batch-optimizing images, etc.

  • Manually publishing packages, e.g. npm publish

  • Prototyping new actions by manually running them on GitHub Actions before writing the actual workflow file. (If you find yourself having to make many changes to your workflow file before getting it to work, having a shell might help!)

  • Running ad-hoc tasks on the default branch (such as yarn audit), while working on something else locally.

That’s it, Thanks for reading!

Top comments (3)

Collapse
 
hedyhli profile image
hedy

Wow, this a great idea! A shell on the web straight from your github repo 😲

Collapse
 
guzzur profile image
Felix Razykov

So for each push to master you will have a prettify commit?

Collapse
 
dtinth profile image
Thai Pangsakulyanont

Hello, this workflow is only triggered manually; it is not automatic. It's for when you want to create a prettify commit, but rather than doing it on your local machine (where you may be working on another branch) we can ask GitHub Actions to do it for you.