Fork me on GitHub
#polylith
<
2023-10-01
>
Don Waldhalm18:10:24

I'm trying to run my tests in a github action such that should they fail, it won't go to the next step (deploy). I've written 'clojure -M:poly test' in a step, which seems to have run the tests. Unfortunately, it doesn't seem to matter whether or not the tests pass. to be fair, I'm even newer at github actions than I am at polylith, so there may lie the problem. What is the idiomatic path to accomplish this test step in the workflow?

furkan3ayraktar19:10:56

You can check Polylith’s own https://github.com/polyfy/polylith/blob/master/.circleci/config.yml (using CircleCI). There is a documentation about it https://cljdoc.org/d/polylith/clj-poly/0.2.18-SNAPSHOT/doc/ci/polylith-ci-setup. At my company, we use GitHub actions to test our Polylith workspace. It simply runs the following run step:

- name: Run tests
  run: clojure -M:poly test :project

seancorfield19:10:15

What version of the Clojure CLI are you using? I think old versions didn't set exit status correctly.

seancorfield19:10:48

clojure -version

furkan3ayraktar19:10:30

Good point, Sean. Here is the GitHub action and the CLI version it uses from our config:

- name: Install Clojure CLI
  uses: DeLaGuardo/[email protected]
  with:
    cli: 1.11.1.1208

Teemu Kaukoranta20:10:01

Don, have you managed to setup any other task properly? I'm asking because you said you're new to GHA. How about debugging the task by replacing the poly command with just exit 1?

Don Waldhalm05:10:21

thanks for all the help, guys:

Don Waldhalm05:10:46

- name: Install clojure tools uses: DeLaGuardo/[email protected] with: cli: 1.11.1.1413

Don Waldhalm05:10:52

@UCQGNA673 yes, I've succeeded in getting the jar to build and deploying to an app service on Azure. I'll post my whole workflow with the problem area commented:

Don Waldhalm05:10:55

name: Build and deploy luapi.jar to Azure Web App

env:
  AZURE_WEBAPP_NAME: app-Advocate-Prod-EastUS-002   

on:
  workflow_dispatch:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Prepare java
        uses: actions/setup-java@v3
        with:
          distribution: 'zulu'
          java-version: '8'

      - name: Install clojure tools
        uses: DeLaGuardo/[email protected]
        with:
          # The value must indicate a particular version of the tool, or use 'latest'
          # to always provision the latest version
          cli: 1.11.1.1413              # Clojure CLI based on tools.deps

      - name: Execute clojure code
        run: clojure -e "(+ 1 1)"

      - name: Build with clojure tools
        run: clojure -T:build uberjar :project luapi 
        working-directory: clojure/advocate/

     # Optional step:
      - name: Cache clojure dependencies
        uses: actions/cache@v3
        with:
          path: |
            ~/.m2/repository
            ~/.gitlibs
            ~/.deps.clj
          # List all files containing dependencies:
          key: cljdeps-${{ hashFiles('deps.edn') }}
          restore-keys: cljdeps-

      ##########################################################################
      # This is the step that only kinda works...the logs show it fetching
      # dependencies, so I think it is working, but I can't see any test 
      # output, and if a test fails, the upload and deploy just keep going
      ##########################################################################
      - name: Run unit tests with Poly
        run: clojure -M:poly test
        working-directory: clojure/advocate/
      ##########################################################################
      # end problem
      ##########################################################################

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v3
        with:
          name: java-app
          path: '${{ github.workspace }}/clojure/advocate/projects/luapi/target/luapi.jar'

  deploy:
    runs-on: ubuntu-latest
    needs: build

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v3
        with:
          name: java-app

      - name: Deploy to Azure Web App
        id: deploy-to-webapp
        uses: azure/webapps-deploy@v2
        with:
          app-name: ${{ env.AZURE_WEBAPP_NAME }}
          publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE_RM_COMMISSION_UPLOAD }}
          package: 'luapi.jar'

seancorfield05:10:44

There's no 12.1 version of Clojure

seancorfield05:10:35

Hmm, weird, there IS a 12.1 version of the setup script... that's confusing.

seancorfield05:10:02

And what is your :poly alias @U02PN5M0VMW?

Don Waldhalm05:10:11

:poly {:extra-deps {polylith/clj-poly {:mvn/version "0.2.17-alpha"}}
         :main-opts  ["-m" "polylith.clj.core.poly-cli.core"]}

Don Waldhalm05:10:23

the github job runner for the "Run tests with Poly" step is 100+ lines of "downloading: ..." then Execution time: 0 seconds

Don Waldhalm05:10:02

I expected something like the console output I see when I run clojure -M:poly test locally. I'm misunderstanding something about what that command actually does on the Github Action, I think.

Don Waldhalm05:10:58

@U04V70XH6 I put clojure -version in the action--it prints Clojure CLI version 1.11.1.1413

Don Waldhalm05:10:33

I added

- name: try just exit 1
  run: exit 1
after the tests, and it stopped the build with an error as I would have expected, and did not proceed to deploy.

seancorfield15:10:34

So "Execution time: 0 seconds" says Polylith doesn't think anything has changed since the last stable tag - so there are no tests to run (and the exit code will be zero - success).

Don Waldhalm15:10:43

@U04V70XH6 fantastic--you did it! I changed my step to

clojure -M:poly test :dev :all
and now, my test output is visible in the log as I expect AND my failing test is registered as an error so the subsequent steps are not executed. Thanks again for all the help.

seancorfield16:10:16

Normally, we don't use :dev (run tests in the development project context) and we don't use :all (run all tests) because the real benefit of poly test in CI is that it should only run tests if code has changed, and only run the tests that could be affected by that. In our CI pipeline, we tag the source as a release (after successfully running the tests, then we bake the release tag into the source code so the apps can tell you what tag they were built from). And our poly test command has since:release so that anything that has changed since the previous release will get tested. We also only build/deploy artifacts that have changed (I blogged about this in https://corfield.org/blog/2022/12/07/deps-edn-monorepo-10/ -- see Polylith to the Rescue, Part 4 in that post).

seancorfield16:10:25

Locally, we run poly test which tests anything since the last stable tag, and each developer maintains their own stable-<username> tag (we have a task in our build.clj to handle this automatically). When we run poly test in CI, we use this command:

clojure -M:poly test since:release
and we tag each successful CI build as a release (and deploy it to our staging environment, where it can be promoted to production).

seancorfield16:10:49

Happy to dive into more detail any time and answer any Qs you may have.