Trigger some codes from diffrent pipeline using include and reference

51 Views Asked by At

I have 2 gitlab pipeline, In one pipeline I have all stages and scripts which will execute mainely but also one script availab in another pipeline which I also want to use when my main pipeline trigger.

Sample pipeline:

Main-pipeline.yml

include:
     - 'common/.deploy.yml'
 
stages:
  - deploy
 
variables:
  VAULT_ADDR: example.com
  APPLICATION_NAME: 
    value: ""
    description: "name of the application/should match name inside dependency manager file"

create_approle:
  stage: deploy
  image: hashicorp/vault-k8s
  before_script:
    - echo "Setting up kubeconfig..."
  script:
    - |-
       echo "test"

Now: Second-pipeline deploy.yml

  image: alpine/helm:3.9.4
  stage: deploy
  artifacts:
    paths:
      - $ARTIFACT_PATH
    reports:
      dotenv: env.env
  before_script:
    - env
    - echo "Setting up kubeconfig..."
    - mkdir -p ~/.kube/
    - echo $KUBE_CONFIG_FILE | tr -d ' ' | base64 -d > ~/.kube/config
    - chmod go-r ~/.kube/config
  script:
    - |
    # Get envsubst
    - apk add gettext

.deploy-dev:
  extends: .deploy
  stage: deploy-dev
  environment:
    name: development
  before_script:
    - !reference [ .deploy, before_script ]
.deploy-pre-prod:
  extends: .deploy
  before_script:
    - !reference [ .install-kubectl, script ]
    - !reference [ .install-jf, script ]
    - !reference [ .deploy, before_script ]
    - !reference [ .getLastDeploy, script ]
  rules:
    - if: $CI_PIPELINE_SOURCE == "trigger"
      allow_failure: false
      when: manual
    - if: $CI_PIPELINE_SOURCE == "web"
      allow_failure: false
      when: always
    - if: $DEPLOY_ACTIVATED == "false"
      when: never

.deploy-prod:
  extends: .deploy
  before_script:
    - !reference [ .deploy-pre-prod, before_script ]

.getLastDeploy:
  script:
        echo "test"

.create-namespace:
   script:
    - |
      ./kubectl --kubeconfig ~/.kube/config create namespace $APP_NAMESPACE \
        --dry-run=client \
        -o yaml | ./kubectl --kubeconfig ~/.kube/config apply -f -
.copy-approle:
  script:
    - ./kubectl --kubeconfig ~/.kube/config get secrets --namespace=$APP_NAMESPACE approle-creds -o yaml > secrets.yml
    - |
      if [[ $(grep -L $APPLICATION_NAME secrets.yml) ]]
      then        
        export ROLE_ID_KEY="${APPLICATION_NAME}-role-id"
        export SECRET_ID_KEY="${APPLICATION_NAME}-secret-id"
        if [[ $DEPLOYMENT_VAULT_TOKEN_ENABLED == true ]] || [[ $VAULT_SIDECAR_TOKEN_REQUIRED == true ]]; then
          export VAULT_TOKEN="${APPLICATION_NAME}-vault-token"
        fi 
        ./yq -i eval-all '.data[env(ROLE_ID_KEY)] |= env(ROLE_ID),.data[env(SECRET_ID_KEY)] |= env(SECRET_ID)' secrets.yml
      fi
    - |
      cat secrets.yml | sed "s/namespace: .*/namespace: $APP_NAMESPACE/g" | \
      ./kubectl --kubeconfig ~/.kube/config apply -f - --force

Here in second yml file have code under .create-namespace: and .copy-approle: which I need to trigger from main-pipeline.yml

Please let me know how to do this.

1

There are 1 best solutions below

0
On

To reuse script sections from .create-namespace and .copy-approle jobs in your Main-pipeline.yml file you can use !reference keyword:

create_approle:
  stage: deploy
  image: hashicorp/vault-k8s
  before_script:
    - echo "Setting up kubeconfig..."
  script:
    - !reference [.create-namespace, script]
    - !reference [.copy-approle, script]
    - echo "other commands"