Skip to content

Instantly share code, notes, and snippets.

@huynguyennovem
Last active May 16, 2023 17:18
Show Gist options
  • Save huynguyennovem/4c9a36e9cc04c5cace3c65d6a35e4444 to your computer and use it in GitHub Desktop.
Save huynguyennovem/4c9a36e9cc04c5cace3c65d6a35e4444 to your computer and use it in GitHub Desktop.
Flutter & Google App Engine build

Manual deploy

  1. Install gcloud CLI on your environment: https://cloud.google.com/sdk/docs/install
  2. Create a project on GCP and enable Cloud Build API (this service requires the payment setup on your account): https://console.cloud.google.com/apis/library/cloudbuild.googleapis.com
  3. Create an app engine by CLI
gcloud app create
  1. Create & build Flutter project Note: Use Web as target platform in this sample
flutter create hello
flutter build web
  1. Create app.yaml file in your Flutter project (root directory)
app.yaml
env: standard
runtime: nodejs14

handlers:
  - url: /
    static_files: build/web/index.html
    upload: build/web/index.html
    secure: always
    redirect_http_response_code: 301

  - url: /(.*)
    static_files: build/web/\1
    upload: build/web/(.*)
    secure: always
    redirect_http_response_code: 301
  1. Deploy it
gcloud app deploy

Check the output endpoint from the console, it will look like this: https://flutter-deploy-demo.as.r.appspot.com

Auto deploy

  1. Check Service account permissions and make sure App Engine Admin is enabled.
  2. Prepare source repository

There are two ways:

Remember to update app.yaml file. This file will be copied to build/web after flutter build command, that's reason why the path is pointing to index.html file and root dir. For eg:

app.yaml
env: standard
runtime: nodejs14

handlers:
  - url: /
    static_files: index.html
    upload: index.html
    secure: always
    redirect_http_response_code: 301

  - url: /(.*)
    static_files: \1
    upload: (.*)
    secure: always
    redirect_http_response_code: 301
  1. Build Cloud Build runner for Flutter There is no supported builder for Flutter: see cloud-builders. But cloud-builders-community does.
Dockerfile
FROM debian:stretch

#Update stretch repositories
RUN sed -i s/deb.debian.org/archive.debian.org/g /etc/apt/sources.list
RUN sed -i 's|security.debian.org|archive.debian.org/|g' /etc/apt/sources.list
RUN sed -i '/stretch-updates/d' /etc/apt/sources.list

# Install Dependencies.
RUN apt update -y
RUN apt install -y \
  git \
  wget \
  curl \
  unzip \
  lib32stdc++6 \
  libglu1-mesa \
  default-jdk-headless

# Install Flutter.
ENV FLUTTER_ROOT="/opt/flutter"
RUN git clone https://github.com/flutter/flutter "${FLUTTER_ROOT}"
ENV PATH="${FLUTTER_ROOT}/bin:${PATH}"

# Disable analytics and crash reporting on the builder.
RUN flutter config  --no-analytics

# Perform an artifact precache so that no extra assets need to be downloaded on demand.
RUN flutter precache

# Perform a doctor run.
RUN flutter doctor -v

# Switch to the correct channel
ARG channel=stable
RUN flutter channel $channel

# Perform a flutter upgrade
RUN flutter upgrade

ENTRYPOINT [ "flutter" ]

  • Update /flutter/cloudbuild.yaml (only need stable and master channels, save money)
cloudbuild.yaml
# In this directory, run the following command to build this builder.
# $ gcloud builds submit . --config=cloudbuild.yaml

steps:
- name: 'docker:stable'
  args: [
    'build', '.',
    '-t', 'gcr.io/$PROJECT_ID/flutter:master',
    '--build-arg', 'channel=master',
  ]

- name: 'docker:stable'
  args: [
    'build', '.',
    '-t', 'gcr.io/$PROJECT_ID/flutter:stable',
    '-t', 'gcr.io/$PROJECT_ID/flutter',
    '--build-arg', 'channel=stable',
  ]

- name: 'gcr.io/$PROJECT_ID/flutter'
  args: ['--help']

timeout: '1200s'

images: [
  'gcr.io/$PROJECT_ID/flutter:master',
  'gcr.io/$PROJECT_ID/flutter:stable',
  'gcr.io/$PROJECT_ID/flutter',
]
tags: ['cloud-builders-community']
  • Start building image on GCP
gcloud builds submit . --config=cloudbuild.yaml
  1. Configure build Flutter
  • Add another cloudbuild.yaml file in the Flutter project root dir:
cloudbuild.yaml
steps:
  - name: 'gcr.io/$PROJECT_ID/flutter:stable'
    args: ['build', 'web']
  - name: gcr.io/google.com/cloudsdktool/cloud-sdk
    args:
      - '-c'
      - cp app.yaml build/web && cd build/web && gcloud config set app/cloud_build_timeout 1600 && gcloud app deploy
    entrypoint: bash
timeout: 1600s

Commit and push that file to Cloud source repository

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment