From 8380b7ff941cd08a474e2e2542af8d73364fc3b1 Mon Sep 17 00:00:00 2001 From: cimnine Date: Fri, 17 Sep 2021 20:57:26 +0200 Subject: [PATCH] Refactor Github Action --- .github/workflows/push.yml | 34 ++++++------ .github/workflows/release.yml | 98 ++++++++++++++++++----------------- build.sh | 13 +++-- 3 files changed, 75 insertions(+), 70 deletions(-) diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 6f803ac..4595f1e 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -13,12 +13,15 @@ jobs: runs-on: ubuntu-latest name: Checks syntax of our code steps: - - uses: actions/checkout@v2 + - + uses: actions/checkout@v2 with: # Full git history is needed to get a proper list of changed files within `super-linter` fetch-depth: 0 - - uses: actions/setup-python@v2 - - name: Lint Code Base + - + uses: actions/setup-python@v2 + - + name: Lint Code Base uses: github/super-linter@v4 env: DEFAULT_BRANCH: develop @@ -35,6 +38,7 @@ jobs: PYTHON_BLACK_CONFIG_FILE: pyproject.toml PYTHON_FLAKE8_CONFIG_FILE: .flake8 PYTHON_ISORT_CONFIG_FILE: pyproject.toml + build: continue-on-error: ${{ matrix.docker_from == 'alpine:edge' }} strategy: @@ -54,33 +58,25 @@ jobs: runs-on: ubuntu-latest name: Builds new NetBox Docker Images steps: - - id: git-checkout + - name: Checkout uses: actions/checkout@v2 - - id: get-version + - name: Get Version of NetBox Docker run: | echo "::set-output name=version::$(cat VERSION)" shell: bash - - id: setup-cache - name: NetBox Docker Buildx Cache - uses: actions/cache@v2 - with: - path: | - .buildx-cache - key: ${{ steps.get-version.outputs.version }}-push-${{ matrix.platform }}-${{ hashFiles('Dockerfile','requirements-container.txt') }} - restore-keys: | - ${{ steps.get-version.outputs.version }}-push- - ${{ steps.get-version.outputs.version }}- - - id: qemu-setup + - name: Set up QEMU uses: docker/setup-qemu-action@v1 - - id: buildx-setup + - + id: buildx-setup name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 with: install: true - - id: docker-build + - + id: docker-build name: Build the image from '${{ matrix.docker_from }}' with '${{ matrix.build_cmd }}' run: ${{ matrix.build_cmd }} env: @@ -88,7 +84,7 @@ jobs: GH_ACTION: enable BUILDX_BUILDER_NAME: ${{ steps.buildx-setup.outputs.name }} BUILDX_PLATFORMS: ${{ matrix.platform }} - - id: docker-test + - name: Test the image run: IMAGE="${FINAL_DOCKER_TAG}" ./test.sh if: steps.docker-build.outputs.skipped != 'true' diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9b10685..46a4f3d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -23,92 +23,94 @@ jobs: runs-on: ubuntu-latest name: Builds new NetBox Docker Images steps: - - id: git-checkout + - name: Checkout uses: actions/checkout@v2 - - id: get-version + - name: Get Version of NetBox Docker run: | echo "::set-output name=version::$(cat VERSION)" shell: bash - - id: setup-cache - name: NetBox Docker Buildx Cache - uses: actions/cache@v2 - with: - path: | - .buildx-cache - key: ${{ steps.get-version.outputs.version }}-release-${{ matrix.platform }}-${{ hashFiles('Dockerfile','requirements-container.txt') }} - restore-keys: | - ${{ steps.get-version.outputs.version }}-release- - ${{ steps.get-version.outputs.version }}- - - id: qemu-setup + - name: Set up QEMU uses: docker/setup-qemu-action@v1 - - id: buildx-setup + - + id: buildx-setup name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 with: install: true - - id: buildx-platforms + - name: Available platforms run: echo ${{ steps.buildx-setup.outputs.platforms }} - - id: docker-build + - + id: docker-build name: Build the image with '${{ matrix.build_cmd }}' run: ${{ matrix.build_cmd }} env: GH_ACTION: enable BUILDX_BUILDER_NAME: ${{ steps.buildx-setup.outputs.name }} BUILDX_PLATFORMS: ${{ matrix.platform }} - - id: docker-test + - name: Test the image run: IMAGE="${FINAL_DOCKER_TAG}" ./test.sh if: steps.docker-build.outputs.skipped != 'true' - - id: registry-login - name: Login to the Docker Registry - run: | - echo "::add-mask::$DOCKERHUB_USERNAME" - echo "::add-mask::$DOCKERHUB_PASSWORD" - docker login -u "$DOCKERHUB_USERNAME" --password "${DOCKERHUB_PASSWORD}" "${DOCKER_REGISTRY}" - env: - DOCKERHUB_USERNAME: ${{ secrets.dockerhub_username }} - DOCKERHUB_PASSWORD: ${{ secrets.dockerhub_password }} + - + name: Login to docker.io + uses: docker/login-action@v1 + with: + registry: docker.io + username: ${{ secrets.dockerhub_username }} + password: ${{ secrets.dockerhub_password }} if: steps.docker-build.outputs.skipped != 'true' - - id: registry-push + - + id: registry-push name: Push the image run: ${{ matrix.build_cmd }} --push-only if: steps.docker-build.outputs.skipped != 'true' - - id: registry-logout - name: Logout of the Docker Registry - run: docker logout "${DOCKER_REGISTRY}" - if: steps.docker-build.outputs.skipped != 'true' - # Quay.io - - id: quayio-docker-build + # quay.io + - name: Build the image with '${{ matrix.build_cmd }}' run: ${{ matrix.build_cmd }} env: DOCKER_REGISTRY: quay.io GH_ACTION: enable - - id: quayio-registry-login - name: Login to the Quay.io Registry - run: | - echo "::add-mask::$QUAYIO_USERNAME" - echo "::add-mask::$QUAYIO_PASSWORD" - docker login -u "$QUAYIO_USERNAME" --password "${QUAYIO_PASSWORD}" "${DOCKER_REGISTRY}" - env: - DOCKER_REGISTRY: quay.io - QUAYIO_USERNAME: ${{ secrets.quayio_username }} - QUAYIO_PASSWORD: ${{ secrets.quayio_password }} if: steps.docker-build.outputs.skipped != 'true' - - id: quayio-registry-push + - + name: Login to Quay.io + uses: docker/login-action@v1 + with: + registry: quay.io + username: ${{ secrets.quayio_username }} + password: ${{ secrets.quayio_password }} + if: steps.docker-build.outputs.skipped != 'true' + - name: Push the image run: ${{ matrix.build_cmd }} --push-only env: DOCKER_REGISTRY: quay.io if: steps.docker-build.outputs.skipped != 'true' - - id: quayio-registry-logout - name: Logout of the Docker Registry - run: docker logout "${DOCKER_REGISTRY}" + + # ghcr.io + - + name: Build the image with '${{ matrix.build_cmd }}' + run: ${{ matrix.build_cmd }} env: - DOCKER_REGISTRY: quay.io + DOCKER_REGISTRY: ghcr.io + GH_ACTION: enable + if: steps.docker-build.outputs.skipped != 'true' + - + name: Login to GitHub Container Registry + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + if: steps.docker-build.outputs.skipped != 'true' + - + name: Push the image + run: ${{ matrix.build_cmd }} --push-only + env: + DOCKER_REGISTRY: ghcr.io if: steps.docker-build.outputs.skipped != 'true' diff --git a/build.sh b/build.sh index cb644f5..7306a8d 100755 --- a/build.sh +++ b/build.sh @@ -6,7 +6,7 @@ echo "▶️ $0 $*" set -e if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then -cat < [--push|--push-only] branch The branch or tag to build. Required. --push Pushes the built Docker image to the registry. @@ -184,7 +184,7 @@ if [ "${2}" != "--push-only" ] && [ -z "${SKIP_GIT}" ]; then ( $DRY cd "${NETBOX_PATH}" - # shellcheck disable=SC2030 + # shellcheck disable=SC2031 if [ -n "${HTTP_PROXY}" ]; then git config http.proxy "${HTTP_PROXY}" fi @@ -444,13 +444,20 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do if [ -n "${BUILDX_PULL_REMOTE_CACHE}" ]; then echo "📥 Pulling cache from '${CACHE_TO_DOCKER_TAG}' before build" DOCKER_BUILD_ARGS+=("--cache-from=type=registry,ref=${CACHE_FROM_DOCKER_TAG},mode=max") + elif [ -n "${GH_ACTION}" ]; then + echo "📥 Pulling from GitHub Action cache before build" else + echo "📥 Pulling buildx cache from '${BUILDX_LOCAL_CACHE-.buildx-cache}' before build" DOCKER_BUILD_ARGS+=("--cache-from=type=local,src=${BUILDX_LOCAL_CACHE-.buildx-cache},mode=max") fi if [ -n "${BUILDX_PUSH_REMOTE_CACHE}" ]; then - echo "📤 Pushing cache to '${CACHE_TO_DOCKER_TAG}' after build" + echo "📤 Pushing buildx cache to '${CACHE_TO_DOCKER_TAG}' after build" DOCKER_BUILD_ARGS+=("--cache-to=type=registry,ref=${CACHE_TO_DOCKER_TAG},mode=max") + elif [ -n "${GH_ACTION}" ]; then + echo "📤 Pushing to GitHub Action cache after build" + DOCKER_BUILD_ARGS+=("--cache-to=type=gha") else + echo "📤 Pushing buildx cache to '${BUILDX_LOCAL_CACHE-.buildx-cache}' after build" DOCKER_BUILD_ARGS+=("--cache-to=type=local,dest=${BUILDX_LOCAL_CACHE-.buildx-cache},mode=max") fi