Home > Back-end >  How to access GitHub repo files in a Docker containter creted by GitHub action?
How to access GitHub repo files in a Docker containter creted by GitHub action?

Time:08-03

I try to create a custom docker container in GitHub action, but I can't reach my repo files.

I have this .github/workflows/workflow.yml:

name: Workflow

on:
  push:
    branches:
      - my-test-branch

jobs:
  testjob:
    runs-on: ubuntu-20.04
    name: My test job
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: My Custom Action
        uses: ./.github/actions/my-custom-action

The .github/actions/my-custom-action/action.yml file contains this:

name: Docker conatiner test
description: Start container
runs:
  using: docker
  image: ./php.dockerfile

In the .github/actions/my-custom-action/php.dockerfile contains this:

FROM php:8.1-fpm-alpine

WORKDIR /var/www/html

# here I want to use COPY or ADD to attach directory into the Docker conatiner

I checked GitHub documentation, but nothing found about this topic. I browsed several Stackoverflow and GitHub issues, tried some tips, but nothing useful.

How can I access to repository files from the Docker container?

CodePudding user response:

I never seen image: ./php.dockerfile in a GitHub Actions workflow file

You can use a custom image that you have first built and published, for instance to a Docker public registry (like grc.io):

jobs:
  my_first_job:
    steps:
      - name: My first step
        uses: docker://gcr.io/cloud-builders/gradle

But the idea that a GitHub workflow would have to build an image first in order to test your program seems counter-productive.

You can run your existing custom image, mounting (instead of Dockerfile ADD and COPY) checked out folders to your running custom container.
It uses jobs.<job_id>.container

name: CI
on:
  push:
    branches: [ main ]
jobs:
  container-test-job:
    runs-on: ubuntu-latest
    container:
      image: node:14.16
      env:
        NODE_ENV: development
      ports:
        - 80
      volumes:
        - my_docker_volume:/volume_mount
      options: --cpus 1
    steps:
      - name: Check for dockerenv file
        run: (ls /.dockerenv && echo Found dockerenv) || (echo No dockerenv)

You would mount volumes in a container

Mounting volumes in a container

volumes:
  - my_docker_volume:/volume_mount
  - /data/my_data
  - /source/directory:/destination/directory  <= of interest in your case

But all that assumes you already built and published your custom image, for your GitHub Action to use at runtime.

  • Related