Skip to content

Commit 55d4475

Browse files
committed
chore: support to publish rc version to pub
1 parent b7b8398 commit 55d4475

File tree

5 files changed

+283
-2
lines changed

5 files changed

+283
-2
lines changed

.github/actions/pub/Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM dart:latest
2+
3+
RUN pwd
4+
5+
# RUN echo "Files in current directory:" && ls -la .
6+
7+
COPY . /
8+
9+
RUN chmod +x /entrypoint.sh
10+
11+
# RUN echo "Files after COPY:" && ls -la /
12+
13+
ENTRYPOINT ["/entrypoint.sh"]

.github/actions/pub/action.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: 'Dart and Flutter Package Publisher'
2+
description: 'Continuously Test & Publish Dart and Flutter Package To Pub.dev When Version Changed'
3+
author: 'k-paxian'
4+
branding:
5+
color: 'blue'
6+
icon: 'package'
7+
inputs:
8+
accessToken:
9+
description: '(Required) Token from ~/.pub-cache/credentials.json. Use secrets.OAUTH_ACCESS_TOKEN'
10+
required: true
11+
refreshToken:
12+
description: '(Required) Token from ~/.pub-cache/credentials.json. Use secrets.OAUTH_REFRESH_TOKEN'
13+
required: true
14+
credentialJson:
15+
description: '(Optional) Overrides accessToken and refreshToken. Whole content of ~/.pub-cache/credentials.json. Use secrets.CREDENTIAL_JSON'
16+
required: false
17+
relativePath:
18+
description: '(Optional) Path to your package root in your repository'
19+
required: false
20+
default: ''
21+
dryRunOnly:
22+
description: '(Optional) Perform dry run only, no real publishing'
23+
required: false
24+
default: false
25+
testRunOnly:
26+
description: '(Optional) Perform unit tests run only, no real publishing'
27+
required: false
28+
default: false
29+
skipTests:
30+
description: '(Optional) Skip unit tests run'
31+
required: false
32+
default: false
33+
suppressBuildRunner:
34+
description: '(Optional) Suppress using `build_runner` for unit tests run'
35+
required: false
36+
default: false
37+
format:
38+
description: '(Optional) Format code of project to get better score in pub.dev'
39+
required: false
40+
default: false
41+
force:
42+
description: '(Optional) Force publishing even if pub tool throws warnings, hints, etc'
43+
required: false
44+
default: false
45+
flutter:
46+
description: '(Optional) Flutter package type'
47+
required: false
48+
default: false
49+
flutterBranch:
50+
description: '(Optional) Flutter branch to use, stable, master, main, dev, etc.'
51+
required: false
52+
default: 'stable'
53+
outputs:
54+
success:
55+
description: 'Result, "true" if actual publishing happened, "false" otherwise'
56+
package:
57+
description: 'Package name from pubspec'
58+
localVersion:
59+
description: 'Package local version from pubspec'
60+
remoteVersion:
61+
description: 'Package remote version from pub.dev'
62+
dartVersion:
63+
description: 'Dart SDK version which is being used to run tests & publish'
64+
flutterVersion:
65+
description: 'Flutter SDK version which is being used to run tests & publish'
66+
runs:
67+
using: 'docker'
68+
image: 'Dockerfile'

.github/actions/pub/entrypoint.sh

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
export PATH="$PATH":"$HOME/.pub-cache/bin"
6+
7+
trace() {
8+
local message=$1
9+
local summary=$2
10+
11+
if [ -z "$message" ]; then
12+
:
13+
else
14+
echo "$message"
15+
fi
16+
17+
if [ -z "$summary" ]; then
18+
:
19+
else
20+
echo "$summary" >> $GITHUB_STEP_SUMMARY
21+
fi
22+
}
23+
24+
check_required_inputs() {
25+
trace "🔑 Check credentials..."
26+
if [ -z "$INPUT_CREDENTIALJSON" ]; then
27+
trace "Missing credentialJson, using tokens"
28+
if [ -z "$INPUT_ACCESSTOKEN" ]; then
29+
trace "❌ Missing accessToken"
30+
exit 1
31+
fi
32+
if [ -z "$INPUT_REFRESHTOKEN" ]; then
33+
trace "❌ Missing refreshToken"
34+
exit 1
35+
fi
36+
fi
37+
}
38+
39+
switch_working_directory() {
40+
if [ -z "$INPUT_RELATIVEPATH" ]; then
41+
:
42+
else
43+
trace "Switching to package directory '$INPUT_RELATIVEPATH'"
44+
cd "$INPUT_RELATIVEPATH"
45+
fi
46+
trace "Package dir: $PWD"
47+
}
48+
49+
detect_flutter_package() {
50+
GET_OUTPUT=`dart pub get`
51+
if [ "$?" = 69 ] || [ "$GET_OUTPUT" = "Resolving dependencies..." ] || [ "$INPUT_FLUTTER" = "true" ]; then
52+
INPUT_FLUTTER="true"
53+
export PATH="$PATH":"/flutter/bin"
54+
trace "Flutter package detected. Installing Flutter from $INPUT_FLUTTERBRANCH branch..."
55+
git clone -b $INPUT_FLUTTERBRANCH https://github.com/flutter/flutter.git /flutter
56+
flutter doctor
57+
fi
58+
}
59+
60+
get_local_package_version() {
61+
if [ "$INPUT_FLUTTER" = "true" ]; then
62+
GET_OUTPUT=`flutter pub get`
63+
DEPS_OUTPUT=`flutter pub deps`
64+
else
65+
GET_OUTPUT=`dart pub get`
66+
DEPS_OUTPUT=`dart pub deps`
67+
fi
68+
PACKAGE_INFO=`echo "$DEPS_OUTPUT" | perl -0777 -pe 's/(└|│|(?<![\d])-)(?!rc\.1).+//s' | head -n 3`
69+
trace "$PACKAGE_INFO"
70+
DART_VERSION=`echo "$PACKAGE_INFO" | perl -n -e'/^Dart SDK (.*)$/ && print $1'`
71+
FLUTTER_VERSION=`echo "$PACKAGE_INFO" | perl -n -e'/^Flutter SDK (.*)$/ && print $1'`
72+
PACKAGE_INFO=`echo "$PACKAGE_INFO" | tail -1`
73+
PACKAGE=`echo "$PACKAGE_INFO" | cut -d' ' -f1`
74+
LOCAL_PACKAGE_VERSION=`echo "$PACKAGE_INFO" | cut -d' ' -f2`
75+
if [ -z "$PACKAGE" ]; then
76+
trace "::error::No package found. :(" "❌ No package found. :("
77+
exit 1
78+
fi
79+
echo "dartVersion=$DART_VERSION" >> $GITHUB_OUTPUT
80+
if [ "$FLUTTER_VERSION" != "" ]; then
81+
echo "flutterVersion=$FLUTTER_VERSION" >> $GITHUB_OUTPUT
82+
fi
83+
echo "package=$PACKAGE" >> $GITHUB_OUTPUT
84+
echo "localVersion=$LOCAL_PACKAGE_VERSION" >> $GITHUB_OUTPUT
85+
}
86+
87+
run_unit_tests() {
88+
if [ "$INPUT_SKIPTESTS" = "true" ]; then
89+
trace "::notice::Skip unit tests set to true, skip unit testing."
90+
else
91+
HAS_BUILD_RUNNER=`echo "$DEPS_OUTPUT" | perl -n -e'/^.* build_runner (.*)/ && print $1'`
92+
HAS_BUILD_TEST=`echo "$DEPS_OUTPUT" | perl -n -e'/^.* build_test (.*)/ && print $1'`
93+
HAS_TEST=`echo "$DEPS_OUTPUT" | perl -n -e'/^.* (test|flutter_test) (.*)/ && print $2'`
94+
if [ "$HAS_BUILD_RUNNER" != "" ] && [ "$HAS_BUILD_TEST" != "" ] && [ "$INPUT_SUPPRESSBUILDRUNNER" != "true" ]; then
95+
if [ "$INPUT_FLUTTER" = "true" ]; then
96+
trace "::notice::flutter tests with build_runner"
97+
flutter pub run build_runner build --delete-conflicting-outputs
98+
flutter test
99+
else
100+
dart pub run build_runner test --delete-conflicting-outputs
101+
fi
102+
else
103+
if [ "$HAS_TEST" != "" ]; then
104+
if [ "$INPUT_FLUTTER" = "true" ]; then
105+
flutter test
106+
else
107+
dart pub run test
108+
fi
109+
else
110+
trace "::notice::No unit test related dependencies detected, skip unit testing." "No unit test related dependencies detected, skip unit testing."
111+
fi
112+
fi
113+
fi
114+
}
115+
116+
get_remote_package_version() {
117+
if [ "$INPUT_FLUTTER" = "true" ]; then
118+
ACTIVATE_OUTPUT=`flutter pub global activate $PACKAGE`
119+
else
120+
ACTIVATE_OUTPUT=`dart pub global activate $PACKAGE`
121+
fi
122+
REMOTE_PACKAGE_VERSION=`echo "$ACTIVATE_OUTPUT" | perl -n -e'/^Activated .* (.*)\./ && print $1'`
123+
if [ -z "$REMOTE_PACKAGE_VERSION" ]; then
124+
REMOTE_PACKAGE_VERSION=""
125+
fi
126+
trace "::notice::Local version: [$LOCAL_PACKAGE_VERSION]" "Local version: [$LOCAL_PACKAGE_VERSION]"
127+
trace "::notice::Remote version: [$REMOTE_PACKAGE_VERSION]" "Remote version: [$REMOTE_PACKAGE_VERSION]"
128+
echo "remoteVersion=$REMOTE_PACKAGE_VERSION" >> $GITHUB_OUTPUT
129+
}
130+
131+
format() {
132+
if [ "$INPUT_FORMAT" = "true" ]; then
133+
if [ "$INPUT_FLUTTER" = "true" ]; then
134+
flutter format .
135+
else
136+
dart format .
137+
fi
138+
fi
139+
}
140+
141+
publish() {
142+
if [ "$LOCAL_PACKAGE_VERSION" = "$REMOTE_PACKAGE_VERSION" ]; then
143+
trace "::notice::Remote & Local versions are equal, skip publishing." "📝Remote & Local versions are equal, skip publishing."
144+
else
145+
mkdir -p ~/.config/dart
146+
if [ -z "$INPUT_CREDENTIALJSON" ]; then
147+
cat <<-EOF > ~/.config/dart/pub-credentials.json
148+
{
149+
"accessToken":"$INPUT_ACCESSTOKEN",
150+
"refreshToken":"$INPUT_REFRESHTOKEN",
151+
"tokenEndpoint":"https://accounts.google.com/o/oauth2/token",
152+
"scopes": [ "openid", "https://www.googleapis.com/auth/userinfo.email" ],
153+
"expiration": 1577149838000
154+
}
155+
EOF
156+
else
157+
echo "$INPUT_CREDENTIALJSON" > ~/.config/dart/pub-credentials.json
158+
fi
159+
trace "Dry 🏃..."
160+
if [ "$INPUT_FLUTTER" = "true" ]; then
161+
flutter pub publish --dry-run
162+
else
163+
dart pub lish --dry-run
164+
fi
165+
if [ $? -eq 0 ]; then
166+
trace "::notice::Dry 🏃 Successfull."
167+
else
168+
if [ "$INPUT_FORCE" != "true" ] && [ "$INPUT_TESTRUNONLY" != "true" ]; then
169+
trace "::error::Dry 🏃 Failed, skip real publishing." "❌ Dry 🏃 Failed, skip real publishing."
170+
exit 1
171+
fi
172+
fi
173+
if [ "$INPUT_DRYRUNONLY" = "true" ]; then
174+
trace "::notice::Dry 🏃 only, skip publishing." "Dry 🏃 only, skip publishing."
175+
else
176+
trace "📦 Publishing..."
177+
if [ "$INPUT_FLUTTER" = "true" ]; then
178+
flutter pub publish -f
179+
else
180+
dart pub lish -f
181+
fi
182+
if [ $? -eq 0 ]; then
183+
echo "success=true" >> $GITHUB_OUTPUT
184+
trace "🚀" "🚀"
185+
else
186+
echo "success=false" >> $GITHUB_OUTPUT
187+
trace "" ""
188+
fi
189+
fi
190+
fi
191+
}
192+
193+
check_required_inputs
194+
switch_working_directory
195+
detect_flutter_package || true
196+
get_local_package_version
197+
run_unit_tests
198+
get_remote_package_version || true
199+
format || true
200+
publish || true

.github/workflows/on_pr_closed.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5353
- name: Publish Dart Package 🚢
5454
id: publish
55-
uses: k-paxian/dart-package-publisher@master
55+
uses: ./.github/actions/pub
5656
with:
5757
accessToken: ${{ secrets.OAUTH_ACCESS_TOKEN }}
5858
refreshToken: ${{ secrets.OAUTH_REFRESH_TOKEN }}

.github/workflows/run_pub.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
fi
3333
- name: Publish Dart Package 🚢
3434
id: publish
35-
uses: k-paxian/dart-package-publisher@1.5
35+
uses: ./.github/actions/pub
3636
with:
3737
accessToken: ${{ secrets.OAUTH_ACCESS_TOKEN }}
3838
refreshToken: ${{ secrets.OAUTH_REFRESH_TOKEN }}

0 commit comments

Comments
 (0)