diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..2885679 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +.git +.github \ No newline at end of file diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..d7d6b44 --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,39 @@ +name: Build and Push Docker Image + +on: + push: + branches: + - main + +jobs: + build-and-push: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v5 + + - name: Allow Git safe directory + run: git config --global --add safe.directory /github/workspace + + - name: Log in to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ vars.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_TOKEN }} + + - name: Set image tags + id: vars + run: | + SHORT_SHA=$(git rev-parse --short HEAD) + echo "COMMIT_SHA=$SHORT_SHA" >> $GITHUB_ENV + echo "IMAGE_NAME=${{ vars.DOCKER_USERNAME }}/youtube-operational-api" >> $GITHUB_ENV + + - name: Build Docker image + run: | + docker build --build-arg VERSION=$COMMIT_SHA -t $IMAGE_NAME:latest -t $IMAGE_NAME:$COMMIT_SHA . + + - name: Push Docker image + run: | + docker push $IMAGE_NAME:latest + docker push $IMAGE_NAME:$COMMIT_SHA diff --git a/.gitignore b/.gitignore index 067f0a7..a156926 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .env -ytPrivate/keys.txt +ytPrivate/keys.txt \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index a63733d..0af5e45 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,18 @@ -FROM php:apache +FROM php:apache AS builder +RUN apt-get update && apt-get install -y git protobuf-compiler +COPY --from=composer/composer:latest-bin /composer /usr/bin/composer +COPY . /app +WORKDIR /app +RUN composer require google/protobuf \ + && protoc --php_out=proto/php/ --proto_path=proto/prototypes/ $(find proto/prototypes/ -type f) +# Final image +FROM php:apache RUN a2enmod rewrite - -# Copy application files into the container -COPY . /var/www/html/ - +COPY --from=builder /app /var/www/html/ # Replace `AllowOverride None` with `AllowOverride All` in `` in `/etc/apache2/apache2.conf`. RUN sed -ri -e 'N;N;N;s/(\n)(.*\n)(.*)AllowOverride None/\1\2\3AllowOverride All/;p;d;' /etc/apache2/apache2.conf - -COPY --from=composer/composer:latest-bin /composer /usr/bin/composer -RUN apt update -RUN apt install -y git protobuf-compiler -RUN composer require google/protobuf -RUN protoc --php_out=proto/php/ --proto_path=proto/prototypes/ $(find proto/prototypes/ -type f) - -CMD apachectl -D FOREGROUND +ARG VERSION +ENV VERSION=$VERSION +EXPOSE 80 +ENTRYPOINT ["apachectl", "-D", "FOREGROUND"] diff --git a/README.md b/README.md index 60820e0..669f5ee 100644 --- a/README.md +++ b/README.md @@ -120,25 +120,15 @@ If you want me to advertise your instance (if you have opened your port, and hav ## Run the API with Docker -1. Install [Docker](https://www.docker.com) and make sure that its daemon is running. - -2. Create a `.env` file and update it with your preferred port: - -```sh -cp .env.sample .env -``` - -3. Start the container with `docker-compose`: +Start the container using: ```sh -# start in the foreground -docker-compose up -# start in the background -docker-compose up -d +docker run -p 8080:80 benjaminloison/youtube-operational-api ``` -4. Verify that your API instance is reachable by trying to access: -- http://localhost:8080 (update preferred port if not 8080) +Then verify that the API is running by opening your browser or using an HTTP client: +[http://localhost:80](http://localhost:80) +> Note: If you prefer a different port, adjust the -p 8080:80 option accordingly. ## Contact: diff --git a/index.php b/index.php index 984ca6e..910f9f7 100644 --- a/index.php +++ b/index.php @@ -291,12 +291,17 @@ function share() { $hash"; + $version = $_ENV['VERSION']; + if (!$version) { + $ref = str_replace("\n", '', str_replace('ref: ', '', file_get_contents('.git/HEAD'))); + $hash = file_get_contents(".git/$ref"); + if ($hash !== false) { + $version = "version: $hash"; + } else { + $version = 'an unknown version.'; + } } + echo "

This instance (" . SERVER_NAME . ") uses $version"; ?>