Based on the latest commit 5a5e859’s content, the following is an in-depth explanation of the jobs in the CI/CD pipeline in their order of execution defined in the ‘.gitlab-ci.yml’ file’s ‘stages’ section.
The following jobs are defined in the pipeline:
This job is responsible for: - Ensure the latest version of Squid from GitHub release. - Updating the ‘variables.env’ file with this latest version for other jobs to consume. - Checks if the fetched Squid version is different from the last known one and tags that information to be used by future jobs. - Commit the new Squid version and updated README.
#Ensure and store the latest Squid version
export SQUID_VERSION=$(curl -LsXGET https://github.com/squid-cache/squid/releases/latest | grep -m 1 "Release" | cut -d " " -f4 |tr -d 'v')
echo "SQUID_VERSION=$SQUID_VERSION" > variables.env
#Update README substitution with the latest Squid version and current date
sed -i "s/{{SQUID_VERSION}}/$SQUID_VERSION/g" README_template.md
sed -i "s/{{DATE}}/$(date +%Y%m%d)/g" README_template.md
#Replace or add 'last_squid_version.txt' with the Squid version
if [ -f ci/last_squid_version.txt ]; then LAST_KNOWN=$(cat ci/last_squid_version.txt); else LAST_KNOWN=""; fi
if [ "$LAST_KNOWN" != "$SQUID_VERSION" ]; then echo "$SQUID_VERSION" > ci/last_squid_version.txt; echo "version_changed=1" > version_changed; git add ci/last_squid_version.txt; else echo "version_changed=0" > version_changed; fi
#Commit updates with a commit message and push to GitHub.
git config user.email "fredbcode"
git config user.name "fredbcode"
git add README.md ci/last_squid_version.txt variables.env || true
git commit -m "README Auto update and update last_squid_version [skip ci]" || true
git push https://$GITLAB_TOKEN@gitlab.com/fredbcode-images/squid.git HEAD:master || true
These two similar jobs for two different architectures (AMD64 and ARM) are responsible for building Squid Docker images. The Dockerfile for build is obtained from the same GitLab repository. The scripts build Docker image with the Docker Build command, tags it, and pushes the image to Docker Hub after logging in with Docker credentials.
Relevant commands used in building the Docker image:
#Log in Docker Hub
docker login -u "$DOCKER_HUB_USER" -p "$DOCKER_HUB_TOKEN" $DOCKER_HUB_REGISTRY
#Build Squid Docker Image and push to Docker Hub
docker build --build-arg SQUID_VERSION=$SQUID_VERSION --pull -t $CONTAINER_BUILD_NOPROD_NAME_AMD64 .
docker push $CONTAINER_BUILD_NOPROD_NAME_AMD64
For ARM, the Docker Build command changes slightly:
#Build Squid Docker Image and push to Docker Hub
docker build -f Dockerfile --build-arg SQUID_VERSION=$SQUID_VERSION --pull -t $CONTAINER_BUILD_NOPROD_NAME_ARM .
docker push $CONTAINER_BUILD_NOPROD_NAME_ARM
These jobs are responsible for testing the Docker images built in the
previous stages. They install curl, point
https_proxy to the Squid image name, and perform a cURL
request.
#Install curl
apt update && apt install -y curl --no-upgrade --no-install-recommends --no-install-suggests
#cURL request via Squid
export https_proxy=http://$CONTAINER_TEST_NAME:3128 && curl -k https://www.google.fr
These two jobs handle tagging the Docker images with the current Squid version and the ‘latest’ tag, and they push these to Docker Hub.
#Log in Docker Hub
docker login -u "$DOCKER_HUB_USER" -p "$DOCKER_HUB_TOKEN" $DOCKER_HUB_REGISTRY
#Pull Docker image
docker pull $CONTAINER_BUILD_NOPROD_NAME_AMD64
#Tag Docker image as Squid version and latest, then push them.
docker tag $CONTAINER_BUILD_NOPROD_NAME_AMD64 $HUB_REGISTRY_IMAGE:$SQUID_VERSION-amd64
docker push $HUB_REGISTRY_IMAGE:$SQUID_VERSION-amd64
docker tag $CONTAINER_BUILD_NOPROD_NAME_AMD64 $HUB_REGISTRY_IMAGE:latest-amd64
docker push $HUB_REGISTRY_IMAGE:latest-amd64
docker tag $CONTAINER_BUILD_NOPROD_NAME_AMD64 $HUB_REGISTRY_IMAGE:latest
docker push $HUB_REGISTRY_IMAGE:latest
For ARM, the relevant Docker commands change slightly:
#Pull Docker image
docker pull $CONTAINER_BUILD_NOPROD_NAME_ARM
#Tag Docker image as Squid version and latest, then push them.
docker tag $CONTAINER_BUILD_NOPROD_NAME_ARM $HUB_REGISTRY_IMAGE:$SQUID_VERSION-arm
docker push $HUB_REGISTRY_IMAGE:$SQUID_VERSION-arm.
docker tag $CONTAINER_BUILD_NOPROD_NAME_ARM $HUB_REGISTRY_IMAGE:latest-arm
docker push $HUB_REGISTRY_IMAGE:latest-arm
This job fetches the README from the GitLab repository and updates the Docker Hub registry’s README accordingly.
#Store readme content
README_CONTENT=$(cat README.md)
#Prepare JSON
PAYLOAD=$(jq -n --arg desc "$README_CONTENT" '{"full_description":$desc}')
#Get Docker hub token
TOKEN=$(curl -v -s -X POST -H "Content-Type:application/json" -d '{"username":"'"$DOCKER_HUB_USER"'","password":"'"$DOCKER_HUB_PASSWORD"'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
#Update Docker hub readme
curl -X PATCH -H "Authorization:JWT $TOKEN" -H "Content-Type:application/json" -d "$PAYLOAD" https://hub.docker.com/v2/repositories/$HUB_REGISTRY_IMAGE
This job uses OpenAI’s ChatGPT API to generate a detailed analysis of the aforementioned jobs. It extracts the current jobs’ content, the last commit details, and uses these data as input for ChatGPT. After conversing with ChatGPT, the job stores the received response to Markdown and HTML files, then it transfers these files to an external server.
Key commands and actions include:
#Get current jobs content and most recent commit content
JOBS_CONTENT=$(cat .gitlab-ci.yml gitlabci/*)
LAST_COMMIT=$(git log -1 --pretty=format:"%h %s%n%b")
#Define content for ChatGPT
CONTENT="Please provide an in-depth explanation of the following GitLab CI/CD jobs with the following details...
#Prepare JSON for ChatGPT and query response
JSON_CONTENT=$(jq -n --arg model "gpt-4" --arg content "$CONTENT" '{model:$model, messages:[{role:"user", content:$content}] }')
RESPONSE=$(curl -X POST https://api.openai.com/v1/chat/completions -H "Authorization:Bearer $CHATGPT_API_KEY" -H "Content-Type:application/json" -d "$JSON_CONTENT")
#Store response to markdown
RESPONSE=$(echo $RESPONSE | jq -r '.choices[0].message.content')
echo -e "$RESPONSE" > chatgpt_analysis_$(date +%Y%m%d).md
#Convert markdown to HTML and transfer to external server
pandoc -s --from=markdown+smart --to=html --metadata=encoding=UTF-8 -o chatgpt_analysis_$(date +%Y%m%d).html chatgpt_analysis_$(date +%Y%m%d).md
scp -P 822 -r chatgpt_analysis*.html e2git@e2guardian.numsys.eu:/datas/e2/html/squid-ci/
The various jobs use a large number of environment variables, defined ad-hoc in the jobs or fetched from the ‘variables.env’ file.
Here are some noteworthy ones:
SQUID_VERSION: The current Squid version fetched from
GitHub Releases.DOCKER_HUB_USER & DOCKER_HUB_TOKEN:
Docker Hub account credentials for interacting with the Docker Hub
API.DOCKER_HUB_REGISTRY: Docker Hub registry URL.CONTAINER_BUILD_NOPROD_NAME_AMD64 &
CONTAINER_BUILD_NOPROD_NAME_ARM: Docker image tags used in
the ‘docker-hub-build’ and ‘docker-hub-build-arm’ jobs.HUB_REGISTRY_IMAGE: The Docker Hub image registry, used
in the ‘push-docker-hub’ & ‘push-docker-hub-arm’ jobs.CI_COMMIT_BRANCH & CI_COMMIT_TITLE:
Used in deriving other environment variables.Certain relevant file references include:
README.md: The README file housed in the GitLab
Repository that gets updated with the latest Squid version.variables.env: Generated and used for storing the
latest Squid version.The sequence of job execution does maintain certain dependencies like:
SQUID_VERSION from the ‘getsquid_vars’ job.These dependencies are registered with the ‘needs’ keyword in each job.
variables.env
file, storing SQUID_VERSION, and commits ‘README.md’ and
‘ci/last_squid_version.txt’.latest, and push them to Docker Hub.Commit 5a5e859 with the message “README Auto update and update last_squid_version [skip ci]” was about updating the ‘README.md’ file with the currently fetched Squid version and storing this version in ‘ci/last_squid_version.txt’. After these changes, the commit is pushed to the repository. This commit ensures the repository and README are always reflective of the latest Squid version.
Link: Project: https://gitlab.com/fredbcode-images/squid Pipeline: https://gitlab.com/fredbcode-images/squid/-/pipelines/2692158609 Docker images: https://hub.docker.com/r/fredbcode