Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add codemagic brick #155

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/check-codemagic-ci-label.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Check Codemagic CI Label

on:
pull_request:
types: [opened, labeled, unlabeled, synchronize]
paths:
- formigas_codemagic/**
- '!formigas_codemagic/**/*.md'
- '!formigas_codemagic/docs/**'
- .github/workflows/continuous-integration-formigas_codemagic.yaml
- .github/workflows/check-codemagic-ci-label.yaml

jobs:
check-label:
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps:
- name: Check for required label
uses: mheap/github-action-required-labels@v5
with:
mode: minimum
count: 1
labels: "codemagic-ci: success"
84 changes: 84 additions & 0 deletions .github/workflows/continuous-integration-formigas_codemagic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: formigas_codemagic brick CI

on:
pull_request:
paths:
- 'formigas_codemagic/**'
- '!formigas_codemagic/**/*.md'
- '!formigas_codemagic/docs/**'
- '.github/workflows/continuous-integration-formigas_codemagic.yaml'
- '.github/workflows/check-codemagic-ci-label.yaml'

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
create-pr-with-changes:
name: Create PR with changes in test repository
runs-on: ubuntu-latest

env:
branch_name: "codemagic-ci-changes-${{ github.event.pull_request.number }}"

steps:
- name: Checkout brick repository
uses: actions/checkout@v4
with:
path: formigas-flutter-bricks

- name: Create GitHub App token
uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ vars.FORMIGAS_VERSION_BUMPER_APP_ID }}
private-key: ${{ secrets.FORMIGAS_VERSION_BUMPER_PRIVATE_KEY }}
owner: formigas
repositories: codemagic-ci-brick-ci

- name: Checkout codemagic test repository
uses: actions/checkout@v4
with:
repository: formigas/codemagic-ci-brick-ci
token: ${{ steps.app-token.outputs.token }}
path: codemagic-ci-brick-ci

- name: Install Dart SDK
uses: dart-lang/setup-dart@v1
with:
sdk: stable

- name: Install Mason CLI
run: dart pub global activate mason_cli

- name: Initialize Mason
run: mason init

- name: Add formigas_codemagic with Mason
run: mason add formigas_codemagic --path ./formigas-flutter-bricks/formigas_codemagic

- name: Make formigas_codemagic
run: mason make formigas_codemagic --on-conflict overwrite
working-directory: codemagic-ci-brick-ci

- name: Create Pull Request
id: create-pr
uses: peter-evans/create-pull-request@v7
with:
branch: ${{ env.branch_name }}
delete-branch: true
commit-message: "Update codemagic CI workflow for codemagic CI brick PR ${{ github.event.pull_request.number }}"
title: "Automated PR for Codemagic CI brick changes"
body: "This PR is automatically generated by the Codemagic CI brick CI workflow. It will merge the changes made in the CI workflow into the main branch. Once the pipelines succeed, this PR will be automatically merged. See the [Codemagic CI brick PR](${{ github.event.pull_request.html_url }}) for more details."
base: main
path: codemagic-ci-brick-ci
labels: automated-pr
draft: false
token: ${{ steps.app-token.outputs.token }}

- name: Set auto-merge for Pull Request
if: steps.create-pr.outputs.pull-request-operation == 'created'
run: gh pr merge ${{ steps.create-pr.outputs.pull-request-number }} --merge --auto
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
working-directory: codemagic-ci-brick-ci
14 changes: 14 additions & 0 deletions .github/workflows/publish-formigas_codemagic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Publish formigas_codemagic on BrickHub

on:
push:
tags:
- "formigas_codemagic-*.*.*"

jobs:
publish:
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/mason_publish.yml@v1
with:
working_directory: ./formigas_codemagic
secrets:
mason_credentials: ${{ secrets.MASON_CREDENTIALS }}
18 changes: 18 additions & 0 deletions .github/workflows/release-formigas_codemagic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: 1 Release formigas_codemagic brick

on:
workflow_dispatch:
inputs:
version:
description: "Version to release (e.g., 1.0.0)"
required: true

jobs:
release-brick:
uses: ./.github/workflows/release-brick.yaml
with:
version: ${{ github.event.inputs.version }}
brick_name: formigas_codemagic
secrets:
app_id: ${{ vars.FORMIGAS_VERSION_BUMPER_APP_ID }}
app_private_key: ${{ secrets.FORMIGAS_VERSION_BUMPER_PRIVATE_KEY }}
3 changes: 3 additions & 0 deletions formigas_codemagic/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 0.1.0

- Initial release.
9 changes: 9 additions & 0 deletions formigas_codemagic/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Contributing
## Continuous Integration
1. The CI pipeline for this brick creates a pull request updating the `codemagic.yaml` file in the [codemagic-ci-brick-ci](https://github.com/formigas/codemagic-ci-brick-ci/) repository to validate the codemagic workflow.
2. Creating the pull request will trigger the workflow in the codemagic-ci-brick-ci repository.
3. When successful, the pull request in the codemagic-ci-brick-ci repository will be merged automatically.
4. The merge will trigger a workflow that adds the `codemagic-ci: success` label to the brick's pull request.
5. The label is used to indicate that the pull request has been validated and can be merged.

![codemagic-ci flow diagram](./docs/formigas_codemagic-brick-flow-diagram.svg)
21 changes: 21 additions & 0 deletions formigas_codemagic/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 formigas GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
58 changes: 58 additions & 0 deletions formigas_codemagic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# formigas Codemagic CI

A brick that provides a [Codemagic][codemagic_link] Continuous Integration workflow for [Flutter][flutter_link] projects as we use it at [formigas][formigas_link].

See [Details](#details) for more info about the pipeline and Troubleshooting.

[![License: MIT][license_badge]][license_link]
[![Powered by Mason][mason_badge]][mason_link]

## Usage 🛠️

See [BrickHub][brick_hub_usage_link] for usage instructions.

## Output 📦
The workflow file
```
codemagic.yaml
```
## Details
The Codemagic CI pipeline runs validation, build and tests.
The pipeline is run on every pull request and push to the repository.
The build stage will create a release build and runs on the `production` flavor.

The test stage will generate a coverage and test results report and upload them to the Codemagic artifacts page.

### Setup and Configuration

#### Setup Codemagic for Android builds
For Android builds, an upload key is required. Please refer to [Android code signing](https://docs.codemagic.io/flutter-code-signing/android-code-signing/) to create a keystore and make it available to the pipeline.

#### Setup Codemagic for iOS builds
No additional setup is required for iOS builds.

#### Configuration
The pipeline is defined in `codemagic.yaml`.

- Flutter version: Defined by the value of `flutter` in the environment section. The default is `stable`.
- Xcode version: Defined by the value of `xcode` in the environment section. The default is `latest`.

### Troubleshooting
##### Error when building Android app
```
Execution failed for task ':app:signDevelopmentReleaseBundle'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.FinalizeBundleTask$BundleToolRunnable
```

This error occurs when the `android_signing` environment variable is not set correctly. Please refer to the [Android code signing](https://docs.codemagic.io/flutter-code-signing/android-code-signing/) documentation to set it up.


[license_badge]: https://img.shields.io/badge/license-MIT-blue.svg
[license_link]: https://opensource.org/licenses/MIT
[formigas_link]: https://formigas.io
[mason_link]: https://github.com/felangel/mason
[mason_cli_link]: https://github.com/felangel/mason/tree/master/packages/mason_cli
[mason_badge]: https://img.shields.io/endpoint?url=https%3A%2F%2Ftinyurl.com%2Fmason-badge
[flutter_link]:https://flutter.dev/
[brick_hub_usage_link]:https://brickhub.dev/bricks/formigas_codemagic#usage
[codemagic_link]:https://codemagic.io/
104 changes: 104 additions & 0 deletions formigas_codemagic/__brick__/codemagic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
definitions:
environment: &environment
flutter: stable
xcode: latest
triggering: &triggering
events:
- pull_request
- push
cancel_previous_builds: true
cache: &cache
cache_paths:
- $HOME/.pub-cache
- $HOME/.gradle/caches
- $HOME/Library/Caches/CocoaPods
scripts:
- &prepare_flutter_script
name: Get Flutter dependencies
script: |
flutter clean
flutter pub get

workflows:
validate:
name: Validate
max_build_duration: 10
instance_type: linux_x2
environment:
<<: *environment
triggering:
<<: *triggering
cache:
<<: *cache
scripts:
- *prepare_flutter_script
- name: Format
script: dart format -o none --set-exit-if-changed lib/ test/
- name: Analyze
script: flutter analyze --fatal-infos --fatal-warnings

build-ios:
name: Build iOS
max_build_duration: 30
instance_type: mac_mini_m2
environment:
<<: *environment
triggering:
<<: *triggering
cache:
<<: *cache
scripts:
- *prepare_flutter_script
- name: Build iOS
script: flutter build ios --release --no-codesign --flavor production -t lib/main_production.dart

build-android:
name: Build Android
max_build_duration: 30
instance_type: linux_x2
environment:
<<: *environment
android_signing:
- codemagic_ci_testapp_android_keystore
triggering:
<<: *triggering
cache:
<<: *cache
scripts:
- *prepare_flutter_script
- name: Set up key.properties
script: |
cat >> "$CM_BUILD_DIR/android/key.properties" <<EOF
storePassword=$CM_KEYSTORE_PASSWORD
keyPassword=$CM_KEY_PASSWORD
keyAlias=$CM_KEY_ALIAS
storeFile=$CM_KEYSTORE_PATH
EOF
- name: Build Android
script: flutter build appbundle --release --flavor production -t lib/main_production.dart

test:
name: Test and generate coverage
max_build_duration: 30
instance_type: linux_x2
environment:
<<: *environment
triggering:
<<: *triggering
cache:
<<: *cache
scripts:
- *prepare_flutter_script
- name: Install lcov
script: |
sudo apt-get update
sudo apt-get -y install lcov
- name: Run tests
script: flutter test --coverage --file-reporter json:test_results.json
- name: Generate coverage report
script: |
lcov --remove coverage/lcov.info '**.g.dart' --remove coverage/lcov.info '**/*.freezed.dart' -o coverage/clean_lcov.info
genhtml coverage/clean_lcov.info -o coverage/html
artifacts:
- test_results.json
- coverage/
7 changes: 7 additions & 0 deletions formigas_codemagic/brick.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: formigas_codemagic
description: A brick that provides a Codemagic CI pipeline for Flutter projects.
repository: https://github.com/formigas/formigas-flutter-bricks.git
version: 0.1.0

environment:
mason: ">=0.1.0-dev.60 <0.1.0"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.