使用 Jenkins Pipeline 登录 Azure 容器应用程序失败:“访问被拒绝”错误

问题描述 投票:0回答:1

我遇到了一个问题:我的 Jenkins 管道无法登录到 Azure,导致“访问被拒绝”错误。

我正在致力于自动化一个流程,每次推送到 GitHub 都会触发一个新的 Docker 镜像构建,然后将其上传到 Azure 容器应用程序。

目前,我的设置包括:

  • 托管我的服务器映像(分析日志服务器)的 Azure 容器应用程序。
  • Jenkins 管道配置为:
    • 在 GitHub 推送上触发构建。 [通过]
    • 从存储库构建 Docker 映像。 [通过]
    • 将镜像推送到 Docker Hub。 [通过]
    • 尝试连接到 Azure。 [失败]

更多细节:

  • 我已经在本地安装了 Jenkins,可以通过 localhost:8080 访问它
  • 我正在使用 Ngrok 公开 GitHub webhook 的公共地址。
  • 按照 Jenkinsfile 中的指定,与 Azure 的连接是使用 Azure 服务主体建立的。
  • 我已在 Jenkins 中将相关凭据配置为服务主体类型。
  • 当尝试在终端中使用相同的命令和相同的值进行连接时,连接成功。

Jenkins 中的输出:

[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy to Azure Container App)
[Pipeline] script
[Pipeline] {
[Pipeline] withCredentials
Masking supported pattern matches of %AZURE_SUBSCRIPTION_ID% or %AZURE_TENANT_ID% or %AZURE_CLIENT_SECRET% or %AZURE_CLIENT_ID%
[Pipeline] {
[Pipeline] sh
+ az login --service-principal -u **** -p **** -t ****
Access is denied.```


the Jenkins File:

管道{ 任何代理

environment {
    // Define environment variables
    DOCKER_HUB_USERNAME = // my Docker Hub username
    DOCKER_IMAGE_NAME = // my Docker image name
    IMAGE_TAG = "${DOCKER_HUB_USERNAME}/${DOCKER_IMAGE_NAME}:${BUILD_NUMBER}"
    RESOURCE_GROUP = 'AI-Dev'
    CONTAINER_APP =  // name of my container app on Azure
    // Credentials IDs
    DOCKER_HUB_CREDENTIALS_ID =  // The ID of Docker Hub credentials in Jenkins
    AZURE_CREDENTIALS_ID =  // The ID of Azure service principal credentials in Jenkins
    
}

stages {
    stage('Checkout') {
        steps {
            // Checks out the GitHub repository
            checkout scm
        }
    }

    stage('Build and Push Docker Image') {
        steps {
            script {
                // Login to Docker Hub
                
                withCredentials([usernamePassword(credentialsId: DOCKER_HUB_CREDENTIALS_ID, usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
                    
                    sh "docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD"
                }
                // Build the Docker image
                sh "docker build -t $IMAGE_TAG ."
                // Push the Docker image to Docker Hub
                sh "docker push $IMAGE_TAG"
            }
        }
    }

    stage('Deploy to Azure Container App') {
        steps {
            script {
                // Login to Azure using the Azure CLI
                
                withCredentials([azureServicePrincipal('008ad470-0ab1-4486-a07c-e5b17cc18ace')]) {
                    sh 'az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID'
                }
                // Set the Azure subscription
                sh "az account set --subscription \${AZURE_SUBSCRIPTION_ID}"

                // Deploy the image from Docker Hub to the Azure Container App
                sh """
                az containerapp update \\
                    --name $CONTAINER_APP \\
                    --resource-group $RESOURCE_GROUP \\
                    --image $IMAGE_TAG \\
                    --registry-login-server index.docker.io \\
                    --registry-username $DOCKER_HUB_USERNAME \\
                    --registry-password $DOCKER_PASSWORD
                """
            }
        }
    }
}

post {
    always {
        // Echo for debugging purposes
        echo "Build, push, and deployment process completed."
        echo "Docker Image: $IMAGE_TAG"
        echo "Deployed to Azure Container App: $CONTAINER_APP in Resource Group: $RESOURCE_GROUP"
    }
}

}

azure jenkins-pipeline azure-cli azure-service-principal azure-container-apps
1个回答
0
投票

尝试从 Jenkins 管道连接到 Azure 时出现“访问被拒绝”错误可能是由于凭据不正确或权限不足造成的。除了我在评论部分提到的几点。请您检查一次以下内容。

  1. 仔细检查 Azure 服务主体凭据是否正确,并具有访问 Azure 容器应用程序所需的权限

  2. 确保在 Jenkins 中正确配置了 Azure 服务主体凭据,即 _用户名和密码

    • 用户名:服务主体
      appId
    • 密码:服务主体
      password
    • ID:凭证标识符(例如
      AzureServicePrincipal
      Jenkins-creds here
  3. 检查订阅-> IAM-> 角色分配-> 你的詹金斯是否有

    contributor
    访问权限 Jenkins-role

  4. 确保您启用了以下两个插件。 enter image description here enter image description here

如果所有这些都正确更新,那么您应该能够将 Jenkins 连接到 azure。有关更多详细信息,请参阅下面的共享文档。

参考资料:

© www.soinside.com 2019 - 2024. All rights reserved.