DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • An Explanation of Jenkins Architecture
  • Streamlining AWS Lambda Deployments
  • Implementing CI/CD Pipelines With Jenkins and Docker
  • Source Code Management and Branching Strategies for CI/CD

Trending

  • Being a Backend Developer Today Feels Harder Than 20 Years Ago
  • Modern Digital Authentication Protocols
  • How to Query XML Files Using APIs in Java
  • Integration of AI Tools With SAP ABAP Programming
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. DevOps and CI/CD
  4. How to Implement Jenkins CI/CD With Git Crypt

How to Implement Jenkins CI/CD With Git Crypt

Take a look at this tutorial that demonstrates how to implement Git secrets with a gpg private key and how to connect it with a Jenkins CI/CD pipeline.

By 
Aditya C S user avatar
Aditya C S
·
Updated by 
Phil Hajjar user avatar
Phil Hajjar
·
Updated Mar. 29, 24 · Tutorial
Like (6)
Save
Tweet
Share
43.6K Views

Join the DZone community and get the full member experience.

Join For Free

Software applications are typically connected to externalities such as databases, SFTP sites, secured web APIs, etc. We often have to store the secrets used to access these externalities in the code we write and share these secrets with other developers in our team. These secrets can include things such as user IDs, passwords, private key files, or anything else that should not be seen by unauthorized persons. While the decision to include such secrets in a coding repository is often highly debated, there can be some use cases in which this approach may be necessary.

What Is Git Crypt?

git-crypt provides a security mechanism for Git repositories. It allows you to encrypt whatever files you wish within a repository. The encryption keys it uses can then be exported and securely shared among other developers, and it can be imported into tools such as Jenkins for testing and deployment.

Getting Started With Git Crypt

To get started with git-crypt, you will need to build it from a source or install it through your operating system's preferred package manager.  Once that is done, you will need to initialize your (existing) repository to work with git-crypt:

Plain Text
 
$ git-crypt init


You then need to tell git-crypt which files it needs to encrypt. Say you have a file containing your secrets in a directory called secretdir and has the name i-want-this-to-be-private.txt. You would need to configure a .gitattributes file to tell git-crypt to encrypt this file:

Plain Text
 
# You can use the standard syntax of .gitattributes to configure this file,
# that could include things like wildcards or other directories.

secretdir/i-want-this-to-be-private.txt filter=git-crypt diff=git-crypt

The .gitattributes file


Once you commit the .gitattributes file, you will need to make and save a change to secretdir/i-want-this-to-be-private.txt so that it will need to be committed. Once you have committed the updated version of this file, it will be encrypted for the next developer who clones the repository.

You can use the git-crypt status command to verify that your file has been encrypted:

Output: using the git-crypt status command

Another user who clones the repository and attempts to view the file without decrypting it will see gibberish:

Gibberish output from cloning the file without decrypting it

One way to allow authorized users to work with the repository would be to securely share with them a key file that will give them access. You can export this key with the command; just make sure to not store it in the same directory as your repository:

Plain Text
 
$ # You can specify any file name or path here.
$ git-crypt export-key ../git-crypt.key


Once another authorized user has the key, he or she can use it to decrypt the file and use the repository:

Plain Text
 
$ git-crypt unlock ~/git-crypt.key


Now that we have the export key, how do we integrate it into a Jenkins Pipeline?

How To Use Git Crypt in a Jenkins Pipeline

Creating Credentials in Jenkins

  1. Log into your Jenkins Web UI interface. Typically, this runs on port 8080 of the server on which Jenkins is installed.
  2. Within Jenkins, access the dashboard.  Go to "Manage Jenkins." Then choose "Credentials." 
  3. Upload the key you generated previously using the interface:Interface of uploading the key
  4. Use the added key file in the Jenkins Pipeline. Here "git-crypt-export-key" is the ID given when you add Jenkins credentials.
Plain Text
 
pipeline {
     agent { 
        node { 
            label 'my-test-node' 
        } 
    }

    environment {
        mySecret = credentials("git-crypt-export-key")

    }

    stages { 


        stage("Decrypt the files") {
            steps {
                sh """
                    cd /opt/my-secret-repo
                    git-crypt unlock '$mySecret'
                """
            }
        }
    }
}


You may get a warning about data being passed insecurely by using this method.

Conclusion

This article shows us both how to use git-crypt to protect secrets in a Git repository and how to use the keys provided by the same for CD tools such as Jenkins.

Further Reading

  • How to Integrate Your GitHub Repository to Your Jenkins Project
  • Working with PHP, Git, and Azure DevOps
  • How to Use Azure DevOps’ Work Items and PHP
Jenkins (software) Continuous Integration/Deployment Git

Opinions expressed by DZone contributors are their own.

Related

  • An Explanation of Jenkins Architecture
  • Streamlining AWS Lambda Deployments
  • Implementing CI/CD Pipelines With Jenkins and Docker
  • Source Code Management and Branching Strategies for CI/CD

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: