2

I just begin in the devops world and for my project, I have to realize a classic CI/CD pipeline using github actions. For my project, I made a simple website where a user can bet winning for some football games I entered in my DB.

My project uses MySQL for the database, Nodejs backend and Nextjs front end. I made a docker compose file to build everything together. Finally, I wrote some tests (only for the server for the moment). Here is the deal :

After I run docker-compose up locally, running 'npm test' runs my tests. The first test is to check the connection to the database. Everything works fine.

But now I want to do the same, but with the github actions, so I cannot push a commit if this doesn't pass my tests of course. However, on github, my first test (connection to the DB) never works, even if I run docker-compose before.

name: Docker Image CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:

  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Build the Docker images 
      working-directory: ./project
      run: docker-compose build

    - name: Run the docker compose
      working-directory: ./project
      run: docker-compose up -d

    - name: install server dependencies
      working-directory: ./project/server
      run: npm ci

    - name: run servers test
      working-directory: ./project/server
      run: npm test

    - name: finish docker down
      working-directory: ./project
      run: docker-compose down

And here is my error: Github action error

Also, and I think it has an important role of course, when I run all this locally, I have my '.env' file with mysql passwords etc.. and I don't understand how my app can get these information on github.

Romano
  • 35
  • 4

1 Answers1

0

I finally made it works, here is how I did. Please, feel free to give me advices if you have to do it better, thank you in advance.

So my problem came from the fact that imagined github action like a VM where everything happens as on my local machine. But it's not really the case I think now because running docker compose up -d in a step of my github action doesn't create a mysql instance with my db that I can now access as I do on my PC. I can't because if I want to use mysql for my actions, I need to use the one on the machine I declare at the beginning of my github action with runs-on: ubuntu-latest, or the container I made will just conflict with it.

After some research, I found out the mysql service doesn't start by default with github actions (see :GitHub Actions Breaking Change: Ubuntu virtual environments will no longer start the MySQL service automatically). So, in my yml file I deleted the docker compose part and add the command line to start mysql on the ubuntu machine and load my sql init file. Here is my github action's code :

    steps:
    - uses: actions/checkout@v3

    - name: start mysql
      run: sudo /etc/init.d/mysql start

    - name: Create test database
      run: mysql -uroot -proot < project/db/init.sql

    - name: install server dependencies
      working-directory: ./project/server
      run: npm ci

    - name: run servers test
      working-directory: ./project/server
      run: npm test

For testing, I am using jest-mysql, which uses mysql instead of mysql2 and got me the error : ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client. I ended up by adding the following couple lines in my init.sql according to this answer on another stackoverflow question, and changed my config file's username and password according to it.

CREATE USER 'sqluser'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'sqluser'@'%';
FLUSH PRIVILEGES;

Now it is finally working, my tests works well but as you noticed, I am pretty sure there are better way to come to this end. This is a little private school project so I am allowed to make mistake, but I would love to learn how to avoid them for the next time!

Thank you for your time and merry Christmas!

Romano
  • 35
  • 4