├── .dockerignore ├── .github ├── FUNDING.yml └── workflows │ ├── build-docker.yaml │ └── update_version.yml ├── Dockerfile ├── README.md ├── action.yml ├── entrypoint.sh └── examples └── .github └── workflows └── push.yml /.dockerignore: -------------------------------------------------------------------------------- 1 | README.md 2 | examples 3 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [OskarStark] 4 | -------------------------------------------------------------------------------- /.github/workflows/build-docker.yaml: -------------------------------------------------------------------------------- 1 | name: "Build and push docker image" 2 | 3 | on: 4 | push: 5 | tags: 6 | - '*' 7 | paths: 8 | - ".github/workflows/build-docker.yaml" 9 | - "**" 10 | 11 | env: 12 | IMAGE: "oskarstark/php-cs-fixer-ga" 13 | PLATFORMS: linux/arm/v7,linux/arm64/v8,linux/amd64 14 | 15 | jobs: 16 | build: 17 | name: 'Build and Push' 18 | runs-on: "ubuntu-latest" 19 | steps: 20 | - name: 'Checkout' 21 | uses: 'actions/checkout@v3' 22 | with: 23 | ref: '${{ github.head_ref }}' 24 | 25 | - name: 'Exposing Release Version' 26 | run: | 27 | echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV 28 | 29 | - name: Login to Dockerhub 30 | uses: docker/login-action@v2 31 | with: 32 | username: ${{ secrets.DOCKERHUB_USERNAME }} 33 | password: ${{ secrets.DOCKERHUB_TOKEN }} 34 | 35 | - name: Set up Docker Buildx 36 | id: buildx 37 | uses: docker/setup-buildx-action@v2.9.1 38 | 39 | - name: Set up QEMU 40 | id: qemu 41 | uses: docker/setup-qemu-action@v2 42 | 43 | - name: Build and push images 44 | uses: docker/build-push-action@v4 45 | with: 46 | push: true 47 | builder: ${{ steps.buildx.outputs.name }} 48 | tags: | 49 | ${{env.IMAGE}}:latest 50 | ${{env.IMAGE}}:${{env.RELEASE_VERSION}} 51 | file: Dockerfile 52 | context: . 53 | cache-from: type=gha 54 | cache-to: type=gha,mode=max 55 | platforms: ${{ env.PLATFORMS }} 56 | -------------------------------------------------------------------------------- /.github/workflows/update_version.yml: -------------------------------------------------------------------------------- 1 | name: Update version 2 | 3 | on: 4 | workflow_dispatch: 5 | schedule: 6 | - cron: '30 */4 * * *' 7 | 8 | jobs: 9 | update-version: 10 | name: Automatically get latest PHP-CS-Fixer version and commit 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - name: "Checkout code" 16 | uses: actions/checkout@v3 17 | with: 18 | ref: master 19 | fetch-depth: '0' 20 | 21 | - name: "Update Dockerfile and action.yml" 22 | id: fetch_version 23 | run: | 24 | latest=$(curl -s https://packagist.org/packages/friendsofphp/php-cs-fixer.json|jq '[.package.versions[]|select(.version|test("^v\\d+\\.\\d+\\.\\d+$"))|.version]|max_by(.|[splits("[.]")]|map(sub("v";"")|tonumber))|sub("v";"")') 25 | latest=$(echo $latest | tr -d '"') 26 | echo "Latest PHP-CS-Fixer version is $latest" 27 | echo ::set-output name=latest::$latest 28 | sed -i -re "s/ENV VERSION=.*/ENV VERSION=$latest/" Dockerfile 29 | cat Dockerfile 30 | sed -i -re "s/php-cs-fixer-ga:([0-9.]+)?/php-cs-fixer-ga:$latest/" action.yml 31 | cat action.yml 32 | 33 | - name: "Commit changes" 34 | uses: stefanzweifel/git-auto-commit-action@v4 35 | id: commit 36 | with: 37 | commit_author: "Oskar Stark " 38 | commit_message: "Enhancement: Upgrade to PHP-CS-Fixer ${{ steps.fetch_version.outputs.latest }}" 39 | commit_user_email: "oskarstark@googlemail.com" 40 | commit_user_name: "Oskar Stark" 41 | 42 | - name: "Tag version ${{ steps.fetch_version.outputs.latest }}" 43 | uses: "anothrNick/github-tag-action@1.30.0" 44 | if: steps.commit.outputs.changes_detected == 'true' 45 | id: tag 46 | env: 47 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 48 | CUSTOM_TAG: ${{ steps.fetch_version.outputs.latest }} 49 | RELEASE_BRANCHES: master 50 | 51 | - name: "Create release ${{ steps.fetch_version.outputs.latest }}" 52 | uses: actions/create-release@v1 53 | if: steps.commit.outputs.changes_detected == 'true' 54 | env: 55 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 56 | with: 57 | tag_name: ${{ steps.tag.outputs.new_tag }} 58 | release_name: ${{ steps.tag.outputs.new_tag }} 59 | commitish: master 60 | body: "Upgrade PHP-CS-Fixer to ${{ steps.tag.outputs.new_tag }}" 61 | 62 | publish_docker_images: 63 | needs: [update-version] 64 | runs-on: ubuntu-20.04 65 | 66 | if: github.ref == 'refs/heads/master' || github.event_name == 'release' 67 | steps: 68 | - name: Checkout 69 | uses: actions/checkout@v3 70 | - name: Docker meta 71 | id: meta 72 | uses: crazy-max/ghaction-docker-meta@v2 73 | with: 74 | images: oskarstark/php-cs-fixer-ga 75 | tags: | 76 | type=raw,value=latest,enable=${{ endsWith(github.ref, 'master') }} 77 | type=ref,event=tag 78 | flavor: | 79 | latest=false 80 | - name: Login to DockerHub 81 | if: github.event_name != 'pull_request' 82 | uses: docker/login-action@v1 83 | with: 84 | username: ${{ secrets.DOCKERHUB_USERNAME }} 85 | password: ${{ secrets.DOCKERHUB_TOKEN }} 86 | - name: Build and push 87 | uses: docker/build-push-action@v2 88 | with: 89 | context: . 90 | push: ${{ github.event_name != 'pull_request' }} 91 | tags: ${{ steps.meta.outputs.tags }} 92 | labels: ${{ steps.meta.outputs.labels }} 93 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:8.3-alpine 2 | 3 | LABEL "com.github.actions.name"="OSKAR-PHP-CS-Fixer" 4 | LABEL "com.github.actions.description"="check php files" 5 | LABEL "com.github.actions.icon"="check" 6 | LABEL "com.github.actions.color"="blue" 7 | 8 | LABEL "repository"="http://github.com/oskarstark/php-cs-fixer-ga" 9 | LABEL "homepage"="http://github.com/actions" 10 | LABEL "maintainer"="Oskar Stark " 11 | 12 | ENV VERSION=3.75.0 13 | 14 | RUN wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v$VERSION/php-cs-fixer.phar -O php-cs-fixer \ 15 | && chmod a+x php-cs-fixer \ 16 | && mv php-cs-fixer /usr/local/bin/php-cs-fixer 17 | 18 | ADD entrypoint.sh /entrypoint.sh 19 | ENTRYPOINT ["/entrypoint.sh"] 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GitHub Action for PHP-CS-Fixer 2 | 3 | > NOTE: If you didn't create a `.php-cs-fixer.dist.php` file, do that first before adding this workflow. An example of this file created by the creators of PHP-CS-Fixer can be found [here](https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/.php-cs-fixer.dist.php). 4 | 5 | ## Usage 6 | 7 | You can use it as a GitHub Action like this: 8 | 9 | ```yaml 10 | # .github/workflows/lint.yml 11 | 12 | on: [push, pull_request] 13 | name: Main 14 | jobs: 15 | php-cs-fixer: 16 | name: PHP-CS-Fixer 17 | runs-on: ubuntu-latest 18 | steps: 19 | - uses: actions/checkout@v3 20 | - name: PHP-CS-Fixer 21 | uses: docker://oskarstark/php-cs-fixer-ga 22 | ``` 23 | 24 | _To use a custom config, e.g. `--diff` and `--dry-run` options:_ 25 | 26 | ```diff 27 | # .github/workflows/lint.yml 28 | 29 | on: [push, pull_request] 30 | name: Main 31 | jobs: 32 | php-cs-fixer: 33 | name: PHP-CS-Fixer 34 | runs-on: ubuntu-latest 35 | steps: 36 | - uses: actions/checkout@v3 37 | - name: PHP-CS-Fixer 38 | uses: docker://oskarstark/php-cs-fixer-ga 39 | + with: 40 | + args: --config=.project.php_cs --diff --dry-run 41 | ``` 42 | 43 | ## Usage with cache 44 | 45 | Add `.php-cs-fixer.cache` to `.gitignore`, then: 46 | 47 | ``` 48 | on: [push, pull_request] 49 | name: Main 50 | jobs: 51 | php-cs-fixer: 52 | name: PHP-CS-Fixer 53 | runs-on: ubuntu-latest 54 | steps: 55 | - uses: actions/checkout@v3 56 | - uses: actions/cache@v3 57 | with: 58 | path: .php-cs-fixer.cache 59 | key: ${{ runner.OS }}-${{ github.repository }}-phpcsfixer-${{ github.sha }} 60 | restore-keys: | 61 | ${{ runner.OS }}-${{ github.repository }}-phpcsfixer- 62 | 63 | - name: PHP-CS-Fixer 64 | uses: docker://oskarstark/php-cs-fixer-ga 65 | ``` 66 | 67 | **You can copy/paste the `.github/` folder (under `examples/`) to your project and that's all!** 68 | 69 | ## Usage with only changed files 70 | 71 | It is also possible to run PHP-CS-Fixer just on your changed files. To achieve this, you can use the [`tj-actions/changed-files`](https://github.com/tj-actions/changed-files) action to retrieve the changed files and subsequently use the result to create extra arguments with `---path-mode=intersection`. 72 | 73 | ``` 74 | on: [push, pull_request] 75 | name: Main 76 | jobs: 77 | php-cs-fixer: 78 | name: PHP-CS-Fixer 79 | runs-on: ubuntu-latest 80 | steps: 81 | - uses: actions/checkout@v3 82 | 83 | - name: Get changed files 84 | id: changed-files 85 | uses: tj-actions/changed-files@v38 86 | 87 | - name: Get extra arguments for PHP-CS-Fixer 88 | id: phpcs-intersection 89 | run: | 90 | CHANGED_FILES=$(echo "${{ steps.changed-files.outputs.all_changed_files }}" | tr ' ' '\n') 91 | if ! echo "${CHANGED_FILES}" | grep -qE "^(\\.php-cs-fixer(\\.dist)?\\.php|composer\\.lock)$"; then EXTRA_ARGS=$(printf -- '--path-mode=intersection\n--\n%s' "${CHANGED_FILES}"); else EXTRA_ARGS=''; fi 92 | echo "PHPCS_EXTRA_ARGS<> $GITHUB_ENV 93 | echo "$EXTRA_ARGS" >> $GITHUB_ENV 94 | echo "EOF" >> $GITHUB_ENV 95 | 96 | - name: PHP-CS-Fixer 97 | uses: docker://oskarstark/php-cs-fixer-ga 98 | with: 99 | args: --config=.php-cs-fixer.dist.php -v --dry-run --stop-on-violation --using-cache=no ${{ env.PHPCS_EXTRA_ARGS }}" 100 | ``` 101 | 102 | ## Docker 103 | 104 | A Docker image is built automatically and located here: 105 | https://hub.docker.com/r/oskarstark/php-cs-fixer-ga 106 | 107 | You can run it in any given directory like this: 108 | 109 | ```bash 110 | docker run --rm -it -w=/app -v ${PWD}:/app oskarstark/php-cs-fixer-ga:latest 111 | ``` 112 | 113 | ## A picture is worth a thousand words 114 | 115 | You can find a working and not working PR here: 116 | https://github.com/OskarStark/test-php-cs-fixer-ga/pulls 117 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | # https://help.github.com/en/articles/metadata-syntax-for-github-actions 2 | 3 | author: 'OskarStark' 4 | 5 | branding: 6 | icon: 'check' 7 | color: 'blue' 8 | 9 | description: 'Use PHP-CS-Fixer via GithubAction.' 10 | 11 | name: 'OSKAR-PHP-CS-Fixer' 12 | 13 | runs: 14 | using: 'docker' 15 | image: 'docker://oskarstark/php-cs-fixer-ga:3.75.0' 16 | -------------------------------------------------------------------------------- /entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -l 2 | 3 | set -e 4 | 5 | /usr/local/bin/php-cs-fixer --version 6 | /usr/local/bin/php-cs-fixer fix $* 7 | -------------------------------------------------------------------------------- /examples/.github/workflows/push.yml: -------------------------------------------------------------------------------- 1 | on: [push, pull_request] 2 | name: Lint 3 | jobs: 4 | php-cs-fixer: 5 | name: PHP-CS-Fixer 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@v4 9 | - name: PHP-CS-Fixer 10 | uses: docker://oskarstark/php-cs-fixer-ga 11 | with: 12 | args: --diff --dry-run 13 | --------------------------------------------------------------------------------