Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Сборка образов 1с #45

Merged
merged 13 commits into from
Oct 1, 2024
Merged

Conversation

nixel2007
Copy link
Member

@nixel2007 nixel2007 commented Oct 1, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced Docker build scripts with conditional checks for login credentials, improving error handling during the Docker login process.
    • Introduced new environment variables for better configuration of Docker image builds.
  • Bug Fixes

    • Improved error handling and control flow in Docker build scripts to prevent unnecessary login attempts when credentials are missing.
  • Chores

    • Updated workflow configurations to streamline job execution and improve overall build process efficiency.

Copy link

coderabbitai bot commented Oct 1, 2024

Walkthrough

The pull request introduces modifications to the GitHub Actions workflow and multiple shell scripts related to Docker image building. Key changes include the addition of environment variables for Docker configuration, conditional checks for the DOCKER_LOGIN, DOCKER_PASSWORD, and DOCKER_REGISTRY_URL variables to prevent empty login attempts, and updates to build arguments for Docker images. The workflow execution strategy has also been adjusted to enhance job management.

Changes

Files Change Summary
.github/workflows/build.yml Added environment variables for Docker configuration and modified the execution strategy; changed the job matrix from directories to scripts for building Docker images.
build-base-k8s-jenkins-agent.sh Updated shebang for portability, added error handling with pipefail, and introduced a conditional check for Docker login credentials.
build-base-swarm-jenkins-agent.sh Enhanced Docker login process with a conditional check for credentials and clarified handling of the NO_CACHE argument.
build-crs.sh Improved handling of Docker login credentials with a conditional check before executing the login command.
build-edt-k8s-agent.sh Added a conditional check for Docker login and updated build argument from DOCKER_REGISTRY_URL to DOWNLOADER_REGISTRY_URL.
build-edt-swarm-agent.sh Enhanced Docker login process with a conditional check for credentials and clarified variable usage for the NO_CACHE condition.
build-oscript-k8s-agent.sh Updated shebang for portability, added error handling with pipefail, and introduced a conditional check for Docker login credentials.
build-oscript-swarm-agent.sh Enhanced Docker login process with a conditional check for credentials and refined handling of the NO_CACHE variable.
build-server.sh Updated shebang for portability, added error handling with set -eo pipefail, and introduced a conditional check for Docker login credentials; referenced an undefined variable no_cache_arg in the docker build command.

Sequence Diagram(s)

sequenceDiagram
    participant Workflow
    participant DockerLogin
    participant DockerBuild

    Workflow->>DockerLogin: Check if DOCKER_LOGIN is not empty
    alt Login Credentials Provided
        DockerLogin->>DockerLogin: Execute docker login
    else No Credentials
        DockerLogin->>DockerLogin: Skip login
    end
    Workflow->>DockerBuild: Execute Docker build commands
Loading

🐰 In the garden where we play,
New scripts hop in, bright as day!
With checks for logins, oh so neat,
Building images, quite the feat!
Docker's dance, a joyful cheer,
Changes made, let's give a cheer! 🎉


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@nixel2007 nixel2007 marked this pull request as ready for review October 1, 2024 06:39
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

🧹 Outside diff range and nitpick comments (17)
build-oscript-k8s-agent.sh (2)

4-6: Approve the login check, but suggest improvements for robustness.

The addition of the DOCKER_LOGIN check is a good improvement to prevent unintended login attempts. However, we can further enhance the script's robustness:

  1. Use the -n test for non-empty strings, which is more idiomatic in bash.
  2. Check all required variables for the docker login command.

Consider refactoring the conditional check as follows:

-if [ $DOCKER_LOGIN != '' ] ; then
-    docker login -u $DOCKER_LOGIN -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL
+if [ -n "$DOCKER_LOGIN" ] && [ -n "$DOCKER_PASSWORD" ] && [ -n "$DOCKER_REGISTRY_URL" ]; then
+    docker login -u "$DOCKER_LOGIN" -p "$DOCKER_PASSWORD" "$DOCKER_REGISTRY_URL"
 fi

This change ensures all required variables are non-empty before attempting to log in and uses proper quoting for variable expansion.


Line range hint 1-38: Suggest improvements for overall script robustness and maintainability.

While the main change in this PR is good, there are several areas where we can improve the overall script:

  1. Add checks for required environment variables.
  2. Use proper quoting for variable expansions.
  3. Add error handling for docker commands.
  4. Consider using arrays for build arguments to improve readability.

Here's a refactored version of the script incorporating these suggestions:

#!/bin/bash
set -euo pipefail

# Check required environment variables
required_vars=("DOCKER_REGISTRY_URL" "DOCKER_SYSTEM_PRUNE" "NO_CACHE")
for var in "${required_vars[@]}"; do
    if [ -z "${!var}" ]; then
        echo "Error: $var is not set" >&2
        exit 1
    fi
done

# Docker login
if [ -n "${DOCKER_LOGIN:-}" ] && [ -n "${DOCKER_PASSWORD:-}" ]; then
    if ! docker login -u "$DOCKER_LOGIN" -p "$DOCKER_PASSWORD" "$DOCKER_REGISTRY_URL"; then
        echo "Error: Docker login failed" >&2
        exit 1
    fi
fi

# Docker system prune
if [ "$DOCKER_SYSTEM_PRUNE" = 'true' ]; then
    docker system prune -af
fi

# Set build arguments
build_args=(
    "--pull"
    "--build-arg" "DOCKER_REGISTRY_URL=library"
    "--build-arg" "BASE_IMAGE=adoptopenjdk"
    "--build-arg" "BASE_TAG=14-hotspot"
)

if [ "$NO_CACHE" = 'true' ]; then
    build_args+=("--no-cache")
fi

# Build oscript-jdk image
if ! docker build "${build_args[@]}" -t "$DOCKER_REGISTRY_URL/oscript-jdk:latest" -f oscript/Dockerfile .; then
    echo "Error: Failed to build oscript-jdk image" >&2
    exit 1
fi

# Update build arguments for oscript-agent
build_args=(
    "--build-arg" "DOCKER_REGISTRY_URL=$DOCKER_REGISTRY_URL"
    "--build-arg" "BASE_IMAGE=oscript-jdk"
    "--build-arg" "BASE_TAG=latest"
)

if [ "$NO_CACHE" = 'true' ]; then
    build_args+=("--no-cache")
fi

# Build oscript-agent image
if ! docker build "${build_args[@]}" -t "$DOCKER_REGISTRY_URL/oscript-agent:latest" -f k8s-jenkins-agent/Dockerfile .; then
    echo "Error: Failed to build oscript-agent image" >&2
    exit 1
fi

# Push oscript-agent image
if ! docker push "$DOCKER_REGISTRY_URL/oscript-agent:latest"; then
    echo "Error: Failed to push oscript-agent image" >&2
    exit 1
fi

This refactored version improves error handling, variable usage, and overall script structure. It also uses arrays for build arguments, which improves readability and maintainability.

build-oscript-swarm-agent.sh (3)

4-6: Approve login check, but suggest security improvements

The addition of the conditional check for $DOCKER_LOGIN is a good improvement. It prevents attempting to log in with empty credentials, which could cause errors. However, there are a few suggestions to enhance security and robustness:

  1. Consider checking all required variables (DOCKER_LOGIN, DOCKER_PASSWORD, DOCKER_REGISTRY_URL) before attempting to log in.
  2. Use double quotes around variable expansions to prevent word splitting and glob expansion.
  3. Consider using ${DOCKER_LOGIN:+x} to check if the variable is set and not empty.

Here's a suggested improvement:

if [ -n "${DOCKER_LOGIN}" ] && [ -n "${DOCKER_PASSWORD}" ] && [ -n "${DOCKER_REGISTRY_URL}" ]; then
    docker login -u "${DOCKER_LOGIN}" -p "${DOCKER_PASSWORD}" "${DOCKER_REGISTRY_URL}"
else
    echo "Skipping Docker login due to missing credentials"
fi

This change ensures all required variables are set, uses proper quoting, and provides feedback when login is skipped.


Line range hint 1-43: Overall script review summary

The changes to this script improve its robustness by adding a check before Docker login. However, there are opportunities for further enhancements:

  1. Improve the security and completeness of the Docker login check.
  2. Resolve the potential issue with the undefined $no_cache_arg variable.
  3. Consider adding more error handling and logging throughout the script.

These improvements will make the script more reliable and maintainable.

Consider breaking down this script into functions for better readability and maintainability. For example:

#!/bin/bash
set -e

perform_docker_login() {
    # Docker login logic here
}

handle_system_prune() {
    # System prune logic here
}

build_and_push_images() {
    # Docker build and push logic here
}

main() {
    perform_docker_login
    handle_system_prune
    build_and_push_images
}

main

This structure would make the script easier to understand and modify in the future.


Potential issue with undefined no_cache_arg variable

The variable $no_cache_arg is used in multiple scripts but is not defined anywhere in the repository. This may lead to unexpected behavior during the build process.

  • build-server.sh:19
  • build-edt-swarm-agent.sh:22
  • build-edt-k8s-agent.sh:22
  • build-oscript-k8s-agent.sh:19
  • build-oscript-k8s-agent.sh:28
  • build-oscript-swarm-agent.sh:19
  • build-oscript-swarm-agent.sh:28
  • build-base-swarm-jenkins-agent.sh:19
  • build-base-k8s-jenkins-agent.sh:19

Consider defining the no_cache_arg variable or replacing $no_cache_arg with $last_arg if they serve the same purpose.

🔗 Analysis chain

Line range hint 1-43: Potential issue with undefined variable

While reviewing the entire file, I noticed that the $no_cache_arg variable is used in the docker build commands, but it's not defined in the visible part of the script. This could lead to unexpected behavior.

To verify this, please run the following script:

If the variable is indeed undefined, consider replacing $no_cache_arg with $last_arg in the docker build commands, as last_arg seems to serve the same purpose.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for the definition and usage of no_cache_arg variable

# Test 1: Search for the definition of no_cache_arg
echo "Searching for no_cache_arg definition:"
rg --type bash -n 'no_cache_arg='

# Test 2: Search for the usage of no_cache_arg
echo "Searching for no_cache_arg usage:"
rg --type bash -n '\$no_cache_arg'

# Expected results:
# - If no_cache_arg is properly defined, Test 1 should show its definition.
# - Test 2 should show the lines where it's used in docker build commands.
# - If Test 1 shows no results but Test 2 does, it indicates the variable is used without being defined.

Length of output: 292


Script:

#!/bin/bash
# Description: Check for the definition and usage of no_cache_arg variable

# Test 1: Search for the definition of no_cache_arg
echo "Searching for no_cache_arg definition:"
rg --type sh -n 'no_cache_arg='

# Test 2: Search for the usage of no_cache_arg
echo "Searching for no_cache_arg usage:"
rg --type sh -n '\$no_cache_arg'

# Expected results:
# - If no_cache_arg is properly defined, Test 1 should show its definition.
# - Test 2 should show the lines where it's used in docker build commands.
# - If Test 1 shows no results but Test 2 does, it indicates the variable is used without being defined.

Length of output: 679

build-crs.sh (1)

Line range hint 1-38: Overall assessment of changes

The addition of the Docker login check is a step in the right direction, improving the script's robustness. However, there are several areas where the script can be further enhanced:

  1. Improve the Docker login conditional check for better error handling and security.
  2. Enhance the security of the Docker login process by using more secure methods for password handling.
  3. Adopt additional shell scripting best practices for increased reliability and maintainability.

These improvements will make the script more secure, robust, and easier to maintain. Despite these suggestions, the core functionality of building and pushing Docker images remains intact and should work as intended.

Consider breaking down this script into smaller, focused scripts or functions for better modularity and easier testing. For example, you could have separate functions for Docker login, system pruning, and building/pushing each image.

build-server.sh (3)

4-6: Improve the condition for Docker login

The addition of a check before executing the Docker login command is a good practice. However, the current condition can be improved for better robustness.

Consider modifying the condition as follows:

-if [ $DOCKER_LOGIN != '' ] ; then
+if [ -n "$DOCKER_LOGIN" ] ; then
     docker login -u $DOCKER_LOGIN -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL
 fi

This change will correctly handle cases where $DOCKER_LOGIN is unset or set to an empty string.


Line range hint 21-21: Fix potential variable name mismatch

There's a potential issue with the variable name used in the Docker build command.

The script uses $no_cache_arg, which is not defined. It seems this should be $last_arg, which is set earlier in the script based on the $NO_CACHE value. Please verify and correct this:

 docker build \
 	--pull \
-    $no_cache_arg \
+    $last_arg \
 	--build-arg DOCKER_REGISTRY_URL=library \
     --build-arg BASE_IMAGE=ubuntu \
     --build-arg BASE_TAG=20.04 \
     --build-arg ONESCRIPT_PACKAGES="yard" \
     -t $DOCKER_REGISTRY_URL/oscript-downloader:latest \
 	-f oscript/Dockerfile \
     $last_arg

Note that $last_arg is also used at the end of the command. You may want to review if this is the intended behavior or if it should be removed from one of these positions.


Line range hint 1-38: Enhance script robustness and flexibility

While the script functions as intended, there are a few areas where it could be improved:

  1. Add checks for required environment variables at the beginning of the script. For example:
required_vars="DOCKER_LOGIN DOCKER_PASSWORD DOCKER_REGISTRY_URL ONEC_USERNAME ONEC_PASSWORD ONEC_VERSION"
for var in $required_vars; do
    if [ -z "${!var}" ]; then
        echo "Error: $var is not set"
        exit 1
    fi
done
  1. Consider making some of the hardcoded values configurable via environment variables or command-line arguments. For instance:
BASE_IMAGE=${BASE_IMAGE:-ubuntu}
BASE_TAG=${BASE_TAG:-20.04}
ONESCRIPT_PACKAGES=${ONESCRIPT_PACKAGES:-"yard"}

These changes would make the script more robust and flexible, allowing for easier configuration and reducing the risk of running with missing or incorrect values.

.github/workflows/build.yml (1)

Line range hint 1-9: Consider expanding trigger events for the workflow

The workflow name "Build Docker Images" accurately describes its purpose, which is good. The current trigger events are appropriate for a feature branch. However, consider the following suggestions:

  1. Include the main branch in the trigger events if these Docker images should be built for main branch changes as well.
  2. Add a trigger for tags or releases if you want to build images for specific versions.

You might want to update the trigger events like this:

on:
  push:
    branches: [ main, feature/first-bit ]
  pull_request:
    branches: [ main, feature/first-bit ]
  release:
    types: [published]
  workflow_dispatch:

This would ensure the images are built for main branch changes, pull requests to main, and when a new release is published, while still allowing manual triggers.

🧰 Tools
🪛 actionlint

16-16: duplicate value "build-base-k8s-jenkins-agent.sh" is found in matrix "script". the same value is at line:16,col:19

(matrix)

build-edt-swarm-agent.sh (3)

4-6: Approve with suggestions: Improve robustness of DOCKER_LOGIN check

The addition of the DOCKER_LOGIN check is a good improvement to prevent empty login attempts. However, we can further enhance its robustness:

  1. Use double quotes around variable expansions to handle potential spaces or special characters.
  2. Use the -n test to check for non-empty strings, which also accounts for unset variables.

Consider applying this diff for improved robustness:

-if [ $DOCKER_LOGIN != '' ] ; then
-    docker login -u $DOCKER_LOGIN -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL
+if [ -n "$DOCKER_LOGIN" ] ; then
+    docker login -u "$DOCKER_LOGIN" -p "$DOCKER_PASSWORD" "$DOCKER_REGISTRY_URL"
 fi

Line range hint 23-23: Fix inconsistent variable naming for NO_CACHE option

There's an inconsistency between the variable definition and its usage for the NO_CACHE option. The variable is defined as last_arg but used as no_cache_arg in the docker build command. This will cause the NO_CACHE option to not work as intended.

Apply this diff to fix the inconsistency:

-    $no_cache_arg \
+    $last_arg \

Alternatively, if the intention was to use a separate variable for the no-cache option, update the variable definition earlier in the script:

-last_arg='.'
-if [ $NO_CACHE = 'true' ] ; then
-	last_arg='--no-cache .'
+no_cache_arg=''
+if [ "$NO_CACHE" = 'true' ] ; then
+	no_cache_arg='--no-cache'
 fi

This second option allows for more flexibility in how the no-cache option is applied.


Line range hint 1-58: Improve overall script robustness

While reviewing the entire script, I noticed a few areas where we can improve robustness and consistency:

  1. Quote environment variables to prevent word splitting and glob expansion issues.
  2. Use consistent indentation (spaces vs tabs).
  3. Remove unnecessary $last_arg usage at the end of docker build commands.

Consider applying these changes throughout the script:

  1. Quote environment variables:
-if [ $DOCKER_SYSTEM_PRUNE = 'true' ] ; then
+if [ "$DOCKER_SYSTEM_PRUNE" = 'true' ] ; then
  1. Use consistent indentation (preferably spaces):
-	last_arg='--no-cache .'
+    last_arg='--no-cache .'
  1. Remove unnecessary $last_arg usage:
 docker build \
     --build-arg DOCKER_REGISTRY_URL=$DOCKER_REGISTRY_URL \
     --build-arg BASE_IMAGE=edt \
     --build-arg BASE_TAG=$edt_escaped \
-    -t $DOCKER_REGISTRY_URL/edt-agent:$edt_escaped \
-	-f swarm-jenkins-agent/Dockerfile \
-    $last_arg
+    -t "$DOCKER_REGISTRY_URL/edt-agent:$edt_escaped" \
+    -f swarm-jenkins-agent/Dockerfile .

Apply similar changes to other docker build commands in the script.

build-edt-k8s-agent.sh (1)

4-6: Good addition, but consider improving robustness

The new conditional block for Docker login is a good practice to prevent unnecessary login attempts. However, consider the following improvements:

  1. Use [[ -n "${DOCKER_LOGIN}" ]] instead of $DOCKER_LOGIN != '' to properly handle cases where the variable is unset.
  2. Add error handling for the login command.

Here's a suggested improvement:

-if [ $DOCKER_LOGIN != '' ] ; then
-    docker login -u $DOCKER_LOGIN -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL
+if [[ -n "${DOCKER_LOGIN}" ]]; then
+    if ! docker login -u "${DOCKER_LOGIN}" -p "${DOCKER_PASSWORD}" "${DOCKER_REGISTRY_URL}"; then
+        echo "Docker login failed" >&2
+        exit 1
+    fi
 fi

This change will:

  • Properly handle cases where DOCKER_LOGIN is unset
  • Exit the script if the login fails, preventing further errors
  • Use double quotes around variables to prevent word splitting and globbing
build-base-swarm-jenkins-agent.sh (3)

4-6: Approve conditional Docker login with suggestion for error handling

The addition of the conditional check for DOCKER_LOGIN is a good improvement. It prevents unnecessary login attempts when no credentials are provided.

Consider adding error handling for the Docker login process. Here's a suggested improvement:

 if [ $DOCKER_LOGIN != '' ] ; then
-    docker login -u $DOCKER_LOGIN -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL
+    if ! docker login -u $DOCKER_LOGIN -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL; then
+        echo "Docker login failed"
+        exit 1
+    fi
 fi

This change will cause the script to exit if the Docker login fails, preventing further errors in subsequent steps.


Line range hint 13-16: Improve variable naming and definition for cache handling

The conditional handling of the NO_CACHE option is a good addition. However, there are a few improvements that can be made:

  1. The variable name last_arg is not descriptive of its purpose.
  2. The $no_cache_arg variable is used in Docker build commands but is not defined in this script.

Consider the following improvements:

-last_arg='.'
-if [ $NO_CACHE = 'true' ] ; then
-    last_arg='--no-cache .'
+build_args='.'
+no_cache_arg=''
+if [ "$NO_CACHE" = 'true' ] ; then
+    no_cache_arg='--no-cache'
+    build_args="$no_cache_arg ."
 fi

Then, update all docker build commands to use $build_args instead of $last_arg:

 docker build \
 	--pull \
-    $no_cache_arg \
 	--build-arg DOCKER_REGISTRY_URL=library \
     --build-arg BASE_IMAGE=ubuntu \
     --build-arg BASE_TAG=20.04 \
     --build-arg ONESCRIPT_PACKAGES="yard" \
     -t $DOCKER_REGISTRY_URL/oscript-downloader:latest \
 	-f oscript/Dockerfile \
-    $last_arg
+    $build_args

These changes improve clarity and ensure that all variables used are properly defined.


Line range hint 1-93: Suggest overall improvements to enhance script robustness and maintainability

While the script functions well for building and pushing multiple Docker images, there are several improvements that could enhance its robustness and maintainability:

  1. Add comprehensive error handling and logging.
  2. Consider breaking down the script into functions for better readability and maintainability.
  3. Implement a dry-run mode for testing purposes.

Here's a high-level suggestion for restructuring the script:

#!/bin/bash
set -euo pipefail

log() {
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*" >&2
}

docker_login() {
    if [ "${DOCKER_LOGIN:-}" != '' ] ; then
        log "Logging into Docker registry"
        if ! docker login -u "$DOCKER_LOGIN" -p "$DOCKER_PASSWORD" "$DOCKER_REGISTRY_URL"; then
            log "Docker login failed"
            exit 1
        fi
    else
        log "Skipping Docker login"
    fi
}

docker_system_prune() {
    if [ "${DOCKER_SYSTEM_PRUNE:-false}" = 'true' ] ; then
        log "Pruning Docker system"
        docker system prune -af
    fi
}

build_and_push_image() {
    local image_name="$1"
    shift
    log "Building image: $image_name"
    if ! docker build "$@" -t "$image_name"; then
        log "Failed to build image: $image_name"
        return 1
    fi
    
    if [ "${DRY_RUN:-false}" != 'true' ]; then
        log "Pushing image: $image_name"
        if ! docker push "$image_name"; then
            log "Failed to push image: $image_name"
            return 1
        fi
    else
        log "[DRY RUN] Would push image: $image_name"
    fi
}

main() {
    docker_login
    docker_system_prune

    local build_args='.'
    local no_cache_arg=''
    if [ "${NO_CACHE:-false}" = 'true' ] ; then
        no_cache_arg='--no-cache'
        build_args="$no_cache_arg ."
    fi

    # Build and push each image
    build_and_push_image "$DOCKER_REGISTRY_URL/oscript-downloader:latest" \
        --pull \
        --build-arg DOCKER_REGISTRY_URL=library \
        --build-arg BASE_IMAGE=ubuntu \
        --build-arg BASE_TAG=20.04 \
        --build-arg ONESCRIPT_PACKAGES="yard" \
        -f oscript/Dockerfile \
        $build_args

    # ... repeat for other images ...
}

main "$@"

This restructured script:

  1. Uses functions for better organization.
  2. Implements proper logging.
  3. Adds error handling for critical operations.
  4. Introduces a dry-run mode for testing.
  5. Uses set -euo pipefail for stricter error checking.

Remember to adapt this structure to fit all your image building and pushing operations.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between b1e71b6 and 4c69ef3.

📒 Files selected for processing (9)
  • .github/workflows/build.yml (2 hunks)
  • build-base-k8s-jenkins-agent.sh (1 hunks)
  • build-base-swarm-jenkins-agent.sh (1 hunks)
  • build-crs.sh (1 hunks)
  • build-edt-k8s-agent.sh (2 hunks)
  • build-edt-swarm-agent.sh (1 hunks)
  • build-oscript-k8s-agent.sh (1 hunks)
  • build-oscript-swarm-agent.sh (1 hunks)
  • build-server.sh (1 hunks)
🧰 Additional context used
🪛 actionlint
.github/workflows/build.yml

16-16: duplicate value "build-base-k8s-jenkins-agent.sh" is found in matrix "script". the same value is at line:16,col:19

(matrix)

🪛 yamllint
.github/workflows/build.yml

[error] 40-40: trailing spaces

(trailing-spaces)

🔇 Additional comments (5)
.github/workflows/build.yml (1)

31-34: ⚠️ Potential issue

Improve handling of sensitive data

The use of secrets for ONEC_USERNAME and ONEC_PASSWORD is a good security practice. However, the handling of Docker credentials needs improvement:

  1. DOCKER_LOGIN and DOCKER_PASSWORD are set to empty strings. This could lead to unintended behavior or security issues if these variables are used in scripts without proper checks.

Consider the following options:

  1. If Docker authentication is required, use GitHub secrets:

    DOCKER_LOGIN: ${{ secrets.DOCKER_LOGIN }}
    DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
  2. If Docker authentication is not needed, remove these variables entirely.

  3. If these variables are used conditionally, ensure all scripts using them have proper checks to handle empty values securely.

To ensure proper handling of these variables, let's check their usage in the scripts:

Review the results to ensure these variables are used safely throughout the codebase.

✅ Verification successful

Sensitive Data Handling Verified

The usage of DOCKER_LOGIN and DOCKER_PASSWORD in the scripts is properly handled:

  • Each script checks if DOCKER_LOGIN is not empty before attempting to perform Docker login using these credentials.
  • Docker credentials are only used when provided, ensuring that empty strings do not lead to unintended behavior or security risks.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for DOCKER_LOGIN and DOCKER_PASSWORD usage in scripts
rg -i 'DOCKER_LOGIN|DOCKER_PASSWORD' *.sh

Length of output: 1507

build-edt-k8s-agent.sh (2)

Line range hint 1-50: Overall, good improvements to the script

The changes to this script enhance its functionality and clarity. The addition of the Docker login check and the more specific build argument name are positive improvements. The suggestions provided in the previous comments will further increase the robustness and maintainability of the script.


35-35: Approve the change, but verify impact and consider new env var

The change from DOCKER_REGISTRY_URL to DOWNLOADER_REGISTRY_URL as a build argument name is good for clarity. However:

  1. Ensure this change doesn't break any expectations in the Dockerfile or downstream processes.
  2. Consider introducing a new environment variable DOWNLOADER_REGISTRY_URL for consistency between the argument name and its value.

To verify the impact of this change, please run the following script:

This will help identify any potential conflicts or inconsistencies in the usage of these variables across the project.

Consider updating the environment variable usage:

-    --build-arg DOWNLOADER_REGISTRY_URL=$DOCKER_REGISTRY_URL \
+    --build-arg DOWNLOADER_REGISTRY_URL=${DOWNLOADER_REGISTRY_URL:-$DOCKER_REGISTRY_URL} \

This change allows for a separate DOWNLOADER_REGISTRY_URL environment variable while falling back to DOCKER_REGISTRY_URL if not set, maintaining backwards compatibility.

✅ Verification successful

Verified: Impact of changing build argument name is acceptable

The addition of DOWNLOADER_REGISTRY_URL as a new build argument, while retaining the usage of DOCKER_REGISTRY_URL, does not affect other parts of the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for uses of DOCKER_REGISTRY_URL and DOWNLOADER_REGISTRY_URL in Dockerfiles and scripts

echo "Checking for DOCKER_REGISTRY_URL usage:"
rg --type dockerfile --type shell 'DOCKER_REGISTRY_URL'

echo "\nChecking for DOWNLOADER_REGISTRY_URL usage:"
rg --type dockerfile --type shell 'DOWNLOADER_REGISTRY_URL'

Length of output: 375


Script:

#!/bin/bash
# Description: Corrected script to check for uses of DOCKER_REGISTRY_URL and DOWNLOADER_REGISTRY_URL in Dockerfiles and scripts

echo "Checking for DOCKER_REGISTRY_URL usage:"
rg --type Dockerfile --type shell 'DOCKER_REGISTRY_URL'

echo "\nChecking for DOWNLOADER_REGISTRY_URL usage:"
rg --type Dockerfile --type shell 'DOWNLOADER_REGISTRY_URL'

Length of output: 375


Script:

#!/bin/bash
# Description: Search for DOCKER_REGISTRY_URL and DOWNLOADER_REGISTRY_URL in Dockerfiles and shell scripts

echo "Finding Dockerfiles and searching for DOCKER_REGISTRY_URL:"
fd --type f --exact 'Dockerfile' | xargs rg 'DOCKER_REGISTRY_URL'

echo "\nFinding Dockerfiles and searching for DOWNLOADER_REGISTRY_URL:"
fd --type f --exact 'Dockerfile' | xargs rg 'DOWNLOADER_REGISTRY_URL'

echo "\nSearching all shell scripts for DOCKER_REGISTRY_URL:"
rg --type sh 'DOCKER_REGISTRY_URL'

echo "\nSearching all shell scripts for DOWNLOADER_REGISTRY_URL:"
rg --type sh 'DOWNLOADER_REGISTRY_URL'

Length of output: 24698

build-base-k8s-jenkins-agent.sh (2)

Line range hint 1-93: Summary of review for build-base-k8s-jenkins-agent.sh

The addition of the DOCKER_LOGIN check is a positive change that improves the script's robustness. However, there are several areas where the script could be further improved:

  1. Enhance the DOCKER_LOGIN check with better error handling and security practices.
  2. Apply bash best practices throughout the script (quoting variables, improving error handling).
  3. Make the script more configurable by using environment variables for currently hardcoded values.
  4. Improve version management for all Docker images built by the script.

These changes will make the script more reliable, secure, and maintainable. Please consider implementing the suggested improvements in a future iteration.


Line range hint 1-93: General script improvements and best practices

While the main change in this PR is the addition of the DOCKER_LOGIN check, there are several areas where the script could be improved:

  1. Quote variables to prevent word splitting and globbing issues.
  2. Define or remove the $no_cache_arg variable.
  3. Make hardcoded values configurable via environment variables.
  4. Add version tagging for the oscript-downloader image.
  5. Improve error handling for build and push commands.

Here are some suggested improvements:

  1. Quote variables:

    if [ "${DOCKER_SYSTEM_PRUNE}" = 'true' ] ; then
        docker system prune -af
    fi
  2. Define no_cache_arg:

    no_cache_arg=""
    if [ "${NO_CACHE}" = 'true' ] ; then
        no_cache_arg="--no-cache"
    fi
  3. Make hardcoded values configurable:

    UBUNTU_VERSION="${UBUNTU_VERSION:-20.04}"
    ONESCRIPT_PACKAGES="${ONESCRIPT_PACKAGES:-yard}"
  4. Add version tagging for oscript-downloader:

    OSCRIPT_VERSION="${OSCRIPT_VERSION:-latest}"
    docker build \
        # ... other args ...
        -t "${DOCKER_REGISTRY_URL}/oscript-downloader:${OSCRIPT_VERSION}" \
        -t "${DOCKER_REGISTRY_URL}/oscript-downloader:latest" \
        # ... rest of command ...
  5. Improve error handling:

    docker build \
        # ... build args ...
        || { echo "Build failed for ${DOCKER_REGISTRY_URL}/onec-client:${ONEC_VERSION}"; exit 1; }
    
    docker push "${DOCKER_REGISTRY_URL}/onec-client:${ONEC_VERSION}" \
        || { echo "Push failed for ${DOCKER_REGISTRY_URL}/onec-client:${ONEC_VERSION}"; exit 1; }

To ensure these changes don't introduce any regressions, please run the following verification script:

#!/bin/bash
# Check if required environment variables are set
echo "Checking required environment variables..."
required_vars=("DOCKER_REGISTRY_URL" "ONEC_USERNAME" "ONEC_PASSWORD" "ONEC_VERSION")
for var in "${required_vars[@]}"; do
    if [[ -z "${!var}" ]]; then
        echo "Error: $var is not set"
    else
        echo "$var is set"
    fi
done

# Check if Docker is available
echo "Checking Docker availability..."
if command -v docker &> /dev/null; then
    echo "Docker is available"
else
    echo "Error: Docker is not available"
fi

build-crs.sh Outdated Show resolved Hide resolved
build-crs.sh Outdated
@@ -1,7 +1,9 @@
#!/bin/bash
set -e

docker login -u $DOCKER_LOGIN -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL
if [ $DOCKER_LOGIN != '' ] ; then
docker login -u $DOCKER_LOGIN -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improve security of Docker login

Passing the Docker password directly in the command line can be a security risk as it may be visible in process lists or logs.

Consider using Docker's built-in credential helpers or passing the password through stdin. Here's an example using stdin:

echo "${DOCKER_PASSWORD}" | docker login -u "${DOCKER_LOGIN}" --password-stdin "${DOCKER_REGISTRY_URL}"

This method is more secure as it doesn't expose the password in the command line.

build-crs.sh Outdated Show resolved Hide resolved
.github/workflows/build.yml Outdated Show resolved Hide resolved
.github/workflows/build.yml Show resolved Hide resolved
.github/workflows/build.yml Outdated Show resolved Hide resolved
build-base-k8s-jenkins-agent.sh Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
build-edt-k8s-agent.sh (2)

4-11: Improved Docker login process with better error handling.

The addition of credential checks before attempting Docker login is a good improvement. It prevents unnecessary login attempts and provides clear feedback.

Consider adding more specific error messages to indicate which credentials are missing. This could help in troubleshooting. For example:

if [ -z "${DOCKER_LOGIN}" ]; then
    echo "DOCKER_LOGIN is not set"
fi
if [ -z "${DOCKER_PASSWORD}" ]; then
    echo "DOCKER_PASSWORD is not set"
fi
if [ -z "${DOCKER_REGISTRY_URL}" ]; then
    echo "DOCKER_REGISTRY_URL is not set"
fi

Line range hint 1-56: Overall improvements with some points to address

The changes to this script have generally improved its robustness and clarity. The main improvements are:

  1. Better error handling for Docker login.
  2. More specific naming for the downloader registry URL.

However, there are a few points that need attention:

  1. Consider adding more specific error messages for missing Docker credentials.
  2. Verify the consistency of DOCKER_REGISTRY_URL and DOWNLOADER_REGISTRY_URL usage throughout the script.
  3. Ensure that DOWNLOADER_REGISTRY_URL is properly set if it's a new variable.

Once these points are addressed, the script will be more maintainable and less prone to errors.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 6fb745c and 4e21eac.

📒 Files selected for processing (9)
  • .github/workflows/build.yml (2 hunks)
  • build-base-k8s-jenkins-agent.sh (1 hunks)
  • build-base-swarm-jenkins-agent.sh (1 hunks)
  • build-crs.sh (1 hunks)
  • build-edt-k8s-agent.sh (2 hunks)
  • build-edt-swarm-agent.sh (1 hunks)
  • build-oscript-k8s-agent.sh (1 hunks)
  • build-oscript-swarm-agent.sh (1 hunks)
  • build-server.sh (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (8)
  • .github/workflows/build.yml
  • build-base-k8s-jenkins-agent.sh
  • build-base-swarm-jenkins-agent.sh
  • build-crs.sh
  • build-edt-swarm-agent.sh
  • build-oscript-k8s-agent.sh
  • build-oscript-swarm-agent.sh
  • build-server.sh
🔇 Additional comments (2)
build-edt-k8s-agent.sh (2)

Line range hint 34-45: Improved clarity with DOWNLOADER_REGISTRY_URL, but verify consistency.

The change from DOCKER_REGISTRY_URL to DOWNLOADER_REGISTRY_URL improves clarity by specifying the purpose of this registry URL. This is a good practice.

However, please verify that this change doesn't introduce inconsistencies with other parts of the script. Consider updating all occurrences of DOCKER_REGISTRY_URL to more specific names where appropriate, or ensure that DOWNLOADER_REGISTRY_URL is properly set earlier in the script.

Run the following script to check all occurrences of both variables:

#!/bin/bash
echo "Occurrences of DOCKER_REGISTRY_URL:"
grep -n "DOCKER_REGISTRY_URL" build-edt-k8s-agent.sh

echo -e "\nOccurrences of DOWNLOADER_REGISTRY_URL:"
grep -n "DOWNLOADER_REGISTRY_URL" build-edt-k8s-agent.sh

Based on the results, decide if further changes are needed for consistency.


Line range hint 13-33: Verify consistency of DOCKER_REGISTRY_URL usage.

While this segment hasn't changed, it's worth noting that DOCKER_REGISTRY_URL is used here as a build argument. However, in the second Docker build command, this has been changed to DOWNLOADER_REGISTRY_URL.

Please confirm if the usage of DOCKER_REGISTRY_URL in this build command is correct or if it should also be updated to DOWNLOADER_REGISTRY_URL for consistency.

✅ Verification successful

Verification Successful: DOCKER_REGISTRY_URL is used consistently as intended.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Verify the usage of DOCKER_REGISTRY_URL across the script
grep -n "DOCKER_REGISTRY_URL" build-edt-k8s-agent.sh

Length of output: 951

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant