通过推送到CodeCommit来触发Jenkins构建

时间:2017-03-29 15:17:37

标签: git jenkins aws-codecommit

我创建了一个存储库git aws codeCommit,当我在分支X上推送一些东西时,我需要启动一个作业Jenkins。

图片中是我的配置:

enter image description here

但它不起作用。当我将源代码推送到我的分支代码时,没有任何反应。你有什么想法吗?

4 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

codecommit不支持像GitHub那样触发Jenkins。但是可以设置Jenkins以轮询codecommit

https://aws.amazon.com/blogs/devops/integrating-aws-codecommit-with-jenkins/

答案 2 :(得分:0)

当我向CodeCommit推送内容时,我使用AWS Lambda触发Jenkins。您知道可以使用cURL手动触发Jenkins:

curl --request POST --url'http://example.com/jenkins/job/MYJOBNAME/job/MYREPONAME/job/master/build?delay=0sec'--user JenkinsUserId:JenkinsAPIToken

只需将此cURL转换为nodejs中的POST请求。

  • 在“配置”部分中找到您的Jenkins凭据。
  • 使用NodeJS创建新的Lambda
  • 向此Lambda添加CodeCommit触发器。
  • 将以下代码粘贴到您的Lambda中,并根据需要替换凭据
[username@localhost ~]$ sudo yum install ncurses-devel
[sudo] password for username: 
Fedora 28 - x86_64                              371 kB/s |  60 MB     02:46    
Last metadata expiration check: 0:01:24 ago on Sat 04 Aug 2018 09:34:19 PM PDT.
Dependencies resolved.
================================================================================
 Package               Arch        Version                   Repository    Size
================================================================================
Installing:
 ncurses-devel         x86_64      6.1-5.20180224.fc28       updates      527 k
Upgrading:
 ncurses               x86_64      6.1-5.20180224.fc28       updates      377 k
 ncurses-base          noarch      6.1-5.20180224.fc28       updates       80 k
 ncurses-libs          x86_64      6.1-5.20180224.fc28       updates      307 k
Installing dependencies:
 ncurses-c++-libs      x86_64      6.1-5.20180224.fc28       updates       54 k

Transaction Summary
================================================================================
Install  2 Packages
Upgrade  3 Packages

Total download size: 1.3 M
Is this ok [y/N]: y
Downloading Packages:
(1/5): ncurses-c++-libs-6.1-5.20180224.fc28.x86  51 kB/s |  54 kB     00:01    
(2/5): ncurses-base-6.1-5.20180224.fc28.noarch.  65 kB/s |  80 kB     00:01    
(3/5): ncurses-libs-6.1-5.20180224.fc28.x86_64. 127 kB/s | 307 kB     00:02    
(4/5): ncurses-devel-6.1-5.20180224.fc28.x86_64 152 kB/s | 527 kB     00:03    
(5/5): ncurses-6.1-5.20180224.fc28.x86_64.rpm   234 kB/s | 377 kB     00:01    
--------------------------------------------------------------------------------
Total                                           296 kB/s | 1.3 MB     00:04     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Upgrading        : ncurses-base-6.1-5.20180224.fc28.noarch                1/8 
  Upgrading        : ncurses-libs-6.1-5.20180224.fc28.x86_64                2/8 
  Installing       : ncurses-c++-libs-6.1-5.20180224.fc28.x86_64            3/8 
  Installing       : ncurses-devel-6.1-5.20180224.fc28.x86_64               4/8 
  Upgrading        : ncurses-6.1-5.20180224.fc28.x86_64                     5/8 
  Cleanup          : ncurses-6.1-4.20180224.fc28.x86_64                     6/8 
  Cleanup          : ncurses-libs-6.1-4.20180224.fc28.x86_64                7/8 
  Cleanup          : ncurses-base-6.1-4.20180224.fc28.noarch                8/8 
  Running scriptlet: ncurses-base-6.1-4.20180224.fc28.noarch                8/8 
  Verifying        : ncurses-devel-6.1-5.20180224.fc28.x86_64               1/8 
  Verifying        : ncurses-c++-libs-6.1-5.20180224.fc28.x86_64            2/8 
  Verifying        : ncurses-libs-6.1-5.20180224.fc28.x86_64                3/8 
  Verifying        : ncurses-base-6.1-5.20180224.fc28.noarch                4/8 
  Verifying        : ncurses-6.1-5.20180224.fc28.x86_64                     5/8 
  Verifying        : ncurses-6.1-4.20180224.fc28.x86_64                     6/8 
  Verifying        : ncurses-base-6.1-4.20180224.fc28.noarch                7/8 
  Verifying        : ncurses-libs-6.1-4.20180224.fc28.x86_64                8/8 

Installed:
  ncurses-devel.x86_64 6.1-5.20180224.fc28                                      
  ncurses-c++-libs.x86_64 6.1-5.20180224.fc28                                   

Upgraded:
  ncurses.x86_64 6.1-5.20180224.fc28                                            
  ncurses-base.noarch 6.1-5.20180224.fc28                                       
  ncurses-libs.x86_64 6.1-5.20180224.fc28                                       

Complete!


[username@localhost project]$ sudo yum install ncurses-libs
Last metadata expiration check: 0:05:45 ago on Sat 04 Aug 2018 09:34:19 PM PDT.
Package ncurses-libs-6.1-5.20180224.fc28.x86_64 is already installed, skipping.
Dependencies resolved.
Nothing to do.
Complete!




[username@localhost project]$ yum provides libncurses.so.5
Fedora 28 - x86_64                                                                                                     3.1 MB/s |  60 MB     00:19    
Last metadata expiration check: 0:00:28 ago on Sat 04 Aug 2018 09:48:51 PM PDT.
ncurses-compat-libs-6.1-5.20180224.fc28.i686 : Ncurses compatibility libraries
Repo        : updates
Matched from:
Provide    : libncurses.so.5

ncurses-compat-libs-6.1-4.20180224.fc28.i686 : Ncurses compatibility libraries
Repo        : fedora
Matched from:
Provide    : libncurses.so.5

答案 3 :(得分:0)

使用git plugin push notifications的另一种解决方案。以下是lambda,您可以将其注册为回购的触发器。

好处是:不需要修改詹金斯的工作(假设已启用SCM轮询),不需要特殊的凭据,也不需要事先知道这些工作。

const url = require('url');
const HEAD_PREFIX='refs/heads/';
const JENKINS_URL=url.parse(url.resolve(process.env.JENKINS_URL, "git/notifyCommit"));
const CLONE_PROTOCOL=process.env.CLONE_PROTOCOL ? process.env.CLONE_PROTOCOL : "ssh";
const http = require(JENKINS_URL.protocol.split(":")[0]);

exports.handler = async (event) => {
    await Promise.all(event.Records.map(handleRecord));
};

async function handleRecord(record) {
    var repo = cloneUrl(record.eventSourceARN);
    await Promise.all(
        record.codecommit.references
            .filter(ref => ref.ref.startsWith(HEAD_PREFIX))
            .map(ref => pollJenkins(repo, ref))
    );
}

function cloneUrl(repoArn) {
    var splitted = repoArn.split(":");
    return `${CLONE_PROTOCOL}://git-codecommit.${splitted[3]}.amazonaws.com/v1/repos/${splitted[5]}`
}

function pollJenkins(repo, ref) {
    const path = url.format(Object.assign({}, JENKINS_URL, {
        query: {
            url: repo,
            branches: ref.ref.substring(HEAD_PREFIX.length),
            sha1: ref.commit
        }
    }));
    return new Promise(function(resolve, reject){
        const req = http.get(path, res => {
            if (res.statusCode > 100 && res.statusCode < 300) {
                console.log("success: " + path);
                resolve();
                res.resume();
            } else {
                res.on('data', (chunk) => {
                  console.log(`BODY: ${chunk}`);
                });
                res.on('end', () => {
                  reject("Unexpected status code " + res.statusCode);
                });
            }
        });
        req.on('error', reject);
   });
}

提供JENKINS_URL并代码提交CLONE_PROTOCOL作为环境变量。