# Flutter CI/CD Github Actions

If you already use github for version control, then you can always go a notch higher and integrate a Continous Integration and Continous Deployment(CI & CD) pipeline for your mobile apps. This can be achieved by using the **github actions** feature that github offers for free. 

**Pre-requisites**

Have github as the version control for your app`s codebase.

Before we kick off lets look at some definitions first:

**Continous Integration ( CI )**

It is an automation process for developers where new code changes to an app are regularly built, tested, and merged to a shared repository. It’s a solution to the problem of having too many branches of an app in development at once that might conflict with each other.

**Continous Delivery/Deployment ( CD )** 

It automates the release of the validated code to a repository. It makes sure the codebase is always ready for deployment to a production environment at minimal effort.

**Setting up Github Actions**

To use github actions, you need to create this folder **.github/workflows** in your projects root folder.
Then create a **file_name.yml** file which will contain the commands to run our CI/CD pipeline.


![gith.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1652029710575/uD9gCKLKz.png align="left")

The created file will contain the following contents:

```
name: Android CI

# This workflow is triggered on pushes to the repository.

on: [push, pull_request]

jobs:
  build:
    # This job will run on windows virtual machine.
    #others include ubuntu-latest and macos-latest
    runs-on: windows-latest

    steps:
      # The branch or tag ref that triggered the workflow will be checked out.
        # https://github.com/actions/checkout
        - uses: actions/checkout@v1
        # Setup the flutter environment.
        - uses: subosito/flutter-action@v1
          with:
            channel: 'stable' # 'dev', 'alpha', default to: 'stable'
            # flutter-version: '1.12.x' # you can also specify exact version of flutter
        
        #set up java environment
        - uses: actions/setup-java@v2
          with:
            distribution: 'zulu' # Mandatory
            java-version: '11' # Mandatory

        - run: flutter doctor -v
        - run: flutter clean
        - run: flutter pub get
        - run: flutter format .
        - run: flutter analyze
        - run: flutter test
        - run: flutter build apk --release
        - uses: ncipollo/release-action@v1
          with:
            artifacts: "build/app/outputs/flutter-apk/*.apk"
            allowUpdates: true
            tag: release
            token: $


``` 
> [actions/checkout@v1](https://github.com/actions/checkout)

This action checks-out your repository , so your workflow can access it and work with the code base.

> [subosito/flutter-action@v1](https://github.com/subosito/flutter-action)

Sets up the flutter environment for use in GitHub Actions.

> [actions/setup-java@v2](https://github.com/actions/setup-java)

Downloads and sets up the requested version of Java. The inputs **java-version** and **distribution** are mandatory.

> [ncipollo/release-action@v1](https://github.com/ncipollo/release-action)

This action will create a GitHub release and upload an artifact to it.
You **must provide** a tag or the **action will fail**. 

You can create multiple **.yml** files in the folder to cater for different scenarios and environment. In our case we can add an IOS platform deployment file.

![gith.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1652030375579/G7x2gzU-8.png align="left")

**Build for IOS**

```
name: IOS CI

# This workflow is triggered on pushes to the repository.

on: [push, pull_request]

jobs:
  build:
    runs-on: macos-latest

    steps:
      # The branch or tag ref that triggered the workflow will be checked out.
        # https://github.com/actions/checkout
        - uses: actions/checkout@v1
        # Setup the flutter environment.
        - uses: subosito/flutter-action@v1
          with:
            channel: 'stable' # 'dev', 'alpha', default to: 'stable'
            architecture: x64

        - run: flutter doctor -v
        - run: flutter clean
        - run: flutter pub get
        - run: flutter pub run build_runner build --delete-conflicting-outputs
        - run: flutter format .
        - run: flutter analyze
        - run: flutter test
``` 
**Build for Windows**

```
name: Windows CI

# This workflow is triggered on pushes to the repository.

on: [push, pull_request]

jobs:
  build:
    # This job will run on windows virtual machine
    runs-on: windows-latest

    steps:
      # The branch or tag ref that triggered the workflow will be checked out.
        # https://github.com/actions/checkout
        - uses: actions/checkout@v1
        # Setup the flutter environment.
        - uses: subosito/flutter-action@v1
          with:
            channel: 'stable' # 'dev', 'alpha', default to: 'stable'

        - run: flutter doctor -v
        - run: flutter config --enable-windows-desktop
        - run: flutter clean
        - run: flutter pub get
        - run: flutter pub run build_runner build --delete-conflicting-outputs
        - run: flutter format .
        - run: flutter analyze
        - run: flutter test
        - run: flutter build windows --release

``` 
With that you have a fully fledged seamless Continous Integration and Continous Deployment environment for your flutter project.

Bye Bye Folks..👋👋👋
Happy Coding😁😀

