DEV Community

Amit Chauhan
Amit Chauhan

Posted on

Automating Jar File Creation and Release Artifact Uploads with GitHub Actions

You have this great java project and every time you make changes to the codebase you need to manually create the jar file and upload it as a release. Let's see how you can automate this release workflow. For this post, I am assuming that your repo sits in github.

  • We are going to use github actions.

  • Create a .github/workflows directory in your repository on GitHub if this directory does not already exist.

  • In the .github/workflows directory, create a file named release.yml.

  • Copy the following code in release.yml



name: Java CI to create and upload release on pull request
on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

env:
  build-number: ${GITHUB_RUN_NUMBER}

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-java@v3
        with:
          java-version: '11'
          distribution: 'temurin'
          cache: 'maven'
      - run: mvn clean package -DskipTests
      - run: mkdir staging && mv target/yb-workload-sim.jar target/yb-workload-sim-${{ env.build-number }}.jar && cp target/*.jar staging
      - uses: actions/upload-artifact@v3
        with:
          name: Package
          path: staging
          retention-days: 1
      - uses: marvinpinto/action-automatic-releases@latest
        with:
          repo_token: "${{ secrets.YOUR-GITHUB-TOKEN }}"
          automatic_release_tag: "${{ github.run_number }}"
          title: "Automated Build ${{ github.run_number }}"
          prerelease: true
          files: staging/*.jar



Enter fullscreen mode Exit fullscreen mode

Let's discuss what we are doing in above code:

  1. on: tag indicates when this action should be triggered. In this example, I am triggering it when someone creates a pull request or when code is pushed to the main branch.

  2. jobs:
    This steps defines the various steps involved in this job. We will use ubuntu-latest os version.

  3. Then we will use prebuilt actions checkout to get the code.

  4. After that we setup java for compiling the jar.

  5. We are using maven to build the jar file. We will fire that up to create a jar file.

  6. When jar file is created, we will do some massaging to add the release number and then upload it as artifact using upload-artifact action.

  7. Last, we will use marvinpinto/action-automatic-releases action to create and upload new release. There are multiple actions available in the marketplace to create the release.

That's it!! Now every time you push new code, github actions will create and upload jar for you.

You can see the working example here.

Screenshot showcasing the automated CI build and release process

Top comments (0)