LoginSignup
2
3

More than 3 years have passed since last update.

GitHub Actions 上で clang-format を動かして勝手にリファクタリングしてもらう

Last updated at Posted at 2019-10-30

2020/08/10 actions/checkout@v1 について書きます

目的

  1. ユーザが master に push する
  2. GitHub Actions が反応して、インスタンスが起動
  3. clang-format が動いて、リポジトリ上のコードがフォーマットされる
  4. GitHub Actions が master に push する

手段

install clang-format

環境は ubuntu-18.04 を選択したとします。

sudo apt install clang-format やるだけに見えるのですが、実際にこれを叩くと、
以下のようなエラーがでて失敗します。

 The following packages have unmet dependencies:
 clang-format : Depends: clang-format-6.0 (>= 6.0~) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

なので、add-apt-repository で repository を追加する必要があります。
repository は、https://apt.llvm.org/ から見つけることができます。

これで、clang-format がインストール出来るようになります。

clang-format

clang-format がインストールされていますので、普段どおりコマンドを叩くだけです。

git commit

素の状態ではgit等使いにくいので、 actions/checkout@v1 を使います。
使い方は、uses: actions/checkout@v1 するだけです。
コミット前に名前とメールアドレスを git config で設定することを忘れずに。

git push

market place に素晴らしいツールがあるので、これを使うだけです。

    - name: push
    - uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}

secrets.GITHUB_TOKEN とは、GitHub の機能を使うための鍵のようです。
Issue を立てたり、Pull Request を作ったりするために使います。

成果物

name: clang-format

on:
  push:
    branches:
    - master

jobs:
  build:
    runs-on: ubuntu-18.04
    steps:
    - uses: actions/checkout@v1
    - name: install tools
      run: |
        sudo add-apt-repository 'deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic main'
        sudo apt update
        sudo apt install clang-format
        git config --global user.email "octocat@example.com"
        git config --global user.name "octocat"
    - name: refactor
      run: |
        clang-format -i -style=file main.cpp
        git add .
        git commit -m 'refactor by Github Actions'
    - uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}

補足

actions/checkout@v2 について

上記の成果物は、actions/checkout@v1を使っていましたが、その後、v2が出たようです。

自分のケースでは、actions/checkout@v2 に token を渡すことで git push まで出来てしまいました…1

jobs:
  build:
    runs-on: ubuntu-18.04
    steps:
    - uses: actions/checkout@v2
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
    - name: install tools
      run: |
        echo "install"
        git config --global user.email "octocat@example.com"
        git config --global user.name "octocat"
    - name: refactor
      run: |
        echo "refactor"
    - name: push
      run: |
        git push origin

  1. actions/checkout@v2 の README.md の git push サンプルに token が書かれていないのが最大の罠だと思います。また、README.md によると、private repository の場合は、ユーザアカウント設定ページのDeveloper Settings から作成できる personal access token(PAT) を指定する必要があるらしいです。 

2
3
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
2
3