DEV Community

Cover image for Guide to Using Any Programming Language in GitHub Actions
CHAHBOUN Mohammed
CHAHBOUN Mohammed

Posted on • Edited on

2

Guide to Using Any Programming Language in GitHub Actions

Cover: Captured from flutter CI/CD Template (not published
yet)

Many times we need to process some data before using it in our Github workflows, but we don't find the appropriate action.
For that, I did some research on how we can use any programming language to process our data easily & use it on Github workflow
I found this example on Github Doc :

- name: Set color
id: random-color-generator
run: echo "SELECTED_COLOR=green" >> $GITHUB_OUTPUT
- name: Get color
run: echo "The selected color is ${{ steps.random-color-generator.outputs.SELECTED_COLOR }}"

As you see we can save some output on GITHUB_OUTPUT & use it later in
GITHUB_OUTPUT. It's the path of a temporary file generated by the runner.
Now we will use some programming language (Dart for an example) to process some data & use it on our Github workflow
We will use an example for Bump Flutter app version using this Fastlane plugin based on PR's labels
Now this is the dart script :
import 'dart:io';
const List<String> options = ["major", "minor", "patch"];
Future<void> main(List<String> labels) async {
List<String> versionParts = List.from(labels);
versionParts.removeWhere((e) => !options.contains(e));
String parts = "";
if (versionParts.isNotEmpty) {
parts = versionParts.join(",");
parts = "parts=bump:$parts";
}
await setOutput(parts);
}
Future<void> setOutput(String item) async {
Map<String, String> envVars = Platform.environment;
String? envPath = envVars["GITHUB_OUTPUT"];
File ghEnv = File(envPath!);
String currentContent = await ghEnv.readAsString();
if (currentContent.isNotEmpty) {
currentContent += "\n" "$item";
} else {
currentContent = item;
}
ghEnv.writeAsString(currentContent);
}

We process input data by removing all labels except major, minor & patch, and split it by (,)
Then in line 16 we get environment variables & select GITHUB_OUTPUT in line 17 to get the path of the temporary file generated by the runner
After it, we open the file by Dart & write our output ({name}={value}) that we need in Github workflow
Now we will create our Github workflow & use the Dart script onto it
name: Bump app version based on PR labels
# Workflow for every new feature/enhance/fix
on:
pull_request:
branches:
- dev
types:
- closed
jobs:
setup_dart_and_run_converter:
if: ${{ (github.event.pull_request.merged == true) }}
runs-on: ubuntu-latest
outputs:
parts: ${{ steps.labels_to_version_parts.outputs.parts }}
steps:
- name: Checkout Repo
uses: actions/checkout@v3
- uses: dart-lang/setup-dart@v1.3
- name: Convert labels to version parts
id: labels_to_version_parts
run: "dart workflows_utils/labels_to_version_parts.dart {{ join(github.event.pull_request.labels.*.name,' ') }}"
bump_version_based_on_labels:
if: ${{ (github.event.pull_request.merged == true) }}
runs-on: ubuntu-latest
needs: setup_dart_and_run_converter
steps:
- name: Checkout Repo
uses: actions/checkout@v3
# Setup Ruby, Bundler, Gemfile
- name: Setup Fastlane
uses: ruby/setup-ruby@72d59482210349c1114eca714b6c5df19fbbec34
with:
ruby-version: "2.6"
bundler-cache: true
working-directory: android
- run: bundle exec fastlane bump_version push:true branch:${{ github.ref_name }} ${{needs.setup_dart_and_run_converter.outputs.parts}} bump_build:false
working-directory: android

First, we need to use a setup for Dart lang action (line 18) (or any language used).
Then on line 21 we run the Dart script and pass PR labels as args & this script will return just the labels we need for the bump version (patch,minor, major) & in line 37 we use (env.parts) as the output of the script to use it as an input for the bump version by Fastlane plugin
Now when merging PR. The workflow will get labels & remove unused labels by Dart script & pass the output to Fastlane plugin to bump the version & push a new commit
You can use these steps on any programming language

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay