skip to Main Content

I have a Scala based multi module project for which I’m having a GitHub Actions pipeline which contains two jobs, one for test and the other for publishing to GitHub packages. Here is my file:

name: Build my projects

on:
  push:
    paths-ignore:
      - 'images/**'
      - README.md
    branches:
      - master
    tags:
      - 'v*.*.*'
  pull_request:
    branches:
      - master

  release:
    types: [ created ]

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
      - uses: actions/checkout@v2

      - name: Cache ivy2
        uses: actions/cache@v1
        with:
          path: ~/.ivy2/cache
          key: ${{ runner.os }}-sbt-ivy-cache-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }}

      - name: SBT Test
        run: sbt clean test

  publish:
    needs: test
    steps:
      - name: Checkout
      - uses: actions/checkout@v2

      - name: SBT Publish
        run: sbt publish

I would need the following:

  1. Trigger the publish job only when I want to do a release, but how do I know that I want to do a release? Do I tag a release when I commit the changes? If I tag it, then how can I check if there is a tag so that I know that I have to run the publish job?

2

Answers


  1. Chosen as BEST ANSWER

    I just had to do this in the publish job:

      publish:
        runs-on: ubuntu-latest
        needs: test
        if: startsWith(github.ref, 'refs/tags/v')
    

    So when there is a tag, the publish job knows that it has to run and publish the new package.


  2. If you want to trigger a workflow "only when you want to do a release", one option is to manually launch the workflow.

    This can be achieved with a specific workflow with following trigger workflow_dispatch:

    on:
      workflow_dispatch:
        inputs:
          releaseVersion:
            description: 'Release version'
            required: true
    

    Here I add an input value that has to be entered manually when launching the workflow. This is not mandatory though if you don’t need any input.

    See also: https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/


    Note that triggering the workflow when a tag is pushed is also a solution that makes sense. It’s really up to you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search