Linux Snap packages

How to build and release a snap package with codemagic.yaml

This guide will illustrate the basic steps necessary for building and publishing your app as a Snap package.

You can find a complete project showcasing these steps in our Sample projects repository.

Note: Snap is only available on Linux instances. Make sure to have instance_type: linux or instance_type: linux_x2 in your codemagic.yaml. See the build machine specification here.

Adding the app to Codemagic

The apps you have available on Codemagic are listed on the Applications page. Click Add application to add a new app.

  1. If you have more than one team configured in Codemagic, select the team you wish to add the app to.
  2. Connect the repository where the source code is hosted. Detailed instructions that cover some advanced options are available here.
  3. Select the repository from the list of available repositories. Select the appropriate project type.
  4. Click Finish: Add application

Creating codemagic.yaml

In order to use codemagic.yaml for build configuration on Codemagic, it has to be committed to your repository. The name of the file must be codemagic.yaml and it must be located in the root directory of the repository. Detailed explanation can be found here.

Tip You can find codemagic.yaml examples in Codemagic Sample Projects repository.

If you prefer to write your codemagic.yaml file from scratch, you can start with this minimal configuration.

        name: Codemagic Sample Workflow
        max_build_duration: 120
        instance_type: mac_mini_m1

Tip You can have more than one workflow in the same codemagic.yaml file. If you are building for both the Android and iOS, simply enter both workflows as:
        name: Android Sample Workflow
        # .......    
        # .......
        # .......  
        name: iOS Sample Workflow
        # ......

Scan for the codemagic.yaml file by selecting a branch to scan and clicking the Check for configuration file button at the top of the page. Note that you can have different configuration files in different branches.

Configure Snap

To set up Snap packaging, create a snapcraft.yaml file with the necessary configurations according to Snapcraft guide for Flutter or follow the general snapcraft.yaml guide.

Optionally, run the snapcraft snap command locally to ensure that everything is set up.

You should store the snapcraft.yaml file in the repository root. Another option is to store snapcraft.yaml in the .snap folder that is located in repository root.

Building snap packages

Include the snapcraft snap command in the scripts section of your codemagic.yaml file as in the example below. The output of this command is a .snap artifact that can later be used for publishing to the Snapcraft Snap Store.

    name: Snapcraft Build
    instance_type: linux
      - name: Create a snap
        script: | 
          snapcraft snap --output flutter-codemagic-example.snap
        - '**/*.snap'

Additionally, you may want to install the generated .snap package onto your machine for testing. The package will not be code signed unless you publish it to Snap Store, so you would need to use the --dangerous flag to install the package without code signing:

    snap install your-package.snap --dangerous
Note: In case you are packaging a Flutter application, be sure to set SNAPCRAFT_BUILD_ENVIRONMENT environment variable to host. It is required to avoid virtualization. Read more about virtualization options here. Additionally, Snapcraft manages all the dependencies according to snapcraft.yaml configuration. There is no need to include the Flutter version in codemagic.yaml.

Publishing to Snap Store

Snap packages can be published to the Snap Store.

  1. Generate your Snapcraft credentials file by running the following command locally and providing your Snapcraft account username and password:

    snapcraft export-login snapcraft-login-credentials

  2. Run the following command and carefully copy/paste the output:

    cat  snapcraft-login-credentials | base64

  3. Open your Codemagic app settings, and go to the Environment variables tab.

  4. Enter the desired Variable name, e.g. SNAPCRAFT_LOGIN_CREDENTIALS.

  5. Paste the base64 encoded credentials from Step 2. as Variable value.

  6. Enter the variable group name, e.g. snapcraft_credentials. Click the button to create the group.

  7. Make sure the Secure option is selected.

  8. Click the Add button to add the variable.

  9. Add the variable group to your codemagic.yaml file

          - snapcraft_credentials

  10. In the scripts section, add steps to base64 decode the credentials file, log in to Snapcraft via CLI, build the snap package and release it to the desired channel.

    - name: Authenticate with Snap Store
      script: | 
        echo $SNAPCRAFT_LOGIN_CREDENTIALS | base64 \
          --decode > /home/builder/snapcraft-login-credentials
        snapcraft login --with /home/builder/snapcraft-login-credentials
    - name: Create a Snap package
      script: | 
        snapcraft snap --output flutter-codemagic-example.snap
    - name: Publish to Snap Store
      script: | 
        snapcraft upload flutter-codemagic-example.snap --release stable