5. Artifacts, Templates, and the Container Registry
Introduction
This section will add templated jobs for a container build and code quality scan to our existing pipeline, as well as use artifacts to make the build artifact from the build_app job available to the run_app job.
Preparing the Pipeline
-
1. In your demo project, find the 5-artifacts-templates-container-registry
branch inCode > Branches
-
2. Next to the branch name, click New
to create a new Merge Request from this branch -
3. Click Create Merge Request
-
4. Review the changes to the .gitlab-ci.yml file: -
Artifacts
- The
artifacts
keyword has been added to thebuild_app
job, specifying that the files in the /dist directory (generated by the invocation ofyarn run build
) are to be uploaded back to GitLab after the job completes. This enables the build artifact to be available in subsequent pipeline jobs that run in fresh containers. - By default, subsequent jobs download all artifacts from successful jobs in a previous stages. This allows the the
run_app
job to test that the previously built artifact is able to successfully execute. - The keywords
needs
andwhen:on_success
have been added to therun_app
job to prevent it from running In the event that thebuild_app
job fails (meaning no build artifact would be generated)
- The
-
Templates and the Container Registry
- Using the include keyword, templates haves been declared which will add a pre-configured jobs to our pipeline.
- The
kaniko-build
job uses Kaniko, a containerization utility that does not require the Docker daemon, to build a Docker image of our application and push the resulting image to our project's integrated Container Registry. - The
code_quality
job runs the open source Code Climate analyzer on the source code of the repository, generating reports on the code’s quality and complexity.
- The
- An override for the
kaniko-build
job has also been added to the pipeline configuration, changing multiple behaviors of the base templated job:- A new stage,
containerize
, has been added which the container build job will run in.- This stage follows the
run
stage which verifies that the compiled .jar file actually runs. This ensures a container image will not be built if jobs in therun
stage fail.
- This stage follows the
- The tag for the built image is specified using the
IMAGE_TAG
variable- Pre-defined CI/CD variables are used to tag the image with the branch from which it was built, as well as the specific commit reference
- Rules for when the containerization job is included in the pipeline
- This job will only be included in branch pipelines, and only after a successful
run_app
job in the pipeline
- This job will only be included in branch pipelines, and only after a successful
- A new stage,
- An override has also been added to the
code_quality
job to allow it to fail without failing the pipeline, as well as to generate the code quality report as an HTML artifact that can be downloaded from the GitLab UI.
- Using the include keyword, templates haves been declared which will add a pre-configured jobs to our pipeline.
-
Artifacts
-
5. Within the Merge Request, click Mark as Ready
in the Overview tab (if prompted), then clickMerge
. -
6. In the left hand navigation menu, click Build
->Pipelines
, then click the link for the most recently ran pipeline to view the status of the pipeline. -
7. Click on the run_app
job to view the job log and confirm that the artifact from thebuild_app
job was downloaded and was able to run without error. -
8. Click on the code_quality
job, then click "download" under "Job Artifacts" on the right side menu to download and view the gl-code-quality-report.html file. This file should report that there were no code quality findings.-
Note: You can also download pipeline artifacts from clicking
Build
->Artifacts
in the right side navigation menu for a project.
-
Note: You can also download pipeline artifacts from clicking
-
9. Verify that the kaniko-build
job successfully completed in the pipeline, then navigate in the left-hand menu toDeploy
->Container Registry
to view the built Docker image for the application.