WZH
WZH

Reputation: 464

How to run a Github Action Task if merging to master or the VERSION file contains letter b

I would like to publish a Python package to pypi if merging to master OR a file named VERSION contains letter b. The VERSION file is located in the root of this repo.

I'm able to get the "merging to master" part work with the following code.

publish:
    needs: [build]
    runs-on: [self-hosted, generic-linux]
    container: python:3
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v2
        with:
          name: package
          path: ./dist

      - name: Install requirements
        run: |
          pip install --upgrade pip
          pip install --upgrade --prefer-binary twine

      - name: Upload to artifactory
        if: ${{ github.ref == 'refs/heads/master' }}
        env:
          TWINE_REPOSITORY_URL: https://artifactory.example.com/artifactory/api/pypi/pypi-all
          TWINE_REPOSITORY: pypi-all
          TWINE_USERNAME: "${{ secrets.PUBLISH_USERNAME }}"
          TWINE_PASSWORD: "${{ secrets.PUBLISH_BEARER_TOKEN }}"
        run: |
          twine upload --skip-existing --verbose dist/*

However, I'm not sure how to add an OR condition to check the content of a file. Could someone help?

Thanks.

Upvotes: 1

Views: 103

Answers (1)

Matteo
Matteo

Reputation: 39430

you could add an extra step to read the content (manually or using some existing GH action like https://github.com/marketplace/actions/read-files-action) of the file and add a condition the the Upload step, to check if the file contains the required string (with https://docs.github.com/en/actions/learn-github-actions/expressions#contains), like:


      - name: Checkout code
        uses: actions/checkout@v3

      - name: Read Version
        id: version
        uses: komorebitech/[email protected]
        with:
          files: '["VERSION"]'
      - name: Echo Version
        run: echo "${{ steps.version.outputs.content }}"


      - name: Upload to artifactory
        if: ${{ github.ref == 'refs/heads/master' || contains(steps.version.outputs.content, 'b')}}

Remember to checkout the code of the repo before try to read the file

Upvotes: 1

Related Questions