github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/build/push (about)

     1  #!/bin/bash
     2  
     3  # Copyright 2019-2020 The OpenEBS Authors. All rights reserved.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  set -e
    18  
    19  if [ -z ${DIMAGE} ];
    20  then
    21    echo "Error: DIMAGE is not specified";
    22    exit 1
    23  fi
    24  
    25  function pushBuildx() {
    26    BUILD_TAG="latest"
    27    TARGET_IMG=${DIMAGE}
    28  
    29  # TODO Currently ci builds with commit tag will not be generated,
    30  # since buildx does not support multiple repo
    31    # if not a release build set the tag and ci image
    32    if [ -z "${RELEASE_TAG}" ]; then
    33      return
    34  #    BUILD_ID=$(git describe --tags --always)
    35  #    BUILD_TAG="${BRANCH}-${BUILD_ID}"
    36  #    TARGET_IMG="${DIMAGE}-ci"
    37    fi
    38  
    39    echo "Tagging and pushing ${DIMAGE}:${TAG} as ${TARGET_IMG}:${BUILD_TAG}"
    40    docker buildx imagetools create "${DIMAGE}:${TAG}" -t "${TARGET_IMG}:${BUILD_TAG}"
    41  }
    42  
    43  # if the push is for a buildx build
    44  if [[ ${BUILDX} ]]; then
    45    pushBuildx
    46    exit 0
    47  fi
    48  
    49  IMAGEID=$( sudo docker images -q ${DIMAGE}:ci )
    50  echo "${DIMAGE}:ci -> $IMAGEID"
    51  if [ -z ${IMAGEID} ];
    52  then
    53    echo "Error: unable to get IMAGEID for ${DIMAGE}:ci";
    54    exit 1
    55  fi
    56  
    57  # Generate a unique tag based on the commit and tag
    58  BUILD_ID=$(git describe --tags --always)
    59  
    60  # Determine the current branch
    61  CURRENT_BRANCH=""
    62  if [ -z ${BRANCH} ];
    63  then
    64    CURRENT_BRANCH=$(git branch | grep \* | cut -d ' ' -f2)
    65  else
    66    CURRENT_BRANCH=${BRANCH}
    67  fi
    68  
    69  #Depending on the branch where builds are generated,
    70  # set the tag CI (fixed) and build tags.
    71  BUILD_TAG="${CURRENT_BRANCH}-${BUILD_ID}"
    72  CI_TAG="${CURRENT_BRANCH}-ci"
    73  if [ ${CURRENT_BRANCH} = "develop" ]; then
    74    CI_TAG="ci"
    75  fi
    76  
    77  echo "Set the fixed ci image tag as: ${CI_TAG}"
    78  echo "Set the build/unique image tag as: ${BUILD_TAG}"
    79  
    80  function TagAndPushImage() {
    81    REPO="$1"
    82    # Trim the `v` from the TAG if it exists
    83    # Example: v1.10.0 maps to 1.10.0
    84    # Example: 1.10.0 maps to 1.10.0
    85    # Example: v1.10.0-custom maps to 1.10.0-custom
    86    TAG="${2#v}"
    87  
    88    #Add an option to specify a custom TAG_SUFFIX
    89    #via environment variable. Default is no tag.
    90    #Example suffix could be "-debug" of "-dev"
    91    IMAGE_URI="${REPO}:${TAG}${TAG_SUFFIX}";
    92    sudo docker tag ${IMAGEID} ${IMAGE_URI};
    93    echo " push ${IMAGE_URI}";
    94    sudo docker push ${IMAGE_URI};
    95  }
    96  
    97  
    98  if [ ! -z "${DNAME}" ] && [ ! -z "${DPASS}" ];
    99  then
   100    sudo docker login -u "${DNAME}" -p "${DPASS}";
   101  
   102    # Push CI tagged image - :ci or :branch-ci
   103    TagAndPushImage "${DIMAGE}" "${CI_TAG}"
   104  
   105    # Push unique tagged image - :develop-<uuid> or :branch-<uuid>
   106    # This unique/build image will be pushed to corresponding ci repo.
   107    TagAndPushImage "${DIMAGE}-ci" "${BUILD_TAG}"
   108  
   109    if [ ! -z "${RELEASE_TAG}" ] ;
   110    then
   111      # Push with different tags if tagged as a release
   112      # When github is tagged with a release, then github action release workflow
   113      # will set the release tag in env RELEASE_TAG
   114      TagAndPushImage "${DIMAGE}" "${RELEASE_TAG}"
   115      TagAndPushImage "${DIMAGE}" "latest"
   116    fi;
   117  else
   118    echo "No docker credentials provided. Skip uploading ${DIMAGE} to docker hub";
   119  fi;
   120  
   121  # Push ci image to quay.io for security scanning
   122  if [ ! -z "${QNAME}" ] && [ ! -z "${QPASS}" ];
   123  then
   124    sudo docker login -u "${QNAME}" -p "${QPASS}" quay.io;
   125  
   126    # Push CI tagged image - :ci or :branch-ci
   127    TagAndPushImage "quay.io/${DIMAGE}" "${CI_TAG}"
   128  
   129    if [ ! -z "${RELEASE_TAG}" ] ;
   130    then
   131      # Push with different tags if tagged as a release
   132      # When github is tagged with a release, then github action reelase workflow
   133      # will set the release tag in env RELEASE_TAG
   134      # Trim the `v` from the RELEASE_TAG if it exists
   135      TagAndPushImage "quay.io/${DIMAGE}" "${RELEASE_TAG}"
   136      TagAndPushImage "quay.io/${DIMAGE}" "latest"
   137    fi;
   138  else
   139    echo "No docker credentials provided. Skip uploading ${DIMAGE} to quay";
   140  fi;
   141  
   142  #Push image to run openebs-e2e based on git commit
   143  if [ ! -z "${COMMIT}" ];
   144  then
   145    sudo docker login -u "${GITLAB_DNAME}" -p "${GITLAB_DPASS}";
   146  
   147    # Push COMMIT tagged image - :COMMIT
   148    TagAndPushImage "${DIMAGE}" "${COMMIT}"
   149  fi;