Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to target container in GitHub actions?

I am trying to use GitHub Actions to fire up a Postgres container for my tests. I have a script called build.sh that gets called when npm run build is called via GitHub actions. This script calls upon restore-schema.sh (shown below).

The issue here is when restore-schema.sh gets ran, I keep getting Error: no such container: postgres. GitHub actions is naming the container some arbitrary string. Is there not a way I can run docker exec on an image or somehow name the postgres container that GitHub actions is creating? I've looked through both documentations to no avail.

How should I go about this? I noticed that in the Docker run ps screenshot, it shows command docker-entrypoint.sh. Should I use this instead? Do I specify the Dockerfile inside .github/workflows/?

I tried to include as much relevant information as possible - comment if you need any other information please.

Screenshots from GitHub Actions

  • Initialize containers
  • Docker run ps <- docker ps showing name postgres
  • Run npm run build --if-present <- WHERE THE ISSUE IS OCCURING

build.sh

#!/bin/sh

# Import core db schema
./.deploy/postgres/restore-schema.sh

.deploy/postgres/restore-schema.sh

#!/bin/sh


docker exec -it postgres psql \
--username postgres \
--password dev \
coredb < .deploy/postgres/db-schema.sql

.github/workflows/test-api-gateway.yml

name: API Gateway CI

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

jobs:
  build:
    runs-on: ubuntu-latest
    services: # Serivce containers to run with `container-job`
      # Label used to access the service container
      postgres:
        # Docker Hub image
        image: postgres
        # Provide the password for postgres
        env:
          POSTGRES_USER: postgres
          POSTGRES_DB: coredb
          POSTGRES_PASSWORD: dev
        # Set health checks to wait until postgres has started
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5 
        ports:
          - 5432:5432

    strategy:
      matrix:
        node-version: [14.x]

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
    - run: docker ps
    - run: chmod +x build.sh .deploy/postgres/restore-schema.sh
    - run: npm ci
    - run: npm run build --if-present
    - run: npm test
like image 943
Bren Avatar asked Dec 21 '25 01:12

Bren


1 Answers

Try the --name option

options: >-
  --health-cmd pg_isready
  --health-interval 10s
  --health-timeout 5s
  --health-retries 5
  --name postgres 

https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idservices

jobs.<job_id>.services.options: Additional Docker container resource options. For a list of options, see "docker create options."

Another solution I've seen is using last created container

docker exec -it $(docker ps --latest --quiet) bash
like image 196
Dustin Butler Avatar answered Dec 23 '25 21:12

Dustin Butler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!