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

Configuring GitHub actions #111

Merged
merged 10 commits into from
Dec 7, 2023
Merged

Configuring GitHub actions #111

merged 10 commits into from
Dec 7, 2023

Conversation

ftomassetti
Copy link
Member

@ftomassetti ftomassetti commented Dec 6, 2023

The GitHub actions should now run for every commit and every PR.
We just perform some basic tasks, like compiling for the JVM and running tests on the JVM. Later one we could enable more tasks.

Fix #108

@ftomassetti ftomassetti marked this pull request as ready for review December 6, 2023 16:16
@ftomassetti
Copy link
Member Author

@lppedd would this work for you?

@@ -138,3 +138,4 @@ Desktop.ini
*~
.directory
.merge_file*
.idea
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea of committing a subset of .idea was to streamline inspections and formatting, mostly. I generally tend to do this everywhere, so a dev can jump in and have the environment ready with no additional processes needed.

But if you feel like it doesn't suit the repo, feel free to remove those files.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to have all the .idea configuration generated by gradle. I just thought these files were committed by mistake

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to have all the .idea configuration generated by Gradle

A part of those files are indeed fine as generated at project import time, but others like codeStyles/* or inspectionProfiles/* are not generated, and when opening the project you'll find the "Default" settings applied. This means everyone will add his own custom formatting.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For formatting, I think we can add ktlint and .editorconfig, which should help with consistency with different IDEs. Perhaps I can add an issue about that

Copy link
Contributor

@lppedd lppedd Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would avoid KTLint and stick with editorconfig then. I can generate the .editorconfig file in case.
KTLint configuration is a PITA and it doesn't have a reliable IDE plugin.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, let's go with .editorconfig then!
Should I consider this a follow up task and proceed with this merge or should we add this to this PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I can add it, but you can generate it:

  1. Restore the files you deleted

  2. Open up the IntelliJ Settings (checking it says "Project" as scheme) and export as EditorConfig

    image

  3. Select the root of the repository as export target

  4. Delete the .idea files again

@ftomassetti
Copy link
Member Author

I changed this to have two separate actions: one for building and one for running tests
Screenshot 2023-12-06 at 17 42 47

Each of them runs both on java 11 and java 17. Java 8 cannot be supported as ANTLR 4.13 requires at least Java 11

@lppedd
Copy link
Contributor

lppedd commented Dec 6, 2023

Studying how Actions are set up right now.

@ftomassetti

as ANTLR 4.13 requires at least Java 11

Is this related to the generation process or also to the runtime?
If this impacts the Java runtime, we can also move up the generated bytecode version in our build scripts.

@ftomassetti
Copy link
Member Author

Studying how Actions are set up right now.

@ftomassetti

as ANTLR 4.13 requires at least Java 11

Is this related to the generation process or also to the runtime?

For sure this affects the build process, so I need to run GitHub actions with Java 11. But it is true that we can still setup the build to compile for Java 8. However that would be unrelated from the GitHub actions configuration

distribution: 'adopt'
java-version: ${{ matrix.java }}
- name: Test
run: ./gradlew ciBasicBuild
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at those two tasks (ciTest and ciBasicBuild) it seems we're just wasting CI time. Building is done two times, so I guess an unified task is enough, it will just say "Build" and we know it also contains tests.

Instead of creating custom tasks you can just invoke:

gradlew build

And it will take care of building and testing what it can actually build and test.
Under Ubuntu it's going to be JVM, JS, and linuxX64.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is true that there is overlap between the two tasks. I originally add one, but following your comment in an issue I thought you preferred to have them split.

Normally I would use build or check, but at least on Mac they require to have Xcode installed to run also the native builds. I am not sure what kind of configuration it would require on GitHub actions, perhaps configuring GCC or other compilers, so I was thinking that we could get away with testing on the JVM for these frequently running actions. We could then have more complete actions later, to build on all platforms, and run those before a release or trigger them manually.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but following your comment in an issue I thought you preferred to have them split.

Yeah I had guessed you did it because of this, sorry! In Jenkins I'm used to publish custom "check points" back to my GitHub Enterprise instance, but I guess GitHub Actions are not that advanced yet.

so I was thinking that we could get away with testing on the JVM for these frequently running actions

I'd test both JVM and JS. JS is a risky platform and it should be always tested imo (I know this by experience).

I'd say you can use gradlew build -Ptarget.is.native=false.
target.is.native=false disables all the Kotlin Native targets, I had this one prepared for this exact occasion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I will run ./gradlew check -Ptarget.is.native=false.target.is.native=false

Comment on lines 21 to 22
- name: Test
run: ./gradlew ciBasicBuild
Copy link
Contributor

@frett frett Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a gradle-build-action action that will automatically cache various gradle build files between subsequent actions.

Suggested change
- name: Test
run: ./gradlew ciBasicBuild
- name: Test
uses: gradle/gradle-build-action@v2
with:
arguments: ciBasicBuild

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

relevant action repo for reference: https://github.com/gradle/gradle-build-action

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, but for now I will keep things simple and avoid caches. For now I think we can have a single job and step and just look at logs if something fails

@frett
Copy link
Contributor

frett commented Dec 6, 2023

on my fork of the repo I had setup a GHA workflow for tests and deploying to our JFrog repo. feel free to grab anything you want from that action file, or ask questions about it.

https://github.com/CruGlobal/antlr-kotlin/blob/build/v0.0.7/.github/workflows/publish-fork.yml

@lppedd
Copy link
Contributor

lppedd commented Dec 6, 2023

@frett looks pretty neat! I've got a question, what's up with the Cache Konan step?

@frett
Copy link
Contributor

frett commented Dec 6, 2023

@frett looks pretty neat! I've got a question, what's up with the Cache Konan step?

Kotlin Multiplatform uses a Konan runtime to actually do the Kotlin/Native compilation. That binary was automatically downloaded and setup everytime a build would run. Unfortunately that isn't captured by the gradle-build-action caching, so I set it to manually cache it

@frett
Copy link
Contributor

frett commented Dec 6, 2023

the publish action on my GHA workflow runs on macos to have access to XCode in order to get the ios targets.

running it on macos does also support jvm & js, but there is potential additional costs for a macos runner vs a normal ubuntu runner. We have a enterprise account so I'm not sure if there are any costs or restrictions in place for an open source project

@lppedd
Copy link
Contributor

lppedd commented Dec 6, 2023

Oh I see, makes sense!

So (pardon my inexperience on Actions), splitting up the YAML file, we could have:

on:
  push:
    branches: ['master']
  pull_request:
    branches: ['*']

And:

jobs:
  build_and_test:
    name: Build & Test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Cache Konan
        uses: actions/cache@v3
        with:
          path: ~/.konan
          key: ${{ runner.os }}-konan-${{ github.sha }}
          restore-keys: ${{ runner.os }}-konan-
      - name: Run Build and Tests
        uses: gradle/gradle-build-action@v2
        with:
          arguments: build -Ptarget.is.native=false

Would this work?

@frett
Copy link
Contributor

frett commented Dec 6, 2023

I would do:

on:
  push:
    branches: ['master']
  pull_request:
    branches: ['master']

the pull_request triggers are based on the branch you are merging into

@lppedd
Copy link
Contributor

lppedd commented Dec 6, 2023

Thank you!
We also have a matrix now:

strategy:
  matrix:
    java: [ '11', '17' ]

I guess this would mean two JS builds too. Maybe running on Java 11 only is sufficient?

@frett
Copy link
Contributor

frett commented Dec 6, 2023

it's probably sufficient, but the GHA workflow is only taking 2-3 mins to run currently, so running it on both toolchains vs 1 wouldn't have a huge impact. plus those 2 jobs would run concurrently.

@lppedd
Copy link
Contributor

lppedd commented Dec 6, 2023

Sounds reasonable. So, looking at the overall picture, we are left with:

name: Build

on:
  push:
    branches: ['master']
  pull_request:
    branches: ['master']

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build_and_test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        java: ['11', '17']
    name: Build & Test (Java ${{ matrix.Java }})
    steps:
      - name: Setup Java
        uses: actions/setup-java@v3
        with:
          distribution: 'adopt'
          java-version: ${{ matrix.java }}
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Cache Konan
        uses: actions/cache@v3
        with:
          path: ~/.konan
          key: ${{ runner.os }}-konan-${{ github.sha }}
          restore-keys: ${{ runner.os }}-konan-
      - name: Run Build and Tests
        uses: gradle/gradle-build-action@v2
        with:
          arguments: build -Ptarget.is.native=false

I'm starting to understand how all of this work finally.

@frett
Copy link
Contributor

frett commented Dec 6, 2023

yeah, although you missed the setup-java step :)

@lppedd
Copy link
Contributor

lppedd commented Dec 6, 2023

Damn, thanks! Added back in the comment.

@frett
Copy link
Contributor

frett commented Dec 6, 2023

that looks good and should work as a starting point, the workflow can evolve over time as needs arise :)

@lppedd
Copy link
Contributor

lppedd commented Dec 6, 2023

@frett so had a bit of docs reading and came up with:

Edit: updated with a working script

name: Build

on:
  push:
    branches: [ 'master' ]
  pull_request:
    branches: [ 'master' ]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    strategy:
      matrix:
        include:
          - java: '11'
            target: JVM
            task: jvmTest
          - java: '17'
            target: JVM
            task: jvmTest
          - java: '17'
            target: JS
            task: jsTest
    runs-on: ubuntu-latest
    name: Build (${{ matrix.java }} / ${{ matrix.target }})
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Setup Java
        uses: actions/setup-java@v3
        with:
          distribution: 'adopt'
          java-version: ${{ matrix.java }}

      - name: Cache Konan
        uses: actions/cache@v3
        with:
          path: ~/.konan
          key: ${{ runner.os }}-konan-${{ github.sha }}
          restore-keys: ${{ runner.os }}-konan-

      - name: Setup Gradle
        uses: gradle/gradle-build-action@v2

      - name: Run Build and Tests (${{ matrix.target }})
        run: ./gradlew ${{ matrix.task }}
        working-directory: ${{ github.workspace }}
        continue-on-error: false

This allows you to execute three jobs:

- JDK 11 + JVM target
- JDK 17 + JVM target
- JDK 17 + JS target

@frett
Copy link
Contributor

frett commented Dec 6, 2023

I'm not sure without giving it a try

@lppedd
Copy link
Contributor

lppedd commented Dec 6, 2023

I've actually found out we can just do the following to define the matrix runs:

matrix:
  include:
    - java: '11'
      target: JVM
      task: jvmTest
    - java: '17'
      target: JVM
      task: jvmTest
    - java: '17'
      target: JS
      task: jsTest

Easier and probably more understandable.

@ftomassetti
Copy link
Member Author

thank you @frett and @lppedd , I learned something very useful thanks to you. I just applied your suggestion as-is, and that works great. Let's get it merged

@ftomassetti ftomassetti merged commit 9ad8927 into master Dec 7, 2023
3 checks passed
@ftomassetti ftomassetti deleted the githubActions branch December 7, 2023 09:48
@lppedd
Copy link
Contributor

lppedd commented Dec 7, 2023

Thanks! Another step towards easier maintenance is done 😎

@sschuberth sschuberth mentioned this pull request Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Setup GitHub Actions
3 participants