From e0d44ff6c11e10fa6d67931104ccb4fd2562955d Mon Sep 17 00:00:00 2001 From: Quinten Stokkink Date: Thu, 22 Aug 2024 10:57:26 +0200 Subject: [PATCH] Added Docker build action Add docker build with GH action Set docker filename on build Add docker tag based on branch and version Update docker/metadata-action to v5 Add tag version and support workflow dispatch Set fetc-depth to 0 on docker build Update builder_docker github action Set fetch depth to zero Define version of actions on build_docker workflow Fix version issue Pass docker args Add an echo log on dockerfile for repo and branch Fix build args GIT_REPO URL Set pyvipv8 on docker pythonpath Update compose.yml file Always use the full tag for version Remove extra metadata tags; instead use branch and version Remove configuration file --- .github/workflows/build_docker.yml | 70 +++++++++++++++++++++++++----- build/docker/build.Dockerfile | 47 ++++++++++---------- build/docker/compose.yml | 11 +++++ 3 files changed, 94 insertions(+), 34 deletions(-) create mode 100644 build/docker/compose.yml diff --git a/.github/workflows/build_docker.yml b/.github/workflows/build_docker.yml index e4e65bd492..0c031c9119 100644 --- a/.github/workflows/build_docker.yml +++ b/.github/workflows/build_docker.yml @@ -1,21 +1,67 @@ -name: Build Docker Image +name: Publish Docker Image -on: workflow_dispatch +on: + workflow_dispatch: + push: + branches: + - 'main' + - 'docker' + tags: + - 'v*' + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} jobs: - build: + build-and-push-image: runs-on: ubuntu-latest + + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + attestations: write + id-token: write + steps: - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Build and export + - name: Checkout repository + uses: actions/checkout@v4 + with: + ref: ${{ github.ref_name }} + fetch-depth: 0 + submodules: 'true' + fetch-tags: 'true' + + - name: Log in to the Container registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract Git Tag Version + id: git_tag_version + run: | + echo "VERSION=$(git describe --tags)" >> $GITHUB_ENV + + - name: Build and push Docker image + id: push uses: docker/build-push-action@v6 with: - tags: tribler:latest - outputs: type=docker,dest=/tmp/tribler.tar + context: . file: build/docker/build.Dockerfile - - name: Upload artifact - uses: actions/upload-artifact@v4 + push: true + tags: | + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.VERSION }} + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }} + build-args: | + GIT_BRANCH=${{ github.ref_name }} + GIT_REPO=https://github.com/${{ github.repository }} + + - name: Generate artifact attestation + uses: actions/attest-build-provenance@v1 with: - name: tribler - path: /tmp/tribler.tar + subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}} + subject-digest: ${{ steps.push.outputs.digest }} + push-to-registry: true diff --git a/build/docker/build.Dockerfile b/build/docker/build.Dockerfile index aeaf96b769..050b940428 100644 --- a/build/docker/build.Dockerfile +++ b/build/docker/build.Dockerfile @@ -5,30 +5,33 @@ RUN apt-get update \ && apt-get install -y --no-install-recommends libsodium23=1.0.18-1 \ && rm -rf /var/lib/apt/lists/* -# Then, install pip dependencies so that it can be cached and does not -# need to be built every time the source code changes. -# This reduces the docker build time. -COPY ./pyipv8/requirements.txt /app/tribler/pyipv8/requirements.txt -COPY ./requirements.txt /app/tribler/requirements.txt -RUN pip3 install -r /app/tribler/requirements.txt - -RUN useradd -ms /bin/bash user - -# Create default state and download directories and set the permissions -RUN chown -R user:user /app -RUN mkdir /state /downloads && chown -R user:user /state /downloads +# Install Xvfb for headless GUI +RUN apt-get update -y \ + && apt-get -y install \ + xvfb nodejs npm git \ + && rm -rf /var/lib/apt/lists/* /var/cache/apt/* + +# Set up a user in the container +RUN useradd -ms /bin/bash --home-dir /home/user user +USER user -# Copy the source code and set the working directory -COPY ./src /app/tribler/src/ -WORKDIR /app/tribler/ +# Clone the repository with arguments +ARG GIT_REPO=${GIT_REPO:-"https://github.com/tribler/tribler.git"} +ARG GIT_BRANCH=${GIT_BRANCH:-"main"} +RUN echo "Cloning $GIT_REPO on branch $GIT_BRANCH" +RUN git clone --recursive --branch "$GIT_BRANCH" "$GIT_REPO" /home/user/tribler -ENV CORE_API_PORT=20100 -ENV TSTATEDIR=/state +# Install NPM dependencies +WORKDIR /home/user/tribler/src/tribler/ui +RUN npm install \ + && npm run build -VOLUME /state -VOLUME /downloads -COPY ./build/docker/configuration.json /state/git/configuration.json +# Install Python dependencies +WORKDIR /home/user/tribler +RUN pip3 install -r requirements.txt -USER user +# Set IPv8 on pythonpath +ENV PYTHONPATH=pyipv8 -CMD exec python3 /app/tribler/src/run_tribler.py +# Run the application using Xvfb +CMD xvfb-run python3 src/run_tribler.py diff --git a/build/docker/compose.yml b/build/docker/compose.yml new file mode 100644 index 0000000000..a25c6a8590 --- /dev/null +++ b/build/docker/compose.yml @@ -0,0 +1,11 @@ + +services: + tribler: + image: ghcr.io/tribler/tribler:main + network_mode: host + environment: + CORE_API_PORT: 8085 + CORE_API_KEY: "changeme" + volumes: + - ~/.Tribler/git:/home/user/.Tribler + - ~/Downloads/TriblerDownloads:/home/user/Downloads/TriblerDownloads