Search code examples
github-actionsterraform-provider-awsaws-lambda-layers

Github action optional step execution


I am trying to execute some option steps if the previous step is failing. I need to download the old artifacts in order to avoid that my Terraform build action is not detecting changes. Therefore I added a diff action to identify if the docker file to build the zip-layer has changed, if there is no change the old artifacts from a previous execution should be downloaded. In some cases the last execution does not contain the artifacts e.g. failure of the jobs. In that case I would like to get the latest version based on the existing docker image. Note: The code is part of a matrix execution but for simplicity I reduced the action to the problem area.

  job_prepare:
     .....
  job_layers:
    needs: job_prepare
    runs-on: ubuntu-latest
    strategy:
      matrix: ${{fromJson(needs.job_prepare.outputs.layer_matrix)}}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: matrix name
        run: |
          echo $GITHUB_WORKSPACE
          echo ${{ matrix.path }}
      - uses: technote-space/get-diff-action@v3
        id: git_diff
        with:
          PREFIX_FILTER: ${{ matrix.prefix }}
          SUFFIX_FILTER: Dockerfile
      - name: LayerDockerBuild
        id: layerDocker
        if: steps.git_diff.outputs.diff
        run: |
          docker build ...
          docker push ...
      - name: Layer via Artifacts
        if: (steps.git_diff.outputs.diff == false)
        uses: dawidd6/action-download-artifact@v2
        with:
          workflow: review.yml
          name: ${{ matrix.name }}
      - name: Layer via Docker
        if: steps.git_diff.outputs.diff && ${{ failure() }}
        id: layerD
        run: |
          ....
          docker pull "docker.pkg.github.com/$REPO_NAME/$IMAGE_ID:$VERSION"
          docker run --rm -v $GITHUB_WORKSPACE:/data docker.pkg.github.com/$REPO_NAME/$IMAGE_ID:$VERSION cp /packages/${{ matrix.name }}.zip /data
      - name: Upload layer zip
        if: ${{ always() }}
        uses: actions/upload-artifact@v2
        with:
          name: ${{ matrix.name }}
          path: ${{ matrix.name }}.zip

The problem is basically the logic in the line if: steps.git_diff.outputs.diff && ${{ failure() }}

thanks for your help on any hints how to make the step option when the diff is false and the step ~Layer via Artifacts~ is not failing.


Solution

  • To execute your step if the previous one was not a failure you can just set failure() without brackets. With the if, no need of brackets.

    if: steps.git_diff.outputs.diff && failure()
    

    Hope it will help