GitHub Actions for Linting and Compiling Bytecode #13152
DavesCodeMusings
started this conversation in
Show and tell
Replies: 1 comment 2 replies
-
AFAIK Linting with ruff assumes Cpython code. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
GitHub Actions for Linting and Compiling Bytecode
I recently figured out how to use GitHub Actions to automatically lint my code and compile it to a bytecode (.mpy) file. If you want to do something similar in your repository, here it is in a nutshell. An explanation as to why and how follows the example.
Figure 1: Contents of my repository's .github/workflows/main.yml
Why?
Previously, I was doing all my MicroPython code linting using the usual VS Code plug-ins for Python. When I wanted to pre-compile into bytecode, I used the mpy-cross Python module at the command-line.
These steps had to be done every time before pushing code to my repository. It works, but after about the eighteenth time, it gets rather tedious. I knew other folks were using GitHub Actions to do this automatically, but I had a tough time finding the relevant information to do what I wanted in a simple, direct way.
How?
I'm rather new to GitHub Actions, so I approached the problem with the goal of proving I could push something, anything, to a repository using a workflow. I started with an example I found on StackOverflow.
Figure 2: Example of generating a test file and pushing to a repository.
The first error I encountered was git complaining the file test.txt needed to be added before it could be commited and pushed. Not a big deal, because I knew the .mpy file I eventually wanted to create already existed in my repo. I just created an empty test.txt alongside it and moved on with my life.
The workflow was successful after that and I saw my test.txt with an integer date-time stamp in it.
Linting with Ruff
I recently added the Ruff plug-in to my VS Code and I've been very happy wth it. So I decided a good next step would be trying do code linting in my workflow. It wasn't too difficult. The workflow is shown below.
Figure 3: Linting with Ruff
A couple things to notice here are:
The SRC_FILES variable makes it easier to configure which files are getting linted. I could have used *.py in the last line,
python3 -m ruff check
, but the flexibility of the variable will come in handy as more workflow steps are added.The write permissions are not needed, because I'm not creating or altering any files with this workflow. All I'm doing is checking the code and letting the workflow fail if problems are found.
Compiling Bytecode
Now that I know I can lint Python code, and I can generate a test.txt file and push it to my repository, the next step is to put everything together while generating an .mpy file instead of test.txt. Here's how:
Figure 4: Linting and compiling Bytecode
The big changes here are:
The command to compile bytecode is not really any different than what I had been doing manually before. Though in the workflow, I'm using the SRC_FILE variable instead of a hardcoded filename.
Checking for changes before committing is accomplished with the conditional
if [ $(git diff | wc -l) -gt 0 ];
This is needed because the workflow will fail when git complains there are no changed files. This error will happen whenever a file other than $SRC_FILE is pushed. The workflow will trigger because of the push action, but there's no change in the bytecode if the pushed file is not the python file indicated by SRC_FILE. No changes causes the error and
git diff
is used to list changes.The last bit is minor. Instead of YourName for the git config user.name, the built-in workflow variable GITHUB_REPOSITORY_OWNER will substitute the repository namespace instead of requiring a hardcoded value. It's not super important, but it makes things easier for other people to borrow the code without a lot of search and replace customizing.
Next Steps
My goal was a simple workflow. You can get fancier if you want. Some good references are:
Beta Was this translation helpful? Give feedback.
All reactions