Sharing a variable across jobs

Hello,

we have an action to build HTML and PDF of our project documentation for pull requests and also publish it when things are pushed into the main branch. It looks something like this:

---
name: deploy

on:
  push:
    branches:
      - master
      - "[0-9]+.[0-9]+"
  pull_request:
    branches:
      - master
      - "[0-9]+.[0-9]+"

jobs:
  build-html:
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
        working-directory: guides
    steps:
      - name: Build HTML
        run: |
          make html
      - name: Upload HTML
        uses: actions/upload-artifact@v2
        with:
          name: foreman-docs-html-${{ env.BRANCH_NAME }}
          path: guides/build/

  build-pdf:
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
        working-directory: guides
    steps:
      - name: Build PDFs
        run: |
          make pdf
      - name: Upload PDFs
        uses: actions/upload-artifact@v2
        with:
          name: foreman-docs-pdf-${{ env.BRANCH_NAME }}
          path: guides/build/*.pdf

  publish:
    if: github.repository_owner == 'theforeman' && (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/heads/2.') || startsWith(github.ref, 'refs/heads/3.') || startsWith(github.ref, 'refs/heads/4.'))
    needs:
      - build-html
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
    steps:
      - name: Download HTML
        uses: actions/download-artifact@v2
        with:
          name: foreman-docs-html-${{ env.BRANCH_NAME }}
          path: public/${{ env.BRANCH_NAME }}
      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./public
          keep_files: true

I shortened it a bit, full version here: foreman-documentation/deploy.yml at master · theforeman/foreman-documentation · GitHub

Now, PDF generation is slow and I would like to run it only when we push into master or stable branches (2.x/3.x etc). So I would like to share the huge if-statement that is in the publish job also with the build-pdf job. How can I create a variable that can be shared across jobs?

My second question would be: can I write the rule in a better way? The goal is only to trigger “publish” and “build-pdf” when we actually merge and push into the main repository into master/2.x/3.x. This must not trigger when we push into gh-pages (this could actually create some loop of sorts).

Thanks.

I’ve not been able to go through in detail, but does this thread:

…address the use case here? Or is this a unique request.

Only hoping to quickly identify valuable content, so apologies if this isn’t what you’re asking for.

1 Like

Thanks, that did not work. I am going for creating an environment variable and do multiple if statements.

Use a job output. For example you could have another job that checks the condition, writes the result to a job output, and then check that output in the following jobs (requires a needs relationship between the jobs).

1 Like