Max
Max

Reputation: 11

CI/CD How to use the ouput from script to run build for specific app

In my repo i have 3 differents APP's I'm trying to run CI/CD pipeline for build the Docker Image and push it to ECR repo. I have 2 stages: Affected: that help me to find spicific project where was changes with follow command.
npx nx print-affected command that save the ouput to "artifacts file" and this allow me to find changes inside at spicific project dir and build the container. Build: I have dependencies of affected stage and also i'm using the script that can read ouput of "artifacts file" My question is, how i can use the output of "artifacts file" and use it for build of specific docker file

stages:
  - affected
  - build

.distributed:
  interruptible: true
  only:
    - main

affected:
  image: node:18
  stage: affected
  cache:
    key:
     files:
      - package-lock.json
    paths:
      - .npm/
  extends: .distributed
  before_script:
    - npm ci --cache .npm --prefer-offline
    - NX_HEAD=$CI_COMMIT_SHA
    - NX_BASE=${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}
  script:
    - echo "$(npx nx print-affected --type=app --select=projects --base=$NX_BASE --head=$NX_HEAD)" > apps.txt
  artifacts:
    paths:
      - apps.txt
      - node_modules/.cache/nx

build:
  stage: build
  dependencies:
    - affected 
  image:
   name: gcr.io/kaniko-project/executor:debug
   entrypoint: [""]
  script:
    -  mkdir -p /kaniko/.docker
    -  echo "{\"credsStore\":\"ecr-login\"}" > /kaniko/.docker/config.json 
    -  for APP in $(cat apps.txt);
       do
        echo $APP;
       done
    -  echo ${CI_PROJECT_DIR}
#    -  /kaniko/executor
#      --context "."
#      --dockerfile "${CI_PROJECT_DIR}/Dockerfile"
#      --destination "$ECR_REPO_CURRENT:${CI_COMMIT_TAG}"

Upvotes: 1

Views: 449

Answers (1)

jcragun
jcragun

Reputation: 2198

You have all the pieces but need to put them in the right order:

build:
  script:
    -  mkdir -p /kaniko/.docker
    -  echo "{\"credsStore\":\"ecr-login\"}" > /kaniko/.docker/config.json 
    -  for APP in $(cat apps.txt);
       do
        echo $APP
        /kaniko/executor --context "." --dockerfile "${CI_PROJECT_DIR}/$APP/Dockerfile" --destination "$ECR_REPO_CURRENT:${CI_COMMIT_TAG}"
       done

In the above script, I assumed each app has its own directory and Dockerfile. If they all share a common Dockerfile, pass build args to kaniko specifying the app.

To improve performance, build the image(s) in parallel.

Upvotes: 0

Related Questions