|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Usage: ./update_docker_image_name.sh <options> |
| 3 | +# |
| 4 | +# Description: |
| 5 | +# |
| 6 | +# Search through all repository files for hardcoded Docker image names and |
| 7 | +# update either the Docker image tag, the NCS version, or both. |
| 8 | +# |
| 9 | +# Notes: |
| 10 | +# |
| 11 | +# At least one of the two options must be provided. The script will update all |
| 12 | +# matching references in repository files accordingly. |
| 13 | +# |
| 14 | +# Options: |
| 15 | +# |
| 16 | +# --image-tag <IMAGE_TAG> The new image tag to replace the existing |
| 17 | +# one in Docker image names. Must be either a |
| 18 | +# valid semantic version in the format |
| 19 | +# vMAJOR.MINOR.PATCH or the string "latest". |
| 20 | +# |
| 21 | +# --ncs-revision <NCS_VERSION> The new NCS version to replace the existing |
| 22 | +# one in Docker image names. Must be a valid |
| 23 | +# semantic version number in the format |
| 24 | +# vMAJOR.MINOR.PATCH. |
| 25 | +# |
| 26 | +# -h, --help Show this help message and exit. |
| 27 | +# |
| 28 | + |
| 29 | +# Starting part of the docker image that the script searches for. |
| 30 | +# If the image name changes, this variable must be updated. |
| 31 | +DOCKER_IMAGE="ghcr.io/irnas/ncs-zephyr" |
| 32 | + |
| 33 | +NCS_REVISION="" |
| 34 | +IMAGE_TAG="" |
| 35 | + |
| 36 | +print_help() { |
| 37 | + sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' <"$0" |
| 38 | +} |
| 39 | + |
| 40 | +val_image_tag() { |
| 41 | + # Validate the argument against the regex and "latest" literal |
| 42 | + if [[ ! $IMAGE_TAG =~ $SEMVER_REGEX && ! $IMAGE_TAG =~ "latest" ]]; then |
| 43 | + echo -e "${RED}Invalid image tag: ${IMAGE_TAG}${NC}" |
| 44 | + echo |
| 45 | + print_help |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | +} |
| 49 | + |
| 50 | +val_ncs_revision() { |
| 51 | + # Validate the argument against the regex |
| 52 | + if [[ ! $NCS_REVISION =~ $SEMVER_REGEX ]]; then |
| 53 | + echo -e "${RED}Invalid NCS revision: ${NCS_REVISION}${NC}" |
| 54 | + echo |
| 55 | + print_help |
| 56 | + exit 1 |
| 57 | + fi |
| 58 | +} |
| 59 | + |
| 60 | +# Parse arguments |
| 61 | +while [[ $# -gt 0 ]]; do |
| 62 | + case "$1" in |
| 63 | + --ncs-revision) |
| 64 | + NCS_REVISION="$2" |
| 65 | + val_ncs_revision |
| 66 | + shift 2 |
| 67 | + ;; |
| 68 | + --image-tag) |
| 69 | + IMAGE_TAG="$2" |
| 70 | + val_image_tag |
| 71 | + shift 2 |
| 72 | + ;; |
| 73 | + --help | -h) |
| 74 | + print_help |
| 75 | + exit 0 |
| 76 | + ;; |
| 77 | + *) |
| 78 | + echo "Unknown option: $1" |
| 79 | + print_help |
| 80 | + exit 1 |
| 81 | + ;; |
| 82 | + esac |
| 83 | +done |
| 84 | + |
| 85 | +SEMVER_REGEX="v[0-9]+\.[0-9]+\.[0-9]+" |
| 86 | + |
| 87 | +# Color codes |
| 88 | +RED='\033[0;31m' |
| 89 | +NC='\033[0m' # No Color |
| 90 | + |
| 91 | +# Check if at least one variable is set |
| 92 | +if [[ -z $NCS_REVISION && -z $IMAGE_TAG ]]; then |
| 93 | + echo -e "${RED}Error: At least one argument must be provided.${NC}" |
| 94 | + echo |
| 95 | + print_help |
| 96 | + exit 1 |
| 97 | +fi |
| 98 | + |
| 99 | +# Get absolute path of the script location |
| 100 | +SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd) |
| 101 | +PROJ_DIR=$(realpath "${SCRIPT_DIR}/../..") |
| 102 | + |
| 103 | +# shellcheck disable=SC2164 |
| 104 | +cd "${PROJ_DIR}" |
| 105 | + |
| 106 | +# Find files containing the search pattern, only search in tracked files to be |
| 107 | +# faster. |
| 108 | +files=$(git ls-files | xargs -I {} grep -rl "$DOCKER_IMAGE" {}) |
| 109 | + |
| 110 | +# Process each file |
| 111 | +for file in $files; do |
| 112 | + if [[ -n $IMAGE_TAG ]]; then |
| 113 | + # Replace Docker image tag |
| 114 | + # |
| 115 | + # 1. (${DOCKER_IMAGE}-[^:]+:) - 1st capture group that matches by the |
| 116 | + # DOCKER_IMAGE up to the first colon. |
| 117 | + # 2. (${SEMVER_REGEX}|latest) - 2nd capture group that matches either |
| 118 | + # the semantic version number or literal "latest". |
| 119 | + # 3. (.*) - 3rd capture group, matches everything after the 2nd group. |
| 120 | + # Needed so that the text after docker image name is not modified. |
| 121 | + # |
| 122 | + # The replacement pattern just surrounds the NEW_VERSION with the |
| 123 | + # first and second capture groups. |
| 124 | + # |
| 125 | + # For example, if "v2.0.0" is passed as the argument, then the: |
| 126 | + # |
| 127 | + # "image": "ghcr.io/irnas/some_image-v1.0.0-dev:v1.0.0" |
| 128 | + # |
| 129 | + # is replaced by |
| 130 | + # |
| 131 | + # "image": "ghcr.io/irnas/some_image-v1.0.0-dev:v2.0.0" |
| 132 | + # |
| 133 | + sed -i -E \ |
| 134 | + "s#(${DOCKER_IMAGE}[^:]+:)(${SEMVER_REGEX}|latest)(.*)#\1${IMAGE_TAG}\3#g" \ |
| 135 | + "$file" |
| 136 | + fi |
| 137 | + |
| 138 | + if [[ -n $NCS_REVISION ]]; then |
| 139 | + # Replace NCS revisions |
| 140 | + # |
| 141 | + # 1. (${DOCKER_IMAGE}-) - 1st capture group that matches by the |
| 142 | + # DOCKER_IMAGE up to the first hyphen. |
| 143 | + # 2. ${SEMVER_REGEX} - Match the semantic version number. |
| 144 | + # 3. (.*) - 2nd capture group, matches everything after the 2nd group. |
| 145 | + # Needed so that the text after docker image name is not modified. |
| 146 | + # |
| 147 | + # The replacement pattern just surrounds the NEW_VERSION with the |
| 148 | + # first and second capture groups. |
| 149 | + # |
| 150 | + # For example, if "v2.0.0" is passed as the argument, then the: |
| 151 | + # |
| 152 | + # "image": "ghcr.io/irnas/some_image-v1.0.0-dev:v1.0.0" |
| 153 | + # |
| 154 | + # is replaced by |
| 155 | + # |
| 156 | + # "image": "ghcr.io/irnas/some_image-v2.0.0-dev:v1.0.0" |
| 157 | + sed -i -E \ |
| 158 | + "s|(${DOCKER_IMAGE}-)${SEMVER_REGEX}(-.*)|\1${NCS_REVISION}\2|g" \ |
| 159 | + "$file" |
| 160 | + fi |
| 161 | + |
| 162 | +done |
0 commit comments