question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Service Containers can be defined in Composite Action but don't initialise

See original GitHub issue

Describe the bug When a service block is defined in a composite action, the containers do not initialise although no syntax errors are thrown.

Either it is not possible for this to work, so there should be an error not allowing e to include service block in composite action; or it is possible but isn’t working as expected.

To Reproduce

Composite Action Definition: (./.github/actions/example/action.yml)

name: "Run RSpec Tests"

inputs:
  spec:
    required: true

services:
  postgres:
    image: postgres:13.3
    env:
      POSTGRES_USER: example
      POSTGRES_DB: example
      POSTGRES_PASSWORD: "example"
    ports: ["5432:5432"]
    options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
  
  redis:
    image: redis 
    ports:
      - 6379:6379
    options: >-
      --health-cmd "redis-cli ping"
      --health-interval 10s
      --health-timeout 5s
      --health-retries 5
      
runs:
  using: "composite"

  steps:
    - name: Checkout repo
      uses: actions/checkout@v2

    - name: Restore Ruby Cache
      uses: ruby/setup-ruby@v1
      with:
        ruby-version: 2.7.4 
        bundler-cache: true 

    - name: Setup DB 
      run: bundle exec rake db:setup 
      shell: bash

    - name: RSpec Tests
      run: bundle exec rake spec:${{ inputs.spec }}
      shell: bash

And to call it in your main workflow yml:

jobs:
  setup_and_lint:
    name: Setup Ruby and Linting
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repo
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Setup Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 2.7.4 
          bundler-cache: true 
      - name: Linting 
        if: ${{ github.event_name == 'pull_request' }}
        uses: HeRoMo/pronto-action@v1.27.0

  rspec_tests_example1:
    name: RSpec Tests (example1)
    runs-on: ubuntu-latest
    needs: setup_and_lint
    steps:
      - name: Checkout repo
        uses: actions/checkout@v2

      - name: RSpec Tests
        uses: ./.github/actions/rspec_test
        with:
          spec: example1

And the example1 block could be replicated for many Specs.

Expected behavior

Either:

  • RSpec Tests jobs should run in parallel as normal, and when they execute, initialise the service containers specified in the composite action

OR,

  • If this is not possible (I realise that I’m calling the action at the Step level but Services are defined at Job level, so perhaps it’s not intended to work) then the syntax of the Composite action should throw an error to show that this is the case.

If that is the case, then it would be great to find another way of resuing Service definitions to save duplicated code.

Runner Version and Platform

ubuntu-latest

What’s not working?

  • The automated “Initialise Containers” step simply does not run
  • Because the containers aren’t created, the spec jobs fail due to nothing running on the defined ports.

Job Log Output

Screenshot 2021-12-02 at 11 46 35

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
pattwellcommented, Jun 29, 2022

@y-nk A very good point and useful addition to this thread, thank you!

I did actually try using exactly that matrix approach and while it did work, unfortunately as we have quite a lot of tests (9 specs) the whole the workflow ran extremely slowly (took about double the time in total) as I think by default they share a runner, or perhaps the service containers became a bottleneck.

Potentially worth revisiting though as it has been a while and I might be wrong on that, or we might be able to enforce runner segregation creatively to make it work more smoothly.

1reaction
y-nkcommented, Jun 29, 2022

@pattwell if that can help, i’m currently solving this issue by abusing the matrix feature. you can define one generic job with specific data in the matrix like this:

rspec_tests_example:
    name: RSpec Tests
    runs-on: ubuntu-latest
    needs: setup_and_lint

    matrix:
      specs:
        - example1
        - example2

    services:
      postgres:
        image: postgres:13.3
        env:
          POSTGRES_USER: example
          POSTGRES_DB: example
          POSTGRES_PASSWORD: "example"
        ports: ["5432:5432"]
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
  
      redis:
        image: redis 
        ports:
          - 6379:6379
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - name: Checkout repo
        uses: actions/checkout@v2

      - name: Restore Ruby Cache
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 2.7.4 
          bundler-cache: true 

      - name: Setup DB 
        run: bundle exec rake db:setup 
        shell: bash

      - name: RSpec Tests
        run: bundle exec rake spec:${{ matrix.spec }}
        shell: bash

there will be limitations to that but for the usecase i’m having (which seems similar to yours) it works just ok.

thank you for the great amount of reporting you did (here and in github community with the follow-ups etc)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to use default github actions env vars for composite ...
I just did a quick test with a composite action and it did output all the expected env vars (with run: env )....
Read more >
Patterns For Building Composite Applications With WPF
Doing this would mean that all services must be registered when the container is initially created. However, most modules have module-specific ...
Read more >
Pods | Kubernetes
Pods are designed to support multiple cooperating processes (as containers) that form a cohesive unit of service. The containers in a Pod are ......
Read more >
Using Simple Injector
An instance of Container is used to register mappings between each abstraction (service) and its corresponding implementation (component). Your application code ...
Read more >
Chapter 3. Beans, BeanFactory and the ApplicationContext
bean behavioral configuration elements, which state how the bean should behave in the container (i.e. prototype or singleton, autowiring mode, dependency ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found