LoginSignup
3
4

More than 1 year has passed since last update.

GitHub ActionsでmasterにpushしたときにPHP-CS-Fixerを動かしてcommit&pushする

Last updated at Posted at 2019-12-23

GitHubのmasterにpushしたらPHP-CS-Fixer動けば楽だなぁと思い、GitHub Actionsで実装
やり方は色々ありそうだが、自分がやったのは

composer require --dev friendsofphp/php-cs-fixer

でcomposerでインストールされるようにする

.github/php-cs-fixer.yml
name: PHP-CS-Fixer

on:
  push:
    branches:
      - master

jobs:
  php-cs-fixer:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@master
    # ↓これはcomposer installの際にextensionが必要なときにのみ必要な部分
    - name: Installing PHP Extension
      uses: shivammathur/setup-php@master
      with:
        php-version: 7.4
        extension-csv: gd, xdebug
    - name: Install Dependencies
      run: composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist
    - name: Execute PHP-CS-Fixer
      run: vendor/bin/php-cs-fixer fix .
    - name: Commit files
      uses: stefanzweifel/git-auto-commit-action@v4
      with:
        commit_message: "PHP-CS-Fixerによる修正:${{github.sha}}"
    - name: GitHub Push
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}

ad-m/github-push-actionは得体のしれなさもあるけど、GitHubActionsの中で星がたくさんついてたから大丈夫でしょうという感じ
master以外にプッシュするとかもad-m/github-push-actionの説明を見るとできる。PRに対して行う場合などは変える必要がある。
https://github.com/ad-m/github-push-action
secrets.GITHUB_TOKENは勝手に入ります

PHP-CS-Fixerによる変更ファイルが無いと失敗するとか、色々課題があったので修正しています。
stefanzweifel/git-auto-commit-action を使うと変更がなくてもエラーにはならないはずです

追記

.gitignoreに

.gitignore
.php_cs.cache

を追加する

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4