Home > Software engineering >  Github Actions Application is being build twice
Github Actions Application is being build twice

Time:12-09

I have a problem. I am starting to work with Github Actions, and I got a working pipeline where I am trying to:

  1. Build the docker application
  2. Run the tests
  3. Publish to docker repository

But the test running and publishing are 2 separate jobs, so I am building the image twice. Here is the workflow code:

name: Test & Publish Docker Image

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  run_application_tests:
    name: Run test suite
    runs-on: ubuntu-latest
    env:
      COMPOSE_FILE: docker-compose.yml
      DOCKER_USER: ${{ secrets.DOCKER_USER }}
      DOCKER_PASS: ${{ secrets.DOCKER_PASS }}

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

    - name: Login To Docker Hub
      uses: docker/login-action@v2
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}

    - name: Build and run docker image
      run: docker-compose up -d --build

    - name: Run migrations
      run: make migrate

    - name: Run tests
      run: make test

  build_and_publish_docker_image:
    needs: run_application_tests
    name: Build & Publish Docker Images
    runs-on: ubuntu-latest
    steps:
    - name: Checkout The Repo
      uses: actions/checkout@v3
    
    - name: Login To Docker Hub
      uses: docker/login-action@v2
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}

    - name: Build & Push Docker Image
      uses: docker/build-push-action@v3
      with:
        push: true
        tags: |
          me/image:latest
          me/image:${{ github.sha }}

The docker image is a Ruby-On-Rails application so the bundle install takes very long. Each build takes about 2-3 minutes. I also tried adding:

cache-from: type=gha
cache-to: type=gha,mode=max

To the docker/build-push-action@v3, but that resulted in:

Error: buildx failed with: ERROR: cache export feature is currently not supported for docker driver. Please switch to a different driver (eg. "docker buildx create --use")

What can I change to improve this pipeline time based?

CodePudding user response:

in your job run_application_tests you have this code:

- name: Login To Docker Hub
  uses: docker/login-action@v2
  with:
    username: ${{ secrets.DOCKER_USERNAME }}
    password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and run docker image
  run: docker-compose up -d --build

this code runs docker-compose why?

this is how your code should be if you want to run tests only:

jobs:
  run_application_tests:
    name: Run test suite
    runs-on: ubuntu-latest

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

    - name: Run migrations
      run: make migrate

    - name: Run tests
      run: make test
  • Related