Jenkins将触发器块传递给共享库管道

时间:2019-08-12 21:46:09

标签: jenkins jenkins-pipeline jenkins-groovy jenkins-declarative-pipeline jenkins-shared-libraries

我有一个共享库,其中包含一个声明性管道,许多作业正在使用该管道进行构建。 但是,我希望能够将触发块从Jenkinsfile传递到共享库,因为我想通过Cron触发某些作业,我想通过SNS触发的其他作业,以及我要通过上游作业触发的其他作业。

有没有办法做到这一点?我尝试过的一切都失败了

我尝试过

static void Main(string[] args)
        {
            //Assuming these are the names that where added with their info
            listOfStudentsNames = new List<string>() { "Tom", "Andrea", "Zoey", "Mike", "Pete" };
            listOfStudentsHomeTown = new List<string>() { "home1", "home2", "home3", "home4", "home5" };
            listOfStudentsFavoriteFood = new List<string>() { "food1", "food2", "food3", "food4", "food5" };
            listOfStudentsFavoriteColor = new List<string>() { "color1", "color2", "color3", "color4", "color5" };
            Sort(listOfStudentsNames, 
                listOfStudentsHomeTown, 
                listOfStudentsFavoriteFood, 
                listOfStudentsFavoriteColor);
        }

        static List<string> listOfStudentsNames = new List<string>();
        static List<string> listOfStudentsHomeTown = new List<string>();
        static List<string> listOfStudentsFavoriteFood = new List<string>();
        static List<string> listOfStudentsFavoriteColor = new List<string>();

        public static void Sort(List<string> names, params List<string>[] otherInfo)
        {
            // I use params for the other info because you will be able 
            // to add more info-lists without making any changes to this code

            // save the index of each name
            var nameWithIndexes = names.Select(x => new Tuple<string, int>(x, names.IndexOf(x))).ToList();

            //sort the list by the names
            nameWithIndexes.Sort((x, y) => x.Item1.CompareTo(y.Item1));

            names.Clear();
            names.AddRange(nameWithIndexes.Select(x => x.Item1));

            // get de index of the names afert sorting
            var order = nameWithIndexes.Select(x => x.Item2);

            // sort the other info by the index order int the variable "order"
            for (int i = 0; i < otherInfo.Length; i++)
            {
                var newOrder = new List<string>();
                foreach (var index in order)
                {
                    newOrder.Add(otherInfo[i][index]);
                }
                otherInfo[i].Clear();
                otherInfo[i].AddRange(newOrder);
            }               
        }

失败

#Jenkinsfile
@Library('build') _
buildAmi{
OS = "amazonlinux"
owners = "amazon"
filters = "\"Name=name,Values=amzn-ami-hvm-*-x86_64-gp2\""
template = "linux_build_template.json"
trigger = triggers {
    cron('0 H(06-07) * * *')
}

#Shared Lib
pipeline {
    $buildArgs.trigger

还尝试仅将cron计划传递到共享库中,例如

Not a valid section definition

但这会导致错误

triggers {
    cron("${buildArgs.cron}")
}

尝试了其他各种方法,但是声明式样式似乎需要一个内部仅包含触发器的触发器块。

有人知道实现我想做的事情的方法吗?

0 个答案:

没有答案
相关问题