Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Travis conditional build stages work?

Tags:

travis-ci

I am aiming to have three steps in my Travis flow:

  1. test (always)
  2. build (of docker image, only when a branch is merged into develop)
  3. deploy (same as build)

I've read the documentation on build stages and conditionals but I seem to be misunderstanding something, as Travis ALWAYS executes all three stages (for example, when I push a feature branch).

My current setup is as follows:

jobs:
  include:
    - stage: test
      script: ".travis/01-test.sh"
    - stage: build
      script: ".travis/02-build-and-push-image.sh"
      if: (branch = develop) AND (NOT(type IN (push, pull_request)))
    - stage: deploy
      script: ".travis/03-deploy.sh staging"
      if: (branch = develop) AND (NOT(type IN (push, pull_request)))

I can see this config when I open the config tab on the Travis build page, so it shouldn't be a parsing error. I have also tried listing jobs and stages separately, but the result is the same:

jobs:
  include:
    - stage: test
      script: ".travis/01-test.sh"
    - stage: build
      script: ".travis/02-build-and-push-image.sh"
    - stage: deploy
      script: ".travis/03-deploy.sh staging"
stages:
  - test
  - name: build
    if: (branch = develop) AND (NOT(type IN (push, pull_request)))
  - name: deploy
    if: (branch = develop) AND (NOT(type IN (push, pull_request)))

How can I make this work? Am I missing something? I know build stages are a beta feature, but I'd assume this should work already, according to a Travis blog post.

like image 439
Laura Avatar asked Dec 13 '17 23:12

Laura


1 Answers

I can confirm this is an issue while parsing the conditions internally.

Can you try adding a space after the "NOT" operator? As in:

if: (branch = develop) AND (NOT (type IN (push, pull_request)))

This seems to resolve the problem, I wonder if Travis is detecting not() as a function (as it does with env(foo)), and therefore this is causing the error.

like image 109
iriberri Avatar answered Jan 04 '23 06:01

iriberri