github-actionsoracle23ai

Cannot use gvenzl/oracle-free oracle free image in Github actions


I have a github actions repository and I would like it to connect with an Oracle database, containerised using Oracle Database Free Container / Docker images image. I would like to perform additional initialization of the database using a *.sql file under /container-entrypoint-initdb.d, as indicated in the image description. However, I get the following error when I try to build it. More specifically, I get the error:

Error 45 initializing SQL*Plus

My workflow.yml contains:

services:

      # Oracle service (label used to access the service container)
      oracle:

        # Docker Hub image (feel free to change the tag "latest" to any other available one)
        image: gvenzl/oracle-free:latest

        # Provide passwords and other environment variables to container
        env:
          ORACLE_RANDOM_PASSWORD: true
          APP_USER: my_user
          APP_USER_PASSWORD: password_i_should_change

        # Forward Oracle port
        ports:
          - 1521:1521

        # Provide healthcheck script options for startup
        options: >-
          --health-cmd healthcheck.sh
          --health-interval 10s
          --health-timeout 5s
          --health-retries 10
        volumes:
          - my-init.sql:/container-entrypoint-initdb.d/my-init.sql:ro

As stated in the image documentation.

On the other hand, the file /container-entrypoint-initdb.d/my-init.sql, included in the repository's root has the following lines:

alter session set container=freepdb1;
CREATE USER TEST IDENTIFIED BY test QUOTA UNLIMITED ON USERS;
GRANT CONNECT, RESOURCE TO TEST;

I do not find the problem. Any help is welcome.

Thanks

UPDATE. Following the first suggestion given by Azzem, I have tried with:

name: Build and test of Java Project
 
on: [push]
 
jobs:
 build:
   runs-on: ubuntu-latest
   timeout-minutes: 6
   services:

      # Oracle service (label used to access the service container)
      oracle:

        # Docker Hub image (feel free to change the tag "latest" to any other available one)
        image: gvenzl/oracle-free:latest
       
        # Provide passwords and other environment variables to container
        env:
          ORACLE_RANDOM_PASSWORD: true
          APP_USER: my_user
          APP_USER_PASSWORD: password_i_should_change

        # Forward Oracle port
        ports:
          - 1521:1521

        # Provide healthcheck script options for startup
        options: >-
          --name oracle
          --health-cmd healthcheck.sh
          --health-interval 10s
          --health-timeout 5s
          --health-retries 10
        # volumes:
        #   - my-init.sql:/container-entrypoint-initdb.d/my-init.sql:ro
   steps:
     - uses: actions/checkout@v2
     - name: Set up JDK 21
       uses: actions/setup-java@v1
       with:
         java-version: 21
     - name: Setup timezone
       uses: zcong1993/setup-timezone@master
       with:
          timezone: Europe/Madrid
     - name: Restart_container
        # Restart container after volumes have been checked out
       uses: docker://docker
       with:
          args: docker restart oracle
     - name: Create database
       run:  bash ./create_tables.sh
       shell: bash 
     - name: Build with Maven
       run: mvn -B test --file pom.xml

where the first lines of the create_tables.sh filer are:

export ORACLE_SID=FREE
export ORACLE_HOME=/opt/oracle/product/23ai/dbhomeFree
export ORAENV_ASK=NO
export PATH=$ORACLE_HOME/bin
$PATH/sqlplus -S my_user/password_i_should_change <<SQL
whenever sqlerror exit 2;
...

I have tried both with SID FREE and FREEPDB1, but I get the same error message line 5: /opt/oracle/product/23ai/dbhomeFree/bin/sqlplus: No such file or directory. Any idea? Thanks!

UPDATE 2: following steps at https://github.com/gvenzl/setup-oracle-free:

name: Build and test of Java Project
 
on: [push]
 
jobs:
 test:
  runs-on: ubuntu-latest
  timeout-minutes: 6
  steps:
     - uses: actions/checkout@v4
     - uses: gvenzl/setup-oracle-free@v1
       with:
          app-user: myUser
          app-user-password: myPwd
          startup-scripts: ${{ github.workspace }}/scripts
     - name: Set up JDK 21
       uses: actions/setup-java@v1
       with:
         java-version: 21
     - name: Setup timezone
       uses: zcong1993/setup-timezone@master
       with:
          timezone: Europe/Madrid
     - name: Build with Maven
       run: mvn -B test --file pom.xml

I got the error:

Error: can only create exec sessions on running containers: container state improper

UPDATE3: Thank you very much again. That works with docker. The point is that I need to create a database with concrete tables and, later, connect to it from JDBC. It seems that, after creating a user/password the login is denied, using:

url=jdbc:oracle:thin:@localhost:1521:FREE
user=myUser
password=myPwd

Although I create the user/password in the workflow.yml, it seems that it does not work. I also try to create them in the init.sql file (I have tried both with FREE and FREEPDB1 service names), but with no success:

ALTER SESSION SET CONTAINER=FREE;
CREATE USER myUser IDENTIFIED BY myPwd QUOTA UNLIMITED ON USERS;
GRANT CONNECT, RESOURCE TO myUser ;
CREATE SCHEMA  AUTHORIZATION myUser ; 
ALTER SESSION SET CURRENT_SCHEMA = myUser;

CREATE TABLE ...

Do you have any idea? Thank you very much


Solution

  • Apparently, this isn't possible with GHA service containers at the moment as the files from your repo will only be available after your checkout step whereas the service container will start before that. See this GitHub discussion How to mount file from repository to the service container's volume? for more details on this.

    There are some workarounds mentioned in there. The simple one would be to restart the service container after your checkout step. See this comment on the same thread.

    Alternatively, you may run container in the workflow using docker run or use docker compose for services.


    However, I found this https://github.com/gvenzl/setup-oracle-free action and it does everything you need. You don't need to set up services yourself. It's simpler and straightforward; and, is from the same author.

    I've tested it on ubuntu-latest runner and this is working fine for me.

    Here are the relevant workflow steps:

    - name: Checkout
      uses: actions/checkout@v4
    
    - name: Setup Oracle container
      uses: gvenzl/setup-oracle-free@v1
      with:
        container-runtime: docker
        app-user: app_user
        app-user-password: app_user_password
        setup-scripts: ${{ github.workspace }}/scripts
    
    - name: Check Oracle container logs for errors
      run: |
        docker container ls
        docker container logs oracledb | tee -a /tmp/oracledb.log
        if grep 'ERROR' /tmp/oracledb.log >/dev/null; then
          echo "[ERR] Something went wrong!"
          echo "[ERR] See above logs for more details."
          exit 1
        fi
    

    The checkout step should come first in the sequence of steps. For the input parameter setup-scripts, the respective directory should be available to be mounted as a volume.

    Currently, the container run logs are not being dumped to stdout/stderr. That's why I had to dump its logs to a file and filter those for any errors in a subsequent step.

    Apart from that, according to the docs, the url in your configuration i.e.:

    url=jdbc:oracle:thin:@localhost:1521:FREE
    

    should be:

    url=jdbc:oracle:thin:@localhost:1521/FREEPDB1
    

    As mentioned under ORACLE_DATABASE env var docs, FREEPDB1 is created by default. FREEPDB1 is a service name, not an SID - observe the difference between : and / in the url.


    Given above information, your create_database.sql script will look like this:

    ALTER SESSION SET CONTAINER = FREEPDB1;
    CREATE USER myUser IDENTIFIED BY myPwd QUOTA UNLIMITED ON USERS;
    
    GRANT CONNECT, RESOURCE TO myUser;
    GRANT CREATE TABLE TO myUser;
    ALTER SESSION SET CURRENT_SCHEMA = myUser;
    

    And, your complete .properties file will be:

    url=jdbc:oracle:thin:@localhost:1521/FREEPDB1
    user=myUser
    password=myPwd
    schema=myUser
    

    Observe that the database username/password (myUser/myPwd) is different from the application one (app-user/app-user-name i.e. app_user/app_user_password).


    Here are some miscellaneous points: