From e05c35a3eaa613d5b9dd115731e5b347f02368c5 Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Fri, 8 Mar 2024 22:44:25 +0100 Subject: [PATCH 01/14] added container building workflow --- .github/workflows/publish-image.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .github/workflows/publish-image.yml diff --git a/.github/workflows/publish-image.yml b/.github/workflows/publish-image.yml new file mode 100644 index 0000000..e516e54 --- /dev/null +++ b/.github/workflows/publish-image.yml @@ -0,0 +1,17 @@ +name: Publish Docker image to GHCR + +on: + push + +jobs: + build_and_publish: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Login to GHCR.io + run: echo "${{ secret.GHCR_PAT }}" | docker login --username ${{ secret.GHCR_USER }} --password-stdin ghcr.io + - name: Build and tag image + run: docker build . -t ghcr.io/${{ secret.GHCR_USER }}/netbox-zabbix-sync:latest + - name: Push image to GHCR.io + run: docker push ghcr.io/${{ secret.GHCR_USER }}/netbox-zabbix-sync:latest + From d46b749af0d95040b933a5dbe9cef5306926ed4c Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Fri, 8 Mar 2024 22:48:57 +0100 Subject: [PATCH 02/14] corrected typos --- .github/workflows/publish-image.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/publish-image.yml b/.github/workflows/publish-image.yml index e516e54..d7b29a9 100644 --- a/.github/workflows/publish-image.yml +++ b/.github/workflows/publish-image.yml @@ -9,9 +9,9 @@ jobs: steps: - uses: actions/checkout@v4 - name: Login to GHCR.io - run: echo "${{ secret.GHCR_PAT }}" | docker login --username ${{ secret.GHCR_USER }} --password-stdin ghcr.io + run: echo "${{ secrets.GHCR_PAT }}" | docker login --username ${{ secrets.GHCR_USER }} --password-stdin ghcr.io - name: Build and tag image - run: docker build . -t ghcr.io/${{ secret.GHCR_USER }}/netbox-zabbix-sync:latest + run: docker build . -t ghcr.io/${{ secrets.GHCR_USER }}/netbox-zabbix-sync:latest - name: Push image to GHCR.io - run: docker push ghcr.io/${{ secret.GHCR_USER }}/netbox-zabbix-sync:latest + run: docker push ghcr.io/${{ secrets.GHCR_USER }}/netbox-zabbix-sync:latest From 7c988f9ff850768ac5deb8ecbaccae7a94b48d11 Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Fri, 8 Mar 2024 22:51:53 +0100 Subject: [PATCH 03/14] changed over to checkout@v3 --- .github/workflows/publish-image.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/publish-image.yml b/.github/workflows/publish-image.yml index d7b29a9..1a56a0f 100644 --- a/.github/workflows/publish-image.yml +++ b/.github/workflows/publish-image.yml @@ -7,7 +7,7 @@ jobs: build_and_publish: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v3 - name: Login to GHCR.io run: echo "${{ secrets.GHCR_PAT }}" | docker login --username ${{ secrets.GHCR_USER }} --password-stdin ghcr.io - name: Build and tag image From 33cf3e5358dfb952c4392193b7a6af3e3a570539 Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Fri, 8 Mar 2024 22:53:34 +0100 Subject: [PATCH 04/14] changed back to checkout@v4 and commited Dockerfile --- .github/workflows/publish-image.yml | 2 +- Dockerfile | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 Dockerfile diff --git a/.github/workflows/publish-image.yml b/.github/workflows/publish-image.yml index 1a56a0f..d7b29a9 100644 --- a/.github/workflows/publish-image.yml +++ b/.github/workflows/publish-image.yml @@ -7,7 +7,7 @@ jobs: build_and_publish: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Login to GHCR.io run: echo "${{ secrets.GHCR_PAT }}" | docker login --username ${{ secrets.GHCR_USER }} --password-stdin ghcr.io - name: Build and tag image diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fa8d9c4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +# syntax=docker/dockerfile:1 +FROM python:3.12-alpine +RUN mkdir -p /opt/netbox-zabbix +COPY . /opt/netbox-zabbix +WORKDIR /opt/netbox-zabbix +RUN if ! [ -f ./config.py ]; then cp ./config.py.example ./config.py; fi +RUN pip install -r ./requirements.txt +ENTRYPOINT ["python"] +CMD ["/opt/netbox-zabbix/netbox_zabbix_sync.py", "-v"] From dcd84e836b846a68ae4e1af99ff0853e22ec6b7b Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Mon, 11 Mar 2024 11:03:37 +0100 Subject: [PATCH 05/14] Chained in quality check --- .github/workflows/publish-image.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/publish-image.yml b/.github/workflows/publish-image.yml index d7b29a9..670d9b0 100644 --- a/.github/workflows/publish-image.yml +++ b/.github/workflows/publish-image.yml @@ -4,6 +4,8 @@ on: push jobs: + test_quality: + uses: ./.github/workflows/quality.yml build_and_publish: runs-on: ubuntu-latest steps: From 5922d3e8ae42115e0b09e398bb29d6f3e8296ebc Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Mon, 11 Mar 2024 11:06:31 +0100 Subject: [PATCH 06/14] allow call from another workflow --- .github/workflows/quality.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/quality.yml b/.github/workflows/quality.yml index a56c573..8af4e9c 100644 --- a/.github/workflows/quality.yml +++ b/.github/workflows/quality.yml @@ -1,7 +1,7 @@ --- name: Pylint -on: [push] +on: ["push","workflow_call"] jobs: build: From bf325c6839de44b7284f336da633a91a857ade78 Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Wed, 13 Mar 2024 11:38:07 +0100 Subject: [PATCH 07/14] testing workflow --- .github/workflows/publish-image.yml | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/.github/workflows/publish-image.yml b/.github/workflows/publish-image.yml index 670d9b0..78b8b64 100644 --- a/.github/workflows/publish-image.yml +++ b/.github/workflows/publish-image.yml @@ -1,7 +1,16 @@ -name: Publish Docker image to GHCR +name: Publish Docker image to GHCR on a new version on: - push + push: + branches: + - main + - dockertest +# tags: +# - [0-9]+.* + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} jobs: test_quality: @@ -10,10 +19,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - name: Login to GHCR.io - run: echo "${{ secrets.GHCR_PAT }}" | docker login --username ${{ secrets.GHCR_USER }} --password-stdin ghcr.io + - name: Login to ghcr.io + run: echo "${{ secrets.GHCR_PAT }}" | docker login --username ${{ github.actor }} --password-stdin ${{ env.REGISTRY }} - name: Build and tag image - run: docker build . -t ghcr.io/${{ secrets.GHCR_USER }}/netbox-zabbix-sync:latest - - name: Push image to GHCR.io - run: docker push ghcr.io/${{ secrets.GHCR_USER }}/netbox-zabbix-sync:latest + run: docker build . -t ${{ env.REGISTRY}}/${{ env.IMAGE_NAME }}:latest + - name: Push image to ghcr.io + run: docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest From 3a39c314bede7d7f47a580865c170f63e55bbd72 Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Wed, 13 Mar 2024 11:42:28 +0100 Subject: [PATCH 08/14] removed `on: push` from pylint --- .github/workflows/quality.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/quality.yml b/.github/workflows/quality.yml index 8af4e9c..7b01f6f 100644 --- a/.github/workflows/quality.yml +++ b/.github/workflows/quality.yml @@ -1,7 +1,8 @@ --- -name: Pylint +name: Pylint Quality control -on: ["push","workflow_call"] +on: + workflow_call jobs: build: From 4b7f3ec0b9da04c92d5d77ad5caf796d417c8252 Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Wed, 13 Mar 2024 11:47:45 +0100 Subject: [PATCH 09/14] try a different way of publishing --- .github/workflows/publish-image.yml | 35 ++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/.github/workflows/publish-image.yml b/.github/workflows/publish-image.yml index 78b8b64..e32d212 100644 --- a/.github/workflows/publish-image.yml +++ b/.github/workflows/publish-image.yml @@ -19,10 +19,33 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - name: Login to ghcr.io - run: echo "${{ secrets.GHCR_PAT }}" | docker login --username ${{ github.actor }} --password-stdin ${{ env.REGISTRY }} - - name: Build and tag image - run: docker build . -t ${{ env.REGISTRY}}/${{ env.IMAGE_NAME }}:latest - - name: Push image to ghcr.io - run: docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest +# - name: Login to ghcr.io +# run: echo "${{ secrets.GHCR_PAT }}" | docker login --username ${{ github.actor }} --password-stdin ${{ env.REGISTRY }} +# - name: Build and tag image +# run: docker build . -t ${{ env.REGISTRY}}/${{ env.IMAGE_NAME }}:latest +# - name: Push image to ghcr.io +# run: docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest + + - name: Log in to the Container registry + uses: docker/login-action@v2 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GHCR_PAT }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v4 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=semver,pattern={{ version }} + + - name: Build and push Docker image + uses: docker/build-push-action@v4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} From 661ce8828743a76c51a0e5466b521a0e12eec50a Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Wed, 13 Mar 2024 11:50:43 +0100 Subject: [PATCH 10/14] updated versions --- .github/workflows/publish-image.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/publish-image.yml b/.github/workflows/publish-image.yml index e32d212..6252706 100644 --- a/.github/workflows/publish-image.yml +++ b/.github/workflows/publish-image.yml @@ -27,7 +27,7 @@ jobs: # run: docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest - name: Log in to the Container registry - uses: docker/login-action@v2 + uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} @@ -35,14 +35,14 @@ jobs: - name: Extract metadata (tags, labels) for Docker id: meta - uses: docker/metadata-action@v4 + uses: docker/metadata-action@v5 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | type=semver,pattern={{ version }} - name: Build and push Docker image - uses: docker/build-push-action@v4 + uses: docker/build-push-action@v5 with: context: . push: true From 18d29c98d3224f094ec949a34215e7dff03b4876 Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Wed, 13 Mar 2024 11:58:40 +0100 Subject: [PATCH 11/14] updated tags --- .github/workflows/publish-image.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/publish-image.yml b/.github/workflows/publish-image.yml index 6252706..86f9fcd 100644 --- a/.github/workflows/publish-image.yml +++ b/.github/workflows/publish-image.yml @@ -40,6 +40,7 @@ jobs: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | type=semver,pattern={{ version }} + type=ref,event=branch - name: Build and push Docker image uses: docker/build-push-action@v5 From e82631c89dde23e77e063e571553ee18574c3d0c Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Wed, 13 Mar 2024 13:16:22 +0100 Subject: [PATCH 12/14] modified tags --- .github/workflows/publish-image.yml | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/.github/workflows/publish-image.yml b/.github/workflows/publish-image.yml index 86f9fcd..e9e6421 100644 --- a/.github/workflows/publish-image.yml +++ b/.github/workflows/publish-image.yml @@ -18,22 +18,15 @@ jobs: build_and_publish: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 -# - name: Login to ghcr.io -# run: echo "${{ secrets.GHCR_PAT }}" | docker login --username ${{ github.actor }} --password-stdin ${{ env.REGISTRY }} -# - name: Build and tag image -# run: docker build . -t ${{ env.REGISTRY}}/${{ env.IMAGE_NAME }}:latest -# - name: Push image to ghcr.io -# run: docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest - - - name: Log in to the Container registry + - name: Checkout sources + uses: actions/checkout@v4 + - name: Log in to the container registry uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GHCR_PAT }} - - - name: Extract metadata (tags, labels) for Docker + - name: Extract metadata (tags, labels) id: meta uses: docker/metadata-action@v5 with: @@ -41,7 +34,8 @@ jobs: tags: | type=semver,pattern={{ version }} type=ref,event=branch - + type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') }} + type=sha - name: Build and push Docker image uses: docker/build-push-action@v5 with: From 4aa8b6d2fbc32e1fd6e2de231333dc80f88457f8 Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Wed, 13 Mar 2024 13:55:46 +0100 Subject: [PATCH 13/14] updated README.md with Docker instructions --- README.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7674a71..6924183 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,33 @@ A script to create, update and delete Zabbix hosts using Netbox device objects. -## Installation +## Installation via Docker + +To pull the latest stable version to your local cache, use the following docker pull command: +``` +docker pull ghcr.io/TheNetworkGuy/netbox-zabbix-sync:latest +``` + +Make sure to specify the needed environment variables for the script to work (see [here](#set-environment-variables)) +on the command line or use an [env file](https://docs.docker.com/reference/cli/docker/container/run/#env). + +``` +docker run -d -t -i -e ZABBIX_HOST='https://zabbix.local' \ +-e ZABBIX_TOKEN='othersecrettoken' \ +-e NETBOX_HOST='https://netbox.local' \ +-e NETBOX_TOKEN='secrettoken' \ +--name netbox-zabbix-sync ghcr.io/TheNetworkGuy/netbox-zabbix-sync:latest +``` + +This should run a one-time sync, you can check the sync with `docker logs`. + +The image uses the default `config.py` for it's configuration, you can use a volume mount in the docker run command +to override with your own config file if needed (see [config file](#config-file)): +``` +docker run -d -t -i -v $(pwd)/config.py:/opt/netbox-zabbix/config.py ... +``` + +## Installation from Source ### Cloning the repository ``` From 3079a88de8674723d03da213ffb5fc9d26308d04 Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Wed, 13 Mar 2024 14:00:03 +0100 Subject: [PATCH 14/14] better `docker logs` example. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6924183..67fa514 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ docker run -d -t -i -e ZABBIX_HOST='https://zabbix.local' \ --name netbox-zabbix-sync ghcr.io/TheNetworkGuy/netbox-zabbix-sync:latest ``` -This should run a one-time sync, you can check the sync with `docker logs`. +This should run a one-time sync, you can check the sync with `docker logs netbox-zabbix-sync`. The image uses the default `config.py` for it's configuration, you can use a volume mount in the docker run command to override with your own config file if needed (see [config file](#config-file)):