Jenkins管道中的shell脚本是否异步运行?

时间:2018-12-10 07:48:14

标签: shell jenkins sh jenkins-pipeline

我有一个用于构建我的android应用程序的管道。在某些阶段,我有shell脚本。当我一个个运行一个阶段(通过评论其他阶段)时,一切正常,但是当我一起运行它们时,我会看到奇怪的行为。

似乎shell脚本正在 parallel !!

中运行

这是我的jenkinsfile:

pipeline{
    agent any

    stages{
        stage("Clean"){
          agent{
            node{
              label 'master'
              customWorkspace getMainDirectory()
            }
          }
          steps{
              sh """#!/bin/bash
                rm -rf Corona
                rm -rf native-corona-android
                cd ..
                cp -a TemplateWorkspace/. ${getCoronaBranch()}-${getNativeBrach()}
                """
          }
        }
        stage("pull native repo"){
            agent{
              node{
                label 'master'
                customWorkspace getNativeProjectPath()
              }
            }
            steps{
                echo "pulling native"
                git(
                   url: nativeRepositoryAddress,
                   credentialsId: credentialsId,
                   branch: getNativeBrach()
                )
                echo "pulling done"
            }
        }
        stage("pull corona repo"){
            agent{
              node{
                label 'master'
                customWorkspace getCoronaProjectPath()
              }
            }
            steps{
                echo "pulling corona"
                git(
                   url: coronaRepositoryAddress,
                   credentialsId: credentialsId,
                   branch: getCoronaBranch()
                )
                echo "pulling done"
            }
        }
        stage("build"){
            environment {
                docDir = getMainDirectory()
                ANDROID_HOME = getAndroidSDKLocation()
            }
            agent{
              node{
                label 'master'
                customWorkspace getNativeProjectPath()
              }
            }
            steps{
                sh """#!/bin/bash
                ./gradlew clean
                ./gradlew changeFiles --stacktrace --no-daemon
                ./gradlew assembleDebug --stacktrace --no-daemon
                """
            }
        }
        stage("move build files"){
          agent{
            node{
              label 'master'
              customWorkspace getGradleBuildLocation()
            }
          }
          steps{
              sh """#!/bin/bash
                yes | cp -rf * ../../../../JenkinsBuilds/${getOutputFolder()}/
                """
          }
        }
    }

}

我只想同步运行步骤(当然还有shell脚本),我的问题是什么?

这是我看到的:

在“清理”步骤中,将删除文件夹,并将模板文件夹的新副本复制到工作目录。步骤“拉本地回购”和“拉电晕回购”执行其应做的工作。但是在“构建”步骤中,我可以看到“ native-corona-android”文件的一部分消失了,“ gradlew”脚本被删除了。我还看到了整个“ native-corona-android”文件夹被删除的情况。然后我以为再次执行“清理”步骤中的脚本。

谢谢

1 个答案:

答案 0 :(得分:0)

除非您使用#include <type_traits> #include <utility> template<class T> using GetFromType = typename T::FromType; template<class T> using GetToType = typename T::ToType; // `get` and `set` are fundamental operations for Lens, // this Mixin will add utilities based on `get` and `set` template<class Derived> struct LensMixin { Derived &self() { return static_cast<Derived&>(*this); } // f has type: A& -> void template<class D, class Mutation> auto modify(D const &d, Mutation f) { auto a = self().get(d); f(a); return self().set(d, a); } }; template< class Getter, class Setter, class D, class A > struct SimpleLens : LensMixin<SimpleLens<Getter, Setter, D, A>> { static_assert(std::is_same< std::invoke_result_t<Getter, const D&>, const A&>{}, "Getter should return const A& for (const D&)"); static_assert(std::is_same< std::invoke_result_t<Setter, const D&, A>, D>{}, "Setter should return D for (const D&, A)"); using FromType = D; using ToType = A; SimpleLens(Getter getter, Setter setter) : getter(getter) , setter(setter) {} A const &get(D const &d) { return getter(d); } D set(D const &d, A newa) { return setter(d, newa); } private: Getter getter; Setter setter; }; template< class LensDA, class LensAB > struct ComposedLens : LensMixin<ComposedLens<LensDA, LensAB>> { static_assert(std::is_same< GetToType<LensDA>, GetFromType<LensAB> >{}, "Cannot compose two Lens with wrong intermediate type"); using FromType = GetFromType<LensDA>; using ToType = GetToType<LensAB>; private: using intermediateType = GetToType<LensDA>; using D = FromType; using B = ToType; LensDA da; LensAB ab; public: ComposedLens(LensDA da, LensAB ab) : da(da), ab(ab) {} B const &get(D const &d) { return ab.get(da.get(d)); } D set(D const &d, B newb) { const auto &a = da.get(d); auto newa = ab.set(a, newb); return da.set(d, newa); } }; namespace detail { template<class LensDA, class LensAB> auto MakeComposedLens(LensDA da, LensAB ab) { return ComposedLens<LensDA, LensAB> { da, ab }; } template<class D, class A, class Getter, class Setter> auto MakeSimpleLens(Getter getter, Setter setter) { return SimpleLens<Getter, Setter, D, A> { getter, setter }; } } template<class LensDA, class LensAB> auto lens_composer(LensDA da, LensAB ab) { return detail::MakeComposedLens (da, ab); } #include <memory> template<class T> struct immut { T const& get() const {return *state;} immut(T in):state(std::make_shared<T>(std::move(in))){} private: std::shared_ptr<T const> state; }; #define MAKE_SIMPLE_LENS(D, A, Aname) \ detail::MakeSimpleLens<D, A>( \ +[] (D const &d) -> A const & { \ return d . Aname . get(); \ }, \ +[] (D const &d, A newa) -> D { \ D newd = d; \ newd . Aname = newa; \ return newd; \ }) struct Charlie { int id = 0; }; struct Alice{ immut<Charlie> charlie; }; struct Anna {}; struct Bob { immut<Alice> alice; immut<Anna> anna; }; auto alice_charlie_lens = MAKE_SIMPLE_LENS(Alice, Charlie, charlie); auto bob_alice_lens = MAKE_SIMPLE_LENS(Bob, Alice, alice); auto bob_charlie_lens = lens_composer(bob_alice_lens, alice_charlie_lens); static_assert(std::is_same<GetFromType<decltype(bob_charlie_lens)>, Bob>{}); static_assert(std::is_same<GetToType<decltype(bob_charlie_lens)>, Charlie>{}); #include <iostream> int main() { immut<Charlie> charlie{Charlie{77}}; immut<Alice> alice{Alice{charlie}}; immut<Anna> anna{Anna{}}; Bob bob{alice, anna}; std::cout << "bob -> anna: " << static_cast<void const*>(&bob.anna.get()) << "\n"; std::cout << "bob -> charlie: " << bob_charlie_lens.get(bob).id << "\n"; // Bob newbob = bob_charlie_lens.set(bob, Charlie{148}); Bob newbob = bob_charlie_lens.modify(bob, [] (auto &charlie) { charlie.id += (148 - 77); }); std::cout << "new bob -> anna: " << static_cast<void const*>(&bob.anna.get()) << "\n"; std::cout << "old bob -> charlie: " << bob_charlie_lens.get(bob).id << "\n"; std::cout << "new bob -> charlie: " << bob_charlie_lens.get(newbob).id << "\n"; } 指令,否则所有步骤都应同步运行。

您观察到什么行为?哪些步骤并行运行?