I made an action to automatically format python code

Why

What

├─.github
│  └─workflows
│          code-format.yml
│
└─python
    │  pyproject.toml
    │
    └─source
            a.py
            b.py

How

code-format.yml


name: python auto code format

on:
  push:
    branches: 
      - main
      - develop
    paths:
      - 'python/**'

  workflow_dispatch:

jobs:
  black:
    name: Check code style with Black
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./python
    steps:    
      - name: Checkout
        uses: actions/checkout@v2
      - name: Set up Python 3.6.8
        uses: actions/setup-python@v2
        with:
          python-version: 3.6.8
      - name: Install black
        run: pip install black
      - name: run code format
        run: find . -type f -name "*.py" | xargs black
      - name: git settings
        run: |
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
      - name: Extract branch name
        id: extract_branch
        run: echo "##[set-output name=BRANCH_NAME;]$(echo ${GITHUB_REF#refs/heads/})"
      - name: commit
        run: git commit -m "python modules changed auto format." -a || echo "No changes to commit."
      - name: push
        run: |
          echo target branch is ${{ steps.extract_branch.outputs.BRANCH_NAME }}
          git pull
          git push origin ${{ steps.extract_branch.outputs.BRANCH_NAME }}

  1. From the develop branch, create a suitable topic branch
  2. Edit the appropriate code (b.py this time) in the appropriate topic branch (break the format) 3. commit & push
  3. Generate pull request on github
  4. Appropriate topic branch → merge into develop
  5. Action runs
  6. b.py in the develop branch is pushed in code-formatted state

lastly This time, I assumed the topic branch → develop branch, but the same applies to develop → main. Also, in this case, actions will run even if you commit directly to the main or develop branch.

Recommended Posts

I made an action to automatically format python code
I want to write in Python! (1) Code format check
Automatically format Python code in Vim
I made Othello to teach Python3 to children (4)
I made Othello to teach Python3 to children (2)
I made Othello to teach Python3 to children (5)
I made Othello to teach Python3 to children (3)
I made Othello to teach Python3 to children (1)
I made a tool to automatically browse multiple sites with Selenium (Python)
I made a script in python to convert .md files to Scrapbox format
I want to format and check Python code to my liking on VS Code
Automatically format Python code into PEP8-compliant code with Emacs
I want to make C ++ code from Python code!
I made an Ansible-installer
I made a package to create an executable file from Hy source code
I made a Python module to translate comment outs
I made a code to convert illustration2vec to keras model
I made Othello to teach Python3 to children (6) Final episode
I tried to implement an artificial perceptron with python
I tried to automatically generate a password with Python3
I felt that I ported the Python code to C ++ 98.
I made a python library to do rolling rank
I made blackjack with python!
I made a python text
An introduction to Python Programming
I made wordcloud with Python.
Convert python 3.x code to python 2.x
I made a package to filter time series with python
I made an IoT device to naturally acquire positive thinking
I want to be able to run Python in VS Code
I made a method to automatically select and visualize an appropriate graph for pandas DataFrame
I made a library to easily read config files with Python
I made a Line-bot using Python!
I tried to touch Python (installation)
I tried to make it possible to automatically send an email just by double-clicking the [Python] icon
Python> I made a test code for my own external file
I made a fortune with Python.
I sent an SMS with Python
I wrote the code to write the code of Brainf * ck in python
I wanted to do something like an Elixir pipe in Python
I made an Angular starter kit
[Python] Simple Japanese ⇒ I tried to make an English translation tool
I want to debug with Python
An introduction to Python for non-engineers
I made a daemon with Python
I tried to automatically collect images of Kanna Hashimoto with Python! !!
An alternative to `pause` in Python
I tried to make an image similarity function with Python + OpenCV
[Python Tutorial] An Easy Introduction to Python
(Python) I made an app from Trello that periodically notifies slack of tasks that are about to expire.
I made a Docker container to use JUMAN ++, KNP, python (for pyKNP).
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
I wrote a code to convert quaternions to z-y-x Euler angles in Python
[Python] I made a decorator that doesn't seem to have any use.
I made a password generator to teach Python3 to children (bonus) * Completely remade
I made a web application in Python that converts Markdown to HTML
I made an AI to judge whether it is alcohol or not!
I was addicted to creating a Python venv environment with VS Code
[Python] I wrote a simple code that automatically generates AA (ASCII art)
Prepare an environment to touch grib2 format files with python (Docker edition)
I tried to discriminate a 6-digit number with a number discrimination application made with python