Foojay Today

Lights, Camera, Action: GitHub Actions with Java (Part 3)

February 03, 2022
Spiderman chooses GitHub Action's Disco API

The Java GitHub Action that Rules them all - Disco API

Introduction

When we ended Part 2 of this blog series Lights, Camera, Action: GitHub Actions with Java Part 2, I talked about the near future of GitHub Actions. Well, today the future is now, so let's get into this!

Welcome back to the series of articles on GitHub Actions with Java Part 3. If you didn’t catch Part 1 & 2 head over here: Part 1 & Part 2

In this article we will talk about the new JDK distributions added to the default GitHub actions [email protected] and the New [email protected] GitHub Actions from our very own Foojay.io. This new Disco GitHub Action uses the flexible and powerful Discovery API created by Gerrit Grunwald (@hansolo_). As you will see later, the Discover API is able to download any JDK/JRE distribution available from any vendor.

For the impatient the example code (yml) for this article is on GitHub ci.yml.

Using actions/[email protected]

New to GitHub Actions [email protected] are additional Java build distributions from the vendors Microsoft and Liberica. To keep an eye on their progress you can see the link below:
https://github.com/actions/setup-java#supported-distributions

As a devops person or owner of a repository on GitHub you'll need to create a GitHub Actions workflow file. To create an Actions workflow I already provide instructions in Part 2 where you are presented a template using the following code snippet:

steps: 
  - uses: actions/[email protected] 
  - name: Set up JDK ${{ matrix.java-version }} 
    uses: actions/[email protected] 

Here you'll notice the actions/[email protected] is the same uses value, but will now support 2 additional vendor build distributions microsoft and liberica.

Specify a Supported JDK Distribution

To specify the build distribution you simply add it to the distribution attribute as shown below:

with:
  java-version: ${{ matrix.java-version }} 
  distribution: 'liberica'

Of course you can create a matrix to specify all the available distributions like the example below:

build:
  runs-on: ubuntu-latest
  strategy:
    matrix:
      java-version: [ 17 ]
      distribution: [ 'zulu', 'microsoft', 'liberica' ]
  steps:
    - uses: actions/[email protected]
    - name: Set up JDK ${{ matrix.java-version }}
        uses: actions/[email protected]
        with:
          java-version: ${{ matrix.java-version }}
          distribution: ${{ matrix.distribution }}
          cache: maven
    - name: Build with Maven
      run: mvn -B package --file pom.xml

Above you'll notice the workflow will build and test the repo for JDK 17 on Linux with zulu, microsoft, and liberica distributions.

To see the example workflow head over to github here
https://github.com/carldea/HelloWorldGHActions/blob/main/.github/workflows/ci.yml#L10-L25

Using foojayio/[email protected]

New to GitHub Actions foojayio/[email protected] is the ability to specify any Java build distribution from any vendor such as the following:

Note: Visit the links above for licenses and support offerings from the respective vendors.

To keep an eye on new build distributions added you can see the link below:
https://github.com/foojayio/discoapi

As a devops person or owner of a repository on GitHub you'll need to create a GitHub Actions workflow file. To create an Actions workflow I already provide instructions in Part 2 where you are presented a template using the following code snippet. But the difference is you want to replace the uses attribute value with foojayio/[email protected].

steps:
  - uses: actions/[email protected]
  - name: Set up JDK ${{ matrix.java-version }}
    uses: foojayio/[email protected]

Specify any JDK Distribution

Now that this job uses the new foojayio/[email protected] you can now specify any JDK distribution as you wish! Let's look at ways we can specify distributions in yml.

Same as before you would specify the build distribution attribute with a particular distribution or the use of a matrix (array of items). What's nice about [email protected] is that if you remove the attribute distribution it will default to zulu builds of the OpenJDK.

Let's look at a build distribution that I chose as it relates to Azul Systems Inc., where we have two different build distributions of the OpenJDK. One is the plain community build and the other namely Azul Zulu Prime Builds of the OpenJDK includes specialized high performance features.

with: 
  java-version: ${{ matrix.java-version }} 
  distribution: 'zulu_prime'

Of course you can create a matrix to specify all the available distributions like the example below:

disco-build:
  runs-on: ubuntu-latest
  strategy:
    matrix:
      java-version: [ 17 ]
      distribution: [ 'zulu', 'zulu_prime', 'liberica', 'microsoft', 'corretto', 'openlogic', 'semeru', 'sap_machine', 'dragonwell' ]
  steps:
    - uses: actions/[email protected]
    - name: Set up JDK ${{ matrix.java-version }}
      uses: foojayio/[email protected]
      with:
        java-version: ${{ matrix.java-version }}
        distribution: ${{ matrix.distribution }}
        cache: maven
    - name: Build with Maven
      run: mvn -B package --file pom.xml

Above you'll notice the workflow will build and test the repo for JDK 17 on Linux with 'zulu', 'zulu_prime', 'liberica', 'microsoft', 'corretto', 'openlogic', 'semeru', 'sap_machine', and 'dragonwell' distributions.

To see the example workflow head over to github to:
https://github.com/carldea/HelloWorldGHActions/blob/main/.github/workflows/ci.yml#L27-L42

When the GitHub action workflows are run successfully the summary should look like the following:

GitHub Actions workflows run successfully

GitHub Actions workflows run successfully

Conclusion

You got a chance to see the new distributions added to actions/[email protected] for you to choose from. Also, you were shown the New foojayio/[email protected] GitHub Actions that enables you to specify ANY build distribution! When using this action you can remove the distribution attribute it will default to using zulu as a distribution. And lastly you saw the GitHub Actions CI/CD job run successfully.

Well, there you have it, more choices and more possibilities when building your CI/CD pipelines.

As always comments and feedback are encouraged.

Topics:

Related Articles

View All

Author(s)

  • Carl Dea

    Carl Dea is a Senior Developer Advocate at Azul. He has authored Java books and has been developing software for 20+ years with many clients, from Fortune 500 companies to ... Learn more

Comments (0)

Your email address will not be published.

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard