Rest Assured - How to Create and Run Jenkins Pipeline using Secure Credentials in API Test Example API - GitHub API - Create GitHub Repository https://docs.github.com/en/rest/repos?apiVersion=2022-11-28 Create a repository for the authenticated user 1) Create API Test with direct API Token 2) Now Store the token securely in Jenkins 3) Create Jenkinsfile in your project and use Secure Token inside Jenkinsfile 4) Now Use Secure Token in your Java automation framework 5) Push your API Test project to GitHub 6) Create a Pipeline job in Jenkins and Run it 7) Jenkins Injects token as environment variable during run How to Store token securely in Jenkins --------------------------------------- Manage Jenkins → Credentials → (Global) → Add Credentials Choose: Kind: Secret Text Secret: your_api_token_here ID: api-token Description: API Token for Automation Click Save Now Jenkins securely stores the token (encrypted). How to Use Credentials (Token) inside Jenkinsfile -------------------------------------------------- pipeline { agent any stages { stage('Checkout Code') { steps { git branch: 'main', url: 'https://github.com/tricity-selenium/TodenDemoTest.git' } } stage('Run API Tests') { steps { withCredentials([string(credentialsId: 'api-token', variable: 'TOKEN')]) { bat 'mvn clean test' } } } } post { always { junit 'target/surefire-reports/*.xml' } } } What is happening here in Pipeline ----------------------------------- | Part | Meaning | | ---------------------------- | ------------------------------ | | `credentialsId: 'api-token'` | ID you gave in Jenkins | | `variable: 'TOKEN'` | Temporary environment variable | | `withCredentials()` | Secure block | | `bat 'mvn clean test'` | Runs your tests | Jenkins automatically: Injects token as environment variable Masks token in console logs How to Use it in your Java automation framework ----------------------------------------------- String token = System.getenv("TOKEN"); given() .header("Accept", "application/vnd.github+json") .header("Authorization", "Bearer "+ token) .header("X-GitHub-Api-Version", "2022-11-28") Project Code is Available to following GitHub Repository ------------------------------------------------------------------------------------------- https://github.com/tricity-selenium/TokenDemoRep #apitesting #restassured #apitestingwithrestassured #automationtesting #restassuredtutorials #qatraining #letslearnqa #jenkinspipeline
The information provided is not trading advice. kdj.com does not assume any responsibility for any investments made based on the information provided in this article. Cryptocurrencies are highly volatile and it is highly recommended that you invest with caution after thorough research!
If you believe that the content used on this website infringes your copyright, please contact us immediately (info@kdj.com) and we will delete it promptly.