Each corresponding job is explained below:
hadolinthadolint job checks the Dockerfile for any issues
following best practices.hadolint tool is used to perform this actiongetsquid_varsvariables.envdocker-hub-builddocker-hub-build is responsible for building
the Docker image for squid using the latest version that was fetched in
the getsquid_vars jobdocker-hub-testdocker-hub-test is responsible
for testing the Docker image that was built in the
docker-hub-build stagepush-docker-hubpush-docker-hub is responsible
for tagging and pushing the Docker image that was built and tested in
the previous stagesdocker-hub-build-armdocker-hub-build but for arm
architecturedocker-hub-test-armdocker-hub-test but for arm
architecturedocker-hub-build-arm stage
is pulled and testedpush-docker-hub-armpush-docker-hub but for arm
architecturedocker-hub-test-arm stage
is tagged and pushed to DockerHubchatgpt_analysishadolintThis job uses the hadolint tool to enforce the use of
best practices in Dockerfiles.
before_script:
- cd $CI_PROJECT_DIR
script:
- hadolint --ignore DL3008 Dockerfile
Here, we first navigate to the project directory
(cd $CI_PROJECT_DIR). Then, using the hadolint
command, we check the Dockerfile for any issues, ignoring particular
rules (-ignore DL3008).
getsquid_varsThe purpose of this job is to automatically fetch the latest version
of squid-cache from GitHub. The fetched squid version is stored in a
file variables.env.
script:
- apt update && apt install git curl ca-certificates -y --no-upgrade --no-install-recommends --no-install-suggests
- 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
- echo $SQUID_VERSION
- sed -i "s/{{SQUID_VERSION}}/$SQUID_VERSION/g" README_template.md
- sed -i "s/{{DATE}}/$(date +%Y%m%d)/g" README_template.md
- cp README_template.md README.md
- git config user.email "fredbcode"
- git config user.name "fredbcode"
- git add README.md
- git commit -m "README Auto update [skip ci]" || true
- git push https://$GITLAB_TOKEN@gitlab.com/fredbcode-images/squid.git HEAD:master || trueExplanation:
git, curl, ca-certificates.SQUID_VERSION is fetched from a GitHub webpage and
exported as a variable for later use in the variables.env
file.README_template.md, replacing the
{{SQUID_VERSION}} placeholder. The current date is also
substituted in for the {{DATE}} placeholder.README_template.md file are then
copied to README.md.README.md file are committed to the
repository, and the commit message is “README Auto update [skip ci]” to
skip triggering another build.docker-hub-buildThe docker-hub-build job builds a Docker image using the
Dockerfile provided in the repository, and the
SQUID_VERSION variable gathered from the
getsquid_vars job. Then it pushes the image to DockerHub
for storage.
before_script:
- docker login -u "$DOCKER_HUB_USER" -p "$DOCKER_HUB_TOKEN" $DOCKER_HUB_REGISTRY
script:
- source variables.env
- docker build --build-arg SQUID_VERSION=$SQUID_VERSION --pull -t $CONTAINER_BUILD_NOPROD_NAME_AMD64 .
- docker push $CONTAINER_BUILD_NOPROD_NAME_AMD64Explanation:
variables.env file to get the
SQUID_VERSION variable.$CONTAINER_BUILD_NOPROD_NAME_AMD64 (The tag name
contains the AMD64 notation as it’s built for AMD64 architecture).docker-hub-testThe objective of this job is to test the image built in previous step. We specifically want to ensure that squid, the reverse proxy, is functioning correctly within the Docker image.
before_script:
- apt update && apt install -y curl --no-upgrade --no-install-recommends --no-install-suggests
script:
- export https_proxy=http://$CONTAINER_TEST_NAME:3128 && curl -k https://www.google.frExplanation:
curl.https://www.google.fr. If squid in the Docker
image works correctly, this command should fetch the HTML content of
Google’s French homepage without any issues.Similar steps are followed for docker-hub-build-arm,
docker-hub-test-arm and push-docker-hub-arm
but for arm architecture.
chatgpt_analysisThe objective of this job is to interact with the Chat GPT-4 API which will generate a detailed analysis. The obtained result is transformed into a Markdown and an HTML document, which are scp-ed to a remote server.
before_script:
- apt update && apt install curl git jq ca-certificates pandoc openssh-client -y --no-upgrade --no-install-recommends --no-install-suggests
- source variables.env
- SQUID_VERSION=squid-$SQUID_VERSION
script:
- JOBS_CONTENT=$(cat .gitlab-ci.yml gitlabci/*)
- LAST_COMMIT=$(git log -1 --pretty=format:"%h %s%n%b")
- CONTENT="Please provide an in-depth explanation of the following GitLab CI/CD jobs with the following details, [TRUNCATED] at the end of the text add a line with \"Project:$CI_PROJECT_URL Pipeline:$CI_PIPELINE_URL Docker images:https://hub.docker.com/r/fredbcode\""
- 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")
- ANSWER=$(echo $RESPONSE | jq 'del(.choices[0].message.content)')
- RESPONSE=$(echo $RESPONSE | jq -r '.choices[0].message.content')
- echo "$ANSWER"
- echo -e "$RESPONSE" > chatgpt_analysis_$(date +%Y%m%d).md
- mkdir -p ~/.ssh
- eval $(ssh-agent -s)
- '[[ -f /.dockerenv ]] && echo -e "Host *
StrictHostKeyChecking no
" > ~/.ssh/config'
- ssh-add <(echo "$SSH_NOSTROMO_KEY")
- 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/
- echo "!!! See Artifact for explanations or https://e2guardian.numsys.eu !!!"Explanations: - We first update the package lists and install the
necessary packages: curl, git, jq
(command-line JSON processing tool), ca-certificates,
pandoc (text conversion tool), and
openssh-client - Have access to the
SQUID_VERSION variable from variables.env by
sourcing it - Then, we concatenate all the CI/CD configuration files,
and store the last commit message. - Next, we construct content for the
ChatGPT by combining pre-defined text with job content and the last
commit - We then format the content into an appropriate JSON payload to
interact with the ChatGPT API - The payload is POSTed to the API, and
response is captured - The ANSWER variable is used to hold
the response without specific fields. While RESPONSE holds
the text content from the choices.message.content field of
the response JSON. - It echoes out the ANSWER variable and
writes the RESPONSE variable to a markdown (.md) file,
named with the current date
(chatgpt_analysis_$(date +%Y%m%d).md) - It then generates
the SSH key, and configures the SSH client for a non-interactive
environment. - Then it converts the markdown file, which contains the
ChatGPT’s elaborate explanations, into an HTML file. - Finally, the HTML
file is scp-ed to a remote web server for public viewing and an
announcement message is printed
update_dockerhub_readmeThis job makes an update to the readme of the docker image on
dockerhub with the README.md present on the git repository. It uses
curl and jq to make the PATCH request to
Dockerhub’s API.
before_script:
- apt update && apt install -y curl jq ca-certificates --no-upgrade --no-install-recommends --no-install-suggests
script:
- README_CONTENT=$(cat README.md)
- PAYLOAD=$(jq -n --arg desc "$README_CONTENT" '{"full_description":$desc}')
- echo "Payload JSON:$PAYLOAD"
- 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)
- curl -X PATCH -H "Authorization:JWT $TOKEN" -H "Content-Type:application/json" -d "$PAYLOAD" https://hub.docker.com/v2/repositories/$HUB_REGISTRY_IMAGE
only:
- masterExplanation:
curl, jq, and
ca-certificates.Below are the significant parameters, environment variables, and files referenced in the job.
CI_PROJECT_DIR: it’s a predefined environment variable
in GitLab which points to the absolute project directory path.GIT_CLONE_PATH: to specify the path where the project
should be cloned.CONTAINER_CLIENT_IMAGE: specifies the Docker image
which is used.DOCKER_HUB_USER and DOCKER_HUB_PASSWORD:
used to login into Dockerhub.DOCKER_HUB_REGISTRY: specifies the Docker
registry.variables.env file: used to hold the
SQUID_VERSION variable that’s used in subsequent jobs..gitlab-ci.yml & files in gitlabci/
directory: These files form the pipeline jobs configurationREADME_template.md and README.md files:
used to keep the user informed about the latest updates/sample
configurations/assetsHUB_REGISTRY_IMAGE: name of the Docker image in
DockerHUB. For instance: “fredbcode/squid”.DOCKER_HUB_TOKEN: used to authenticate to DockerHUB
API.SSH_NOSTROMO_KEY: the private SSH key to establish a
secured connection.CHATGPT_API_KEY: to authenticate requests to Chat GPT-4
API.The script part of each job is only run if all jobs from
prior stages succeed. For example, all jobs in the
Docker-hub-build stage will only run if the jobs in the
Get-version stage have succeeded.
Most of the jobs have dependencies on other jobs. This dependency is usually because of shared environment variables:
docker-hub-build &
docker-hub-build-arm need environment variables from
getsquid_vars.docker-hub-test depends on
docker-hub-build, docker-hub-test-arm depends
on docker-hub-build-arm.push-docker-hub depends on docker-hub-test
& getsquid_vars, push-docker-hub-arm
depends on docker-hub-test-arm &
getsquid_vars.chatgpt_analysis &
update_dockerhub_readme depends on
getsquid_vars.For every job that executes successfully, it can potentially generate an output or an “artifact”. This could be test results, a compiled app, jar files, war files, Docker images, logs, or any files generated during the run of the job. The specific artifacts for this CI/CD pipelines include:
getsquid_vars job generates
variables.env which contains the latest squid versiondocker-hub-build-amd64 and
docker-hub-build-arm jobs generate a docker image and push
it to DockerHubchatgpt_analysis job generates ‘chatgpt_analysis_ date
.md’ and ‘chatgpt_analysis_ date .html’ files which contain in-depth
analysis and these files are scp-ed to a remote web server for public
viewingupdate_dockerhub_readme updates the
README.md on the image page on DockerHub.The commit message “Fix Healthcheck” indicates that this commit introduces changes that aim to fix health checks in the application. Health checks are used to probe systems and applications for their status.
In terms of CI/CD pipelines, this could signify changes to tests that are performed as part of the pipeline, changes to configurations, or corrections to scripts that perform these checks.
In this particular pipeline, there isn’t a job that indicates a health check. The commit might be fixing an issue which is outside of the pipeline, maybe in the application for which the pipeline is made (here, Squid). The exact impact of this commit would therefore depend on the nature of the “health check” that has been fixed. The changes related to the commit can be viewed by accessing GitLab with the commit hash 2d97f25.
Please note that it’s not necessary that each commit impacts the
pipeline and triggers a new pipeline run. The
workflow:rules:if condition states that if a commit message
includes the “[skip ci]” tag, the pipeline associated with that commit
will not run. Hence, a commit can be made without triggering a CI/CD
pipeline.