Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub Actions CI Conditional Regex

Tags:

I'm trying to move my CI workflow from CircleCI to GitHub Actions. The last major struggle I'm facing is with deployment.

Currently my workflow is such that when I push a tag to my GitHub repo, it will run the tests, then run the deployment. Only thing is CircleCI filters tags to only run the job if the tag matches the regex: /v[0-9]+(\.[0-9]+)*/.

How can I check to ensure the tag I pushed matches the regex pattern above before running the deployment?

I currently have the following GitHub Actions yml file:

name: CI
on: [create]

jobs:
  # ...

  deploy:
    runs-on: ubuntu-latest
    if: github.event.ref_type == 'tag' && github.event.ref == SOMETHING HERE
    steps:
      - uses: actions/checkout@v1
      # ...

Under the if block, I need to change github.event.ref == SOMETHING HERE to be something else. I have looked in the Contexts and expression syntax for GitHub Actions documentation page. But due to how flexible and powerful GitHub Actions is, it seems like there should be a method or way to do this, or at least some type of workaround.

How can I ensure the tag (github.event.ref) matches the regex pattern (/v[0-9]+(\.[0-9]+)*/)?

like image 490
Charlie Fish Avatar asked Nov 14 '19 17:11

Charlie Fish


People also ask

How do I set an environment variable in GitHub Actions?

To set a custom environment variable, you must define it in the workflow file. The scope of a custom environment variable is limited to the element in which it is defined. You can define environment variables that are scoped for: The entire workflow, by using env at the top level of the workflow file.

What is GitHub Head_ref?

github.head_ref. string. The head_ref or source branch of the pull request in a workflow run. This property is only available when the event that triggers a workflow run is either pull_request or pull_request_target .

Do GitHub Actions run as root?

/github/workspace - Note: GitHub Actions must be run by the default Docker user (root).


1 Answers

Unfortunately, I don't think there is any way to do regex matching on if conditional expressions yet.

One option is to use filtering on push events.

on:
  push:
    tags:
      - 'v*.*.*'

Another option is to do the regex check in a separate step where it creates a step output. This can then be used in an if conditional.

      - name: Check Tag
        id: check-tag
        run: |
          if [[ ${{ github.event.ref }} =~ ^refs/tags/v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
              echo ::set-output name=match::true
          fi
      - name: Build
        if: steps.check-tag.outputs.match == 'true'
        run: |
          echo "Tag is a match"
like image 151
peterevans Avatar answered Oct 07 '22 22:10

peterevans