Categories
Flutter

Configure Gitlab To Deploy Flutter App Via CodeMagic

In this post, we will learn to configure Gitlab to deploy Flutter app via CodeMagic API. We will create a Gitlab CI/CD configuration file that can trigger manual release from Gitlab pipeline.

Introduction

CodeMagic is the most popular CI/CD platform in the Flutter Developer’s community to automate app deployment. With CodeMagic, one can deploy apps to both Android and Apple app stores with just a few clicks.

Gitlab also has a powerful CI/CD support built-in. So how can we connect the Gitlab CI/CD pipeline with CodeMagic for one-click deployment of Flutter apps from Gitlab itself?

CodeMagic API

We can make use of the CodeMagic API to integrate Gitlab CI/CD with it.

First of all, get your CodeMagic API auth token from the User Settings of CodeMagic UI.

CodeMagic API Auth Token

You will also need the App Id and Workflow Id of your project in CodeMagic.

These values can be retrieved from the project settings url which looks like this:

https://codemagic.io/app/601faafd83016c431a4b12e8/workflow/601faafd83016c431a4b12e7/settings

Here, the part before workflow is the App Id and the part after is Workflow Id.

Next, setup Gitlab CI/CD pipeline to make CodeMagic build request.

Configure GitLab CI/CD For CodeMagic

GitLab CI/CD is configured by a file called .gitlab-ci.yml placed in your Flutter project repository’s root folder. There are a lot of things you could do with Gitlab CI/CD and so there are many configurational things here.

For the purpose of this post, we will focus only on integrating with CodeMagic for one manual release from Gitlab CI/CD.

stages:
  - release

release:staging:
  stage: release
  when: manual
  script:
    - curl -H "Content-Type:application/json" -H "x-auth-token:YOUR_CODEMAGIC_AUTH_TOKEY" https://api.codemagic.io/builds --data '{"appId":"YOUR_APP_ID", "workflowId":"YOUR_WORKFLOW_ID","branch":"staging"}'
  only:
    refs:
      - staging

This is a basic Gitlab CI/CD configuration which allows to trigger manual release by making an authorized request to CodeMagic API. Besides specifying the workflow to use in CodeMagic, it also specifies which branch to use.

Configure Environment Variables For CodeMagic

You can also pass other CodeMagic configurations via environment variables which could look like this:

- curl -H "Content-Type:application/json" -H "x-auth-token:YOUR_CODEMAGIC_AUTH_TOKEY" https://api.codemagic.io/builds --data '{"appId":"YOUR_APP_ID", "workflowId":"YOUR_WORKFLOW_ID","branch":"staging", "environment":{"variables":{"flavor":"STAGING"}}}'

Once you have the Gitlab CI/CD in place, you are ready to trigger CodeMagic builds from Gitlab pipeline!

Learn To Build Hangman Game In Flutter