Rune Laenen

Shopware plugin manufacturer & freelancer

Use Github Actions to compile & release SW6 plugins

23/10/2020

Shopware offers the possibility to upload .zip files in the backend to install plugins. As an open source developer, the main way to distribute my plugins was to offer a composer command. However, not all Shopware installations (or webmasters) have the possibility to easily add extra dependencies through composer.

To not have the hassle of creating the zip files manually, it’s possible to use Github Actions to automatically create it whenever you tag a new version.

The action uses a docker container from FriendsOfShopware to compile the needed files and zip it. The action then also creates a Github Release from the tag, and uploads the zip file as Release Asset.

Feel free to use the code below. Make sure to change the plugin name SwagTestPlugin to your plugin name everywhere.

What is Github Actions?

GitHub Actions is a CI/CD tool, built in into GitHub. And the best thing is: it’s free! It can be used without a limit for open source repositories! That makes it the ideal tool to run long(er) running actions - like setting up a Shopware installation & building a plugin.

Composer troubles

At the moment, the plugins need to have 2 composer files: a normal composer.json file, and a composer-ci.json file which does not have the shopware/* dependencies. If we try to build with the normal composer.json file, it will install a full shopware & Symfony application in the vendor folder. This makes the .zip grow from ~400kb to ~38mb.

If you know any better way to fix this issue without having a double file. Please help the community, and let us know 💙

File: .github/workflows/create-zip.yml

name: Create zip
on:
    push:
        tags:
            - '*'

jobs:
    CreateZip:
        runs-on: ubuntu-latest
        container: ghcr.io/friendsofshopware/platform-plugin-dev:v6.3.1
        steps:
            -   name: Get the version
                id: get_version
                run: echo ::set-output name=VERSION::${GITHUB_REF#refs/tags/}

            -   name: Checkout
                uses: actions/checkout@v2.3.1
                with:
                    path: SwagTestPlugin

            -   name: Build & create zip
                run: |
                    cp -r "./SwagTestPlugin" "/plugins/SwagTestPlugin"
                    COMPOSER="composer-ci.json" composer install -d "/plugins/SwagTestPlugin" --no-ansi --no-dev --no-interaction --no-plugins --no-progress --no-scripts --no-suggest --optimize-autoloader --ignore-platform-reqs
                    start-mysql
                    pack-plugin "SwagTestPlugin"

            -   name: Create Release
                if: startsWith(github.ref, 'refs/tags/')
                id: create_release
                uses: actions/create-release@v1.0.0
                env:
                    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
                with:
                    tag_name: ${{ steps.get_version.outputs.VERSION }}
                    release_name: ${{ steps.get_version.outputs.VERSION }}
                    draft: false
                    prerelease: false

            -   name: Upload Release Asset
                if: startsWith(github.ref, 'refs/tags/')
                id: upload_release_asset
                uses: actions/upload-release-asset@v1.0.2
                env:
                    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
                with:
                    upload_url: ${{ steps.create_release.outputs.upload_url }}
                    asset_path: SwagTestPlugin.zip
                    asset_name: SwagTestPlugin.zip
                    asset_content_type: application/zip

Are you using and liking this post? Consider sponsoring me or buying me a coffee so I can keep justifying putting a lot of time in these free open source plugins & blog-posts.