Why
When developing as a team, there is a big difference in code format
Some people do it, others don't
Different formatter
I don't want to commit code formatting only
I want to eliminate the hassle of developers and the dependence on the local environment.
I don't want to depend on the local environment for libraries such as black and autopep
I don't want to depend on editors such as vscode and emacs
I wanted to touch github actions
I thought it was just right for the first time
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 }}
Actions
Commit history of develop branch
It's a bit confusing, but below, you can see that the history of commits by github actions remains and it is well formatted.
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.