Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub Action: How do I run commands inside a docker container

Just started learning about CI/CD today. Didn't know what docker was previous to today. I was able to get CD working on CircleCI with my Unity project. All of the run commands were happening inside the docker container.

version: 2.1
executors:
  unity:
    docker:
      # https://hub.docker.com/r/gableroux/unity3d/tags
      - image: gableroux/unity3d:2018.2.21f1
jobs:
  build-test:
    executor: unity
    steps:
      - checkout

      # Install zip
      - run: apt-get update
      - run: apt-get install zip -y

      # Decrypt the license file
      - run: openssl aes-256-cbc -d -in .circleci/Unity_v2018.x.ulf.enc -k ${UNITY_LICENSE_DECRYPT_KEY} >> .circleci/Unity_v2018.x.ulf

      # Activate unity
      - run: /opt/Unity/Editor/Unity -quit -batchmode -nographics -silent-crashes -logFile -manualLicenseFile .circleci/Unity_v2018.x.ulf || exit 0

      # Build Windows and OSX
      - run: /opt/Unity/Editor/Unity -quit -batchmode -nographics -silent-crashes -noUpm -logFile -projectPath . -buildWindows64Player ./bin-win64/CISample.exe
      - run: /opt/Unity/Editor/Unity -quit -batchmode -nographics -silent-crashes -noUpm -logFile -projectPath . -buildOSXUniversalPlayer ./bin-osx/CISample.app      

      # Zip builds
      - run: zip -r CISampleWin64Binary.zip ./bin-win64
      - run: zip -r CISampleOSXBinary.zip ./bin-osx

      # Store builds
      - store_artifacts:
          path: ./CISampleWin64Binary.zip
      - store_artifacts:
          path: ./CISampleOSXBinary.zip

workflows:
  version: 2
  build:
    jobs:
      - build-test

Can't figure out how to do this with GitHub Actions. I can run a docker image but I can't figure out how to run commands within it. All of my commands are run at Ubuntu VM level. My test is to run a ls command to see if it's running from the docker container which it is not.

Here a few of the things I have tried.

name: CI
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - run: |
          docker run gableroux/unity3d:2018.2.21f1 bash
          ls
name: CI
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: docker://gableroux/unity3d:2018.2.21f1
      - run: ls
like image 225
Matt Thompson Avatar asked Nov 19 '19 09:11

Matt Thompson


People also ask

How do I run a command inside a running docker container?

If you need to run a command inside a running Docker container, but don't need any interactivity, use the docker exec command without any flags: docker exec container-name tail /var/log/date.

Do GitHub actions run in a container?

Overview. GitHub Actions has a relatively little known feature where you can run jobs in a container, specifically a Docker container. I liken it to delegating the entire job to the container, so every step that would run in the job will instead run in the container, including the clone/checkout of the repository.


1 Answers

Figured it out. You can specify container for a job. My full workflow looks like this:

name: CI
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    container: gableroux/unity3d:2018.2.21f1
    steps:
        - name: Checkout Project
          uses: actions/checkout@v1

        - name: Intall Zip
          run: | 
            apt-get update
            apt-get install zip -y

        - name: Decrypt the license file
          run: openssl aes-256-cbc -d -in .github/Unity_v2018.x.ulf.enc -k ${{ secrets.UNITY_LICENSE_DECRYPT_KEY }} >> .github/Unity_v2018.x.ulf

        # Activate unity
        - name: Activate Unity
          run: /opt/Unity/Editor/Unity -quit -batchmode -nographics -silent-crashes -logFile -manualLicenseFile .github/Unity_v2018.x.ulf || exit 0

        # Build Windows and OSX
        - name: Build Windows Player
          run: /opt/Unity/Editor/Unity -quit -batchmode -nographics -silent-crashes -noUpm -logFile -projectPath . -buildWindows64Player ./bin-win64/CISample.exe
        - name: Build OSX Player
          run: /opt/Unity/Editor/Unity -quit -batchmode -nographics -silent-crashes -noUpm -logFile -projectPath . -buildOSXUniversalPlayer ./bin-osx/CISample.app      

        - name: Zip Builds
          run: |
            zip -r CISampleWin64Binary.zip ./bin-win64
            zip -r CISampleOSXBinary.zip ./bin-osx

        - name: Archive Windows Build
          uses: actions/upload-artifact@v1
          with:
            name: CISampleWin64Binary
            path: ./CISampleWin64Binary.zip

        - name: Archive Mac Build
          uses: actions/upload-artifact@v1
          with:
            name: CISampleOSXBinary
            path: ./CISampleOSXBinary.zip
like image 93
Matt Thompson Avatar answered Oct 22 '22 10:10

Matt Thompson