preface

Sometimes you need to download tar.gz from the GitHub repository to do something else; Probably the most common way to do this is to go to GitHub and find a release or tag that provides tar.gz. Then click download ~ ~ but in some scenarios this operation may not be very convenient, such as packaging the source code for the customer; Quickly specify a Git tag or commit_hash, download it, encrypt the source code, and send it to the customer for onsite construction and deployment.

At this point, we would consider starting with a script, entering some parameters in a configuration file posture, and then quickly get some resources we need

Here is a relatively single scenario to start this article, look below ~

Regular CURL download Github repo.tar.gz

A private warehouse

# Specify a path to save and renameThe curl - Ls https://github.com/nodejs/node/tarball/v16.6.2 > node. The tar. Gz# Retain the original name of tar.gzThe curl - LsO https://github.com/nodejs/node/tarball/v16.6.2The -l parameter causes the HTTP request to follow the server's redirect. Curl does not follow redirects by default.
-s = silent
-o writes the output to this file, keeping the name of the remote file

Copy the code

A private warehouse

  You can specify the login user and authorize the token to get
  curl -sL --user "${GITHUB_LOGIN_USER}:${GITHUB_AUTH_TOKEN}" ${GIT_ARCHIVE_URL} >${SAVE_ARCHIVE_PATH}
  
  --user crper:crper_auth_token --> --user crper:crper_auth_token
Copy the code

Single scenario demonstration

We are here to download the private repository example ~

Key.txt variable configuration file

# GITHUB login user
GITHUB_LOGIN_USER=<github_user>
# Github authorizes Toke
GITHUB_AUTH_TOKEN=<github_token>

# Github code tag or commit hash
Each tag is essentially a commit hash
REPO_ORG=<repo_org>
PROJECT_GIT_REPO_TAG_OR_HASH=<project_tag_or_commit_hash>
PROJECT_COMMON_GIT_REPO_TAG_OR_HASH=<project_common_commit_hash>
WIKI_GIT_REPO_TAG_OR_HASH=<wiki_tag_or_commit_hash>
WIKI_COMMON_GIT_REPO_TAG_OR_HASH=<wiki_common_commit_hash>

If you need to apply to the entire shell context, you can use export as a temporary environment variable ~
# without export, we are aware of the variables we define in the context of executing that script
Copy the code

demo.sh

#! /bin/bash
The path relative to the SRC directory in the script execution path
SRC_PATH="./src"

Read some variable definitions from the configuration file
init_args() {
  if [ -f "$SRC_PATH/key.txt" ]; then
    Is equivalent to the source command
    . $SRC_PATH/key.txt
  fi
}

< p style = "word-break: inherit
check_exec_result() {
  if[$?!= 0];then
    exit 1
  fi
}


# Download archive resources
download_repo_src() {
  GIT_REPO_NAME=The $1
  GIT_REPO_TAG_OR_HASH=$2
  SAVE_ARCHIVE_PATH=$3
  GIT_ARCHIVE_URL="https://github.com/${REPO_ORG}/${GIT_REPO_NAME}/tarball/${GIT_REPO_TAG_OR_HASH}"
  echo "Download Git source archive: ${GIT_ARCHIVE_URL}"
  curl -sL --user "${GITHUB_LOGIN_USER}:${GITHUB_AUTH_TOKEN}" ${GIT_ARCHIVE_URL} >${SAVE_ARCHIVE_PATH}
  check_exec_result
  echo "Download Git source archive: ${GIT_ARCHIVE_URL} Success!!"
}

# initialization
init_arg

# Download resources
download_repo_src xxx-repo $PROJECT_GIT_REPO_TAG_OR_HASH $SRC_PATH/project.src.tar.gz
download_repo_src xxx-repo $WIKI_GIT_REPO_TAG_OR_HASH $SRC_PATH/wiki.src.tar.gz
Copy the code

Download the renderings



The actual requirements of our real business scenarios are often more complex, and downloading resources is just a relatively simple step;

Such as configuration file parameter validation, path validation and environment build scenarios, variable injection,

Different products are combined into different images, resources are reused between images and so on



Build the profile output

Sometimes we may need to output some information generated by the build process to make it easy to trace or compare the correct resources.



Commit_hash ~ ~ = commit_hash ~ ~ = commit_hash ~ ~ = commit_hash ~ ~

There is a standard folder pattern for packages that are downloaded through the tarball,

The first directory name of the package is:

Organization + repository name +commit_hash(commit_hash is always last)



After the rule is actually better to solve ~ is to use shell combination cropping filter can take ~ code as follows


Output build dependency information
write_build_info() {
  PROJECT_COMMIT=$(tar -tf ${SRC_PATH}/project.src.tar.gz | head -n 1 | awk -F The '-' '{print $NF}' | sed 's/\///g')
  WIKI_COMMIT=$(tar -tf ${SRC_PATH}/wiki.src.tar.gz | head -n 1 | awk -F The '-' '{print $NF}' | sed 's/\///g')
  CURRENT_BUILD_DATE=$(date +'%Y-%m-%d_%H:%m')

  echo "PROJECT_COMMIT=${PROJECT_COMMIT}" >> "$SRC_PATH/build_info.txt"
  echo "WIKI_COMMIT=${WIKI_COMMIT}" >> "$SRC_PATH/build_info.txt"
}
Copy the code



FAQ

Git Clone: Git Clone: Git Clone: Git Clone: Git Clone: Git Clone: Git Clone: Git Clone: Git Clone: Git Clone: Git Clone Git Clone currently supports only branch, repository, and repository tags, while Commit requires you to clone the entire repository and checkout it

conclusion

Please leave a message if there is anything wrong, we will correct it in time, thanks for reading ~