From 67ca3b312e0d95104a97348c83d6a1d193fefbaf Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Tue, 28 Nov 2023 11:30:23 +0000 Subject: [PATCH 1/4] ci: [#375] add compose varaible to change API BASE URL at runtime For development, the API base URL is taken from the `.env` file. ``` API_BASE_URL=http://localhost:3001/v1 ``` For production, you can use the pre-build docker image which uses the default value set at building time (`http://localhost:3001/v1`). If you want, you can overwrite it when you run the new app with the env var `NUXT_PUBLIC_API_BASE` but it only works if you are serving the app with node. It doesn't work for static web app. You new to rebuild the app with your value for the env var `API_BASE_URL` so that it's included in the packaged app. --- compose.yaml | 1 + contrib/dev-tools/container/docker-run-local.sh | 1 + contrib/dev-tools/container/docker-run-public.sh | 1 + 3 files changed, 3 insertions(+) diff --git a/compose.yaml b/compose.yaml index 3464ae2c..84d1c359 100644 --- a/compose.yaml +++ b/compose.yaml @@ -11,6 +11,7 @@ services: tty: true environment: - USER_ID=${USER_ID} + - NUXT_PUBLIC_API_BASE=${TORRUST_INDEX_GUI_API_BASE_URL:-http://localhost:3001/v1} ports: - 3000:3000 - 24678:24678 diff --git a/contrib/dev-tools/container/docker-run-local.sh b/contrib/dev-tools/container/docker-run-local.sh index 7e59ec22..f6728afb 100755 --- a/contrib/dev-tools/container/docker-run-local.sh +++ b/contrib/dev-tools/container/docker-run-local.sh @@ -4,5 +4,6 @@ mkdir -p ./storage/index-gui/lib/ ./storage/index-gui/log/ ./storage/index-gui/e docker run -it \ --env USER_ID="$(id -u)" \ + --env NUXT_PUBLIC_API_BASE="http://localhost:3001/v1" \ --publish 3000:3000/tcp \ torrust-index-gui:release diff --git a/contrib/dev-tools/container/docker-run-public.sh b/contrib/dev-tools/container/docker-run-public.sh index 48cc9883..fecfa0dc 100755 --- a/contrib/dev-tools/container/docker-run-public.sh +++ b/contrib/dev-tools/container/docker-run-public.sh @@ -4,5 +4,6 @@ mkdir -p ./storage/index-gui/log/ docker run -it \ --env USER_ID="$(id -u)" \ + --env NUXT_PUBLIC_API_BASE="http://localhost:3001/v1" \ --publish 3000:3000/tcp \ torrust/index-gui:latest From 93bdab9ae1edda6ffc637e25ddcb29651276066c Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Tue, 28 Nov 2023 11:42:53 +0000 Subject: [PATCH 2/4] ci: [#375] add debug stage to Containerfile It will allow us to use the same strcuture as the workflow to publish the docker image in the Index and Tracker projects. --- Containerfile | 7 +++++++ .../dev-tools/container/docker-build-debug.sh | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100755 contrib/dev-tools/container/docker-build-debug.sh diff --git a/Containerfile b/Containerfile index 0e738d40..3189c611 100644 --- a/Containerfile +++ b/Containerfile @@ -54,6 +54,13 @@ VOLUME ["/var/log/torrust/index-gui"] ENV RUNTIME="runtime" ENTRYPOINT ["/usr/local/bin/entry.sh"] +## Torrust-Index-GUI (debug) +FROM runtime as debug +ENV RUNTIME="debug" +COPY --from=test /app/.output /app/.output +RUN env +CMD ["sh"] + ## Torrust-Index-GUI (release) (default) FROM runtime as release diff --git a/contrib/dev-tools/container/docker-build-debug.sh b/contrib/dev-tools/container/docker-build-debug.sh new file mode 100755 index 00000000..a35f1636 --- /dev/null +++ b/contrib/dev-tools/container/docker-build-debug.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +echo "Building docker image ..." + +USER_ID=${USER_ID:-1001} +TORRUST_INDEX_GUI_API_BASE_URL=${TORRUST_INDEX_GUI_API_BASE_URL:-http://localhost:3001/v1} + +echo "USER_ID: $USER_ID" +echo "TORRUST_INDEX_GUI_API_BASE_URL: $TORRUST_INDEX_GUI_API_BASE_URL" + +docker build \ + --build-arg USER_ID="$USER_ID" \ + --build-arg API_BASE_URL="$TORRUST_INDEX_GUI_API_BASE_URL" \ + --target debug \ + --tag torrust-index-gui:debug \ + --file Containerfile . From 674cd19e0066481e503317a2cde10a47dcdea85b Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Tue, 28 Nov 2023 11:52:20 +0000 Subject: [PATCH 3/4] ci: [#375] add container workflow from Index --- .github/workflows/container.yaml | 178 +++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 .github/workflows/container.yaml diff --git a/.github/workflows/container.yaml b/.github/workflows/container.yaml new file mode 100644 index 00000000..0cf75f77 --- /dev/null +++ b/.github/workflows/container.yaml @@ -0,0 +1,178 @@ +name: Container + +on: + push: + branches: + - "develop" + - "main" + - "releases/**/*" + pull_request: + branches: + - "develop" + - "main" + +env: + CARGO_TERM_COLOR: always + +jobs: + test: + name: Test (Docker) + runs-on: ubuntu-latest + + strategy: + matrix: + target: [debug, release] + + steps: + - id: setup + name: Setup Toolchain + uses: docker/setup-buildx-action@v3 + + - id: build + name: Build + uses: docker/build-push-action@v5 + with: + file: ./Containerfile + push: false + load: true + target: ${{ matrix.target }} + tags: torrust-index-gui:local + cache-from: type=gha + cache-to: type=gha + + - id: inspect + name: Inspect + run: docker image inspect torrust-index-gui:local + + - id: checkout + name: Checkout Repository + uses: actions/checkout@v4 + + - id: compose + name: Compose + run: docker compose build + + context: + name: Context + needs: test + runs-on: ubuntu-latest + + outputs: + continue: ${{ steps.check.outputs.continue }} + type: ${{ steps.check.outputs.type }} + version: ${{ steps.check.outputs.version }} + + steps: + - id: check + name: Check Context + run: | + if [[ "${{ github.repository }}" == "torrust/torrust-index-gui" ]]; then + if [[ "${{ github.event_name }}" == "push" ]]; then + if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then + + echo "type=development" >> $GITHUB_OUTPUT + echo "continue=true" >> $GITHUB_OUTPUT + echo "On \`main\` Branch, Type: \`development\`" + + elif [[ "${{ github.ref }}" == "refs/heads/develop" ]]; then + + echo "type=development" >> $GITHUB_OUTPUT + echo "continue=true" >> $GITHUB_OUTPUT + echo "On \`develop\` Branch, Type: \`development\`" + + elif [[ $(echo "${{ github.ref }}" | grep -P '^(refs\/heads\/releases\/)(v)(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$') ]]; then + + version=$(echo "${{ github.ref }}" | sed -n -E 's/^(refs\/heads\/releases\/)//p') + echo "version=$version" >> $GITHUB_OUTPUT + echo "type=release" >> $GITHUB_OUTPUT + echo "continue=true" >> $GITHUB_OUTPUT + echo "In \`releases/$version\` Branch, Type: \`release\`" + + else + echo "Not Correct Branch. Will Not Continue" + fi + else + echo "Not a Push Event. Will Not Continue" + fi + else + echo "On a Forked Repository. Will Not Continue" + fi + + publish_development: + name: Publish (Development) + environment: dockerhub-torrust + needs: context + if: needs.context.outputs.continue == 'true' && needs.context.outputs.type == 'development' + runs-on: ubuntu-latest + + steps: + - id: meta + name: Docker Meta + uses: docker/metadata-action@v5 + with: + images: | + "${{ vars.DOCKER_HUB_USERNAME }}/${{vars.DOCKER_HUB_REPOSITORY_NAME }}" + tags: | + type=ref,event=branch + + - id: login + name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ vars.DOCKER_HUB_USERNAME }} + password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} + + - id: setup + name: Setup Toolchain + uses: docker/setup-buildx-action@v3 + + - name: Build and push + uses: docker/build-push-action@v5 + with: + file: ./Containerfile + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha + + publish_release: + name: Publish (Release) + environment: dockerhub-torrust + needs: context + if: needs.context.outputs.continue == 'true' && needs.context.outputs.type == 'release' + runs-on: ubuntu-latest + + steps: + - id: meta + name: Docker Meta + uses: docker/metadata-action@v5 + with: + images: | + "${{ vars.DOCKER_HUB_USERNAME }}/${{vars.DOCKER_HUB_REPOSITORY_NAME }}" + tags: | + type=semver,value=${{ needs.context.outputs.version }},pattern={{raw}} + type=semver,value=${{ needs.context.outputs.version }},pattern={{version}} + type=semver,value=${{ needs.context.outputs.version }},pattern=v{{major}} + type=semver,value=${{ needs.context.outputs.version }},pattern={{major}}.{{minor}} + + - id: login + name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ vars.DOCKER_HUB_USERNAME }} + password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} + + - id: setup + name: Setup Toolchain + uses: docker/setup-buildx-action@v3 + + - name: Build and push + uses: docker/build-push-action@v5 + with: + file: ./Containerfile + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha From 3e1ef2ff4b01b5b12dc4e6117589f562311403ab Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Tue, 28 Nov 2023 11:56:38 +0000 Subject: [PATCH 4/4] ci: [#375] delete old workflows for containers They were replace by a single workflow: `container.yaml`, folowwing the same structure as in the Tracker and Index. --- .github/workflows/publish_docker_image.yml | 64 ---------------------- .github/workflows/test_docker_build.yml | 32 ----------- 2 files changed, 96 deletions(-) delete mode 100644 .github/workflows/publish_docker_image.yml delete mode 100644 .github/workflows/test_docker_build.yml diff --git a/.github/workflows/publish_docker_image.yml b/.github/workflows/publish_docker_image.yml deleted file mode 100644 index ae883f81..00000000 --- a/.github/workflows/publish_docker_image.yml +++ /dev/null @@ -1,64 +0,0 @@ -name: Publish Docker Image - -on: - push: - branches: - - "main" - - "develop" - tags: - - "v*" - -jobs: - check-secret: - runs-on: ubuntu-latest - environment: dockerhub-torrust - outputs: - publish: ${{ steps.check.outputs.publish }} - steps: - - id: check - env: - DOCKER_HUB_USERNAME: "${{ secrets.DOCKER_HUB_USERNAME }}" - if: "${{ env.DOCKER_HUB_USERNAME != '' }}" - run: echo "publish=true" >> $GITHUB_OUTPUT - - dockerhub: - needs: check-secret - if: needs.check-secret.outputs.publish == 'true' - runs-on: ubuntu-latest - environment: dockerhub-torrust - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Docker meta - id: meta - uses: docker/metadata-action@v5 - with: - images: | - # For example: torrust/index-frontend - "${{ secrets.DOCKER_HUB_USERNAME }}/${{secrets.DOCKER_HUB_REPOSITORY_NAME }}" - tags: | - type=ref,event=branch - type=ref,event=pr - type=semver,pattern={{version}} - type=semver,pattern={{major}}.{{minor}} - - - name: Login to Docker Hub - uses: docker/login-action@v3 - with: - username: ${{ secrets.DOCKER_HUB_USERNAME }} - password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - - name: Build and push - uses: docker/build-push-action@v5 - with: - context: . - file: ./Containerfile - push: ${{ github.event_name != 'pull_request' }} - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} - cache-from: type=gha - cache-to: type=gha,mode=max diff --git a/.github/workflows/test_docker_build.yml b/.github/workflows/test_docker_build.yml deleted file mode 100644 index 72c48b1c..00000000 --- a/.github/workflows/test_docker_build.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: Test Docker build - -on: - push: - pull_request: - -jobs: - test: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - - name: Build docker image - uses: docker/build-push-action@v5 - with: - context: . - file: ./Containerfile - push: false - cache-from: type=gha - cache-to: type=gha,mode=max - - - id: checkout - name: Checkout Repository - uses: actions/checkout@v4 - - - id: compose - name: Compose - run: docker compose build -