`needs`로 잡을 더 빨리 시작하기
Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated
needs 키워드를 사용하여 pipeline의 잡 의존성을 지정합니다. 모노레포: 병렬 실행 경로로 독립적인 서비스를 빌드하고 테스트합니다. 멀티 플랫폼 빌드: 모든 빌드 완료를 기다리지 않고 다른 플랫폼용으로 컴파일합니다.
needs 키워드를 사용하여 pipeline의 잡 의존성을 지정합니다.
잡은 의존성이 완료되는 즉시 시작되며, pipeline 스테이지에 관계없이 실행됩니다.
이를 통해 잡을 더 일찍 실행하고 불필요한 대기를 방지할 수 있습니다.
사용 사례:
-
모노레포: 병렬 실행 경로로 독립적인 서비스를 빌드하고 테스트합니다.
-
멀티 플랫폼 빌드: 모든 빌드 완료를 기다리지 않고 다른 플랫폼용으로 컴파일합니다.
-
빠른 피드백: 테스트 결과와 오류를 더 일찍 확인합니다.
needs: project 및 needs: pipeline 키워드는 잡 의존성 지정에 사용되지 않습니다.
다른 pipeline에서 아티팩트를 가져오려면 needs: project를 사용하세요.
업스트림 pipeline의 상태를 미러링하려면 needs: pipeline을 사용하세요.
needs 동작 방식#
기본적으로 잡은 스테이지 순서대로 실행됩니다. 잡은 이전 스테이지의 모든 잡이 완료될 때까지 기다립니다.
needs를 사용하면 잡이 의존하는 정확한 잡을 지정합니다. 이전 스테이지의 다른 잡이 여전히 실행 중이더라도
해당 의존성이 완료되면 즉시 잡이 시작됩니다.
이를 통해 방향성 비순환 그래프(DAG) 방식의 pipeline을 만들 수 있습니다.
동일한 pipeline에서 스테이지 기반 잡과 needs 의존성 잡을 혼합할 수 있습니다.
또한 needs: []를 사용하면 이전 잡이나 스테이지 완료를 기다리지 않고 즉시 실행되는 잡을 설정할 수 있습니다.
소스 코드를 바로 실행할 수 있고 빌드 결과에 의존하지 않는 린트 잡이나 스캐너를 즉시 실행할 때 흔히 사용합니다.
needs와 스테이지만 사용하는 잡 비교#
needs 사용의 이점을 보여주기 위해 6개의 잡이 있는 pipeline을 구성하는 두 가지 방법을 비교합니다.
이 pipeline은 스테이지로 구성된 6개의 잡을 가집니다. needs 없이는 일부 잡이 독립적이더라도
스테이지의 모든 잡이 완료될 때까지 다음 스테이지가 시작되지 않습니다:
stages:
- build
- test
- deploy
build_app_A:
stage: build
script: echo "Building A..."
build_app_B:
stage: build
script: echo "Building B..."
test_app_A:
stage: test
script: echo "Testing A..."
test_app_B:
stage: test
script: echo "Testing B..."
deploy_app_A:
stage: deploy
script: echo "Deploying A..."
deploy_app_B:
stage: deploy
script: echo "Deploying B..."
이 예시에서 build 스테이지의 모든 잡이 완료될 때까지 테스트 또는 배포 잡이 실행되지 않습니다.
A 잡들이 오래 걸리면 A 잡 완료를 기다리는 동안 B 테스트 및 배포 잡이 지연될 수 있습니다.
needs를 사용하면 두 개의 독립적인 실행 경로를 정의할 수 있습니다. 각 잡은 실제로 필요한 잡에만 의존하며,
두 경로에서 병렬 실행이 가능합니다:
stages:
- build
- test
- deploy
build_app_A:
stage: build
script: echo "Building A..."
build_app_B:
stage: build
script: echo "Building B..."
test_app_A:
stage: test
needs: ["build_app_A"]
script: echo "Testing A..."
test_app_B:
stage: test
needs: ["build_app_B"]
script: echo "Testing B..."
deploy_app_A:
stage: deploy
needs: ["test_app_A"]
script: echo "Deploying A..."
deploy_app_B:
stage: deploy
needs: ["test_app_B"]
script: echo "Deploying B..."
이 예시에서 test_app_B는 build_app_A가 여전히 실행 중이더라도 build_app_B가 성공적으로 완료되는 즉시 실행됩니다.
마찬가지로 deploy_app_B는 build_app_A가 완료되기 전에 실행되어 배포될 수 있습니다.
잡 간 의존성 보기#
pipeline 그래프에서 잡 간의 의존성을 볼 수 있습니다.
이 뷰를 활성화하려면 pipeline 세부 정보 페이지에서:
-
Job dependencies를 선택합니다.
-
선택 사항. Show dependencies 토글로 잡 간의 연결 방식을 표시합니다.
needs 예시#
needs를 사용하여 잡 간의 의존성을 만들고 잡이 시작 대기하는 시간을 줄입니다.
패턴에는 팬아웃, 팬인, 다이아몬드 의존성이 포함될 수 있습니다.
팬아웃#
팬아웃 잡 의존성 그래프를 만들려면 여러 잡이 하나의 잡에 의존하도록 구성합니다.
예를 들어:
stages:
- build
- test
build:
stage: build
script: echo "Building..."
test_unit:
stage: test
needs: ["build"]
script: echo "Unit tests..."
test_integration:
stage: test
needs: ["build"]
script: echo "Integration tests..."
test_performance:
stage: test
needs: ["build"]
script: echo "Performance tests..."
팬인#
팬인 의존성 그래프를 만들려면 하나의 잡이 여러 잡의 완료를 기다리도록 구성합니다. 예를 들어:
stages:
- build
- test
- deploy
build_frontend:
stage: build
script: echo "Building frontend..."
build_backend:
stage: build
script: echo "Building backend..."
test_frontend:
stage: test
needs: ["build_frontend"]
script: echo "Testing frontend..."
test_backend:
stage: test
needs: ["build_backend"]
script: echo "Testing backend..."
deploy:
stage: deploy
needs: ["test_frontend", "test_backend"]
script: echo "Deploying..."
다이아몬드 의존성#
다이아몬드 의존성 그래프를 만들려면 팬아웃과 팬인을 결합합니다. 하나의 잡이 여러 잡으로 팬아웃되고, 이들이 다시 하나의 잡으로 팬인됩니다. 예를 들어:
stages:
- build
- test
- deploy
build:
stage: build
script: echo "Building..."
test_unit:
stage: test
needs: ["build"]
script: echo "Unit tests..."
test_integration:
stage: test
needs: ["build"]
script: echo "Integration tests..."
test_performance:
stage: test
needs: ["build"]
script: echo "Performance tests..."
deploy:
stage: deploy
needs: ["test_unit", "test_integration", "test_performance"]
script: echo "Deploying..."
즉시 시작#
needs: []를 사용하면 pipeline 생성 시 다른 잡이나 스테이지를 기다리지 않고 즉시 잡을 시작할 수 있습니다.
나중 스테이지(예: test)에 포함되어야 하지만 즉시 실행 가능한 린팅 또는 스캔 도구에 사용합니다.
예를 들어:
stages:
- build
- test
- deploy
build_app:
stage: build
script: echo "Building app..."
test_app:
stage: test
script: echo "Testing app..."
lint_yaml:
stage: test
needs: []
script: echo "Linting YAML..."
lint_code:
stage: test
needs: []
script: echo "Linting code..."
deploy_app:
stage: deploy
script: echo "Deploying app..."
이 예시에서 lint_yaml과 lint_code는 needs: []로 인해 build_app이나 test 스테이지 완료를 기다리지 않고 즉시 시작됩니다.
deploy_app은 needs를 사용하지 않으므로 이전 스테이지의 모든 잡이 완료될 때까지 기다립니다.
pipeline 뷰는 스테이지별로 그룹화된 잡을 보여줍니다.
스테이지리스 pipeline#
stage 및 stages 키워드를 생략하고 needs만으로 잡 순서를 정의할 수 있습니다.
stage 키워드가 없는 모든 잡은 기본 test 스테이지에서 실행됩니다:
compile:
script: echo "Compiling..."
unit_tests:
needs: ["compile"]
script: echo "Running unit tests..."
integration_tests:
needs: ["compile"]
script: echo "Running integration tests..."
package:
needs: ["unit_tests", "integration_tests"]
script: echo "Packaging..."
이 pipeline의 구조를 보려면 pipeline 세부 정보 페이지에서 Job dependencies를 선택합니다.
기본 뷰를 사용하면 모든 잡이 test 스테이지에 함께 그룹화됩니다.
선택적 의존성#
파이프라인에 존재하는 경우에만 잡에 의존하려면 needs에 optional: true를 사용합니다.
needs와 rules를 함께 사용할 때 실행되거나 실행되지 않을 수 있는 잡을 처리하는 데 이 옵션을 사용합니다.
예를 들어:
stages:
- build
- test
- deploy
build:
stage: build
script: echo "Building..."
test:
stage: test
needs: ["build"]
script: echo "Testing..."
test_optional:
stage: test
rules:
- if: $RUN_OPTIONAL_TESTS == "true"
script: echo "Optional tests..."
deploy:
stage: deploy
needs:
- job: "test"
- job: "test_optional"
optional: true
script: echo "Deploying..."
이 예시에서:
-
deploy는 다음에 의존합니다:-
test: pipeline에 항상 존재합니다. -
test_optional:RUN_OPTIONAL_TESTS가true일 때만 pipeline에 존재합니다.
-
-
RUN_OPTIONAL_TESTS가:-
false이면test_optional이 pipeline에 존재하지 않고deploy는test완료 후 실행됩니다. -
true이면test_optional이 pipeline에 존재하고deploy는test와test_optional모두 완료될 때까지 기다립니다.
-
optional: true 없이는 deploy 잡이 test_optional을 기대하지만 pipeline에 존재하지 않기 때문에 pipeline 생성에 실패합니다.
needs와 parallel:matrix 결합#
needs 키워드는 parallel:matrix와 함께 작동하여
병렬화된 잡 간의 의존성을 정의합니다.
문제 해결#
오류: 'job'이 pipeline에 존재하지 않음#
needs와 rules를 결합하면 pipeline 생성에 실패하고 다음 오류가 표시될 수 있습니다:
'unit_tests' job needs 'compile' job, but 'compile' does not exist in the pipeline.
This might be because of the only, except, or rules keywords. To need a job that
sometimes does not exist in the pipeline, use needs:optional.
이 오류는 pipeline에 존재하지 않는 다른 잡에 needs를 설정한 경우 발생합니다.
이 문제를 해결하려면 다음 중 하나를 해야 합니다:
-
잡 의존성에
optional: true를 추가하여 필요한 잡이 pipeline에 없을 때 무시되도록 합니다. -
필요한 잡이 항상 실행되도록 해당 잡의
rules구성을 업데이트합니다.
예를 들어:
# 방법 1: 존재하지 않을 수 있는 rules가 있는 잡
compile:
stage: build
rules:
- if: $COMPILE == "true"
script: echo "Compiling..."
unit_tests:
stage: test
needs:
- job: "compile" # $COMPILE == "false"이면 `compile` 잡이 pipeline에 추가되지 않으며
optional: true # 이 needs는 무시됩니다.
script: echo "Running unit tests..."
# 방법 2: 의존 잡과 항상 일치하는 rules가 있는 잡
build:
stage: build
rules:
- if: $BUILD == "true"
script: echo "Building..."
test:
stage: test
rules: # 두 잡 모두 동일한 `rules`를 가지며, 항상 pipeline에 함께 존재합니다.
- if: $BUILD == "true"
needs: ["build"]
script: echo "Testing..."
