How to dynamically pick a git branch to use in Jenkins build

前端 未结 3 1821
夕颜
夕颜 2021-02-02 09:41

I\'m trying to create a new project configuration for Jenkins build server. To simplify what I\'m trying to do, I will use only two components to describe the problem.

C

3条回答
  •  无人共我
    2021-02-02 10:33

    Using the Credentials Binding Plugin worked very well for me (also mentioned by @zeppelin)

    Steps:

    In the Global credentials section:

    1. Add Credentials of the type: "Username with password". This should be the username and password for component B repository git server using HTTPS protocol (the SSH option is not good for this purpose)

    In your Jenkins job configuration:

    1. Put component A in the regular Source Code Management under the Git section all required fields (Repositories, Branches, etc.).
      • It will clearer and cleaner to put the repository in a sub-directory: under Additional Behaviours choose Check out to a sub-directory and write: component_a
    2. Make sure also to check in Build Triggers the Build when a change is pushed to GitHub
    3. In the Build Environment section tick the Use secret text(s) or file(s)

      • put in Variable some name: MY_CRED
      • in Credentials choose the Specific credentials you created in step 1.

    4. Now using the MY_CRED in the Execute shell code you will have access to the component B repository:

      DIR="component_b"
      if [ "$(ls -A $DIR/.git)" ]; then
          cd $DIR
          git fetch
      else
          git clone https://$MY_CRED@github.com/proj/component_b.git $DIR 
          cd $DIR
      fi
      git show
      

      • Note: you will NOT see the user and password in the logs, so it should be safe. you would see: git clone 'https://****@github.com/proj/component_b.git' component_b
    5. Do all your parsing of config from component A to get the desired tag: TAG=$(cat ./component_a/config.cfg | grep ... | sed ...)

    6. Checkout the desired Tag: cd component_b; git checkout -f $TAG
      • Note: the -f force tag.
    7. Now run the code and test as desired...

提交回复
热议问题