Integrate Harbor into Pipelines
- You need to enable the KubeSphere DevOps System.
- You need to create a workspace, a DevOps project, and a user (). This account needs to be invited to the DevOps project with the
operator
role. See if they are not ready.
Install Harbor
It is highly recommended that you install Harbor through . Alternatively, install Harbor manually through Helm3.
After Harbor is installed, visit
<NodeIP>:30002
and log in to the console with the default account and password (admin/Harbor12345
). Click Projects in the left navigation pane and click NEW PROJECT on the Projects page.In the displayed dialog box, set a name (
ks-devops-harbor
) and click OK.Click the project you just created, and click NEW ROBOT ACCOUNT under the Robot Accounts tab.
In the displayed dialog box, set a name (
robot-test
) for the robot account and click SAVE. Make sure you select the checkbox for pushing artifact in Permissions.In the displayed dialog box, click EXPORT TO FILE to save the token.
Enable Insecure Registry
You have to configure Docker to disregard security for your Harbor registry.
Run the following commands to restart Docker for the changes to take effect.
Note
It is suggested that you use this solution for isolated testing or in a tightly controlled, air-gapped environment. For more information, refer to . After you finish the above operations, you can also use the images in your Harbor registry when deploying workloads in your project. You need to create an image Secret for your Harbor registry, and then select your Harbor registry and enter the absolute path of your images in Container Settings under the Container Image tab to search for your images.
Log in to KubeSphere as , go to your DevOps project and create credentials for Harbor in Credentials under DevOps Project Settings.
On the Create Credentials page, set a credential ID (
robot-test
) and select Username and password for Type. The Username field must be the same as the value ofname
in the JSON file you just downloaded and enter the value oftoken
in the file for Password/Token.Click OK to save it.
Create a Pipeline
Go to the Pipelines page and click Create. In the Basic Information tab, enter a name (
demo-pipeline
) for the pipeline and click Next.
Click the pipeline to go to its details page and click Edit Jenkinsfile.
Copy and paste the following contents into the Jenkinsfile. Note that you must replace the values of
REGISTRY
,HARBOR_NAMESPACE
,APP_NAME
, andHARBOR_CREDENTIAL
with your own values.``` pipeline {
agent {node {
label 'maven'
}
}
environment {
}
stages {
stage('docker login') {
container ('maven') {
sh '''echo $HARBOR_CREDENTIAL_PSW | docker login $REGISTRY -u 'robot$robot-test' --password-stdin'''
}
}
}
stage('build & push') {
steps {
container ('maven') {
sh 'git clone https://github.com/kstaken/dockerfile-examples.git'
sh 'cd dockerfile-examples/rethinkdb && docker build -t $REGISTRY/$HARBOR_NAMESPACE/$APP_NAME:devops-test .'
sh 'docker push $REGISTRY/$HARBOR_NAMESPACE/$APP_NAME:devops-test'
}
}
}