Gitlab CI/CD running pipeline only on tag push and only if specific files changed - deployment

I have a monorepository with a lot of things that needs to be deployed seperately.
Now i wonder if it is possible to run a task only if a tag is pushed and if there are changes in a certain directory.
I've tried the following:
.components:
only:
changes:
- source/packages/components/**/*
except:
- branches
.components:
only:
refs:
- tags
changes:
- source/packages/components/**/*
.components:
rules:
- if: $CI_COMMIT_TAG
changes:
- source/packages/components/**/*
.components:
rules:
- if: $CI_COMMIT_TAG
changes:
- source/packages/components/**/*
when: always
- when: never
This is one of the .gitlab-ci.yml files i use. Each of then looks the same except of a few things.
.components:
rules:
- if: '$CI_COMMIT_TAG'
changes:
- source/packages/components/**/*
build_componets:
stage: build
extends: .components
needs: []
script:
- echo "Compiling the components code..."
test_componets:
stage: test
extends: .components
needs: ["build_componets"]
script:
- echo "Compiling the components code..."
deploy_componets:
stage: deploy
extends: .components
needs: ["test_componets"]
script:
- echo "Compiling the components code..."
Currently behavior: Every pipeline runs... regardless of this configurations but only when I'm pushing a tag.
All of those files are imported to a main file at the project root.
stages:
- build
- test
- deploy
include:
- source/packages/business-logic/.gitlab-ci.yml
- source/packages/components/.gitlab-ci.yml
- source/applications/native/.gitlab-ci.yml
- source/services/user-service/.gitlab-ci.yml
And the merged YAML looks like this.
---
".business-logic":
rules:
- if: "$CI_COMMIT_TAG"
changes:
- source/packages/business-logic/**/*
when: always
- when: never
build_business_logic:
rules:
- if: "$CI_COMMIT_TAG"
changes:
- source/packages/business-logic/**/*
when: always
- when: never
stage: build
extends: ".business-logic"
needs: []
script:
- yarn install
- yarn workspace #zeou/business-logic build
test_business_logic:
rules:
- if: "$CI_COMMIT_TAG"
changes:
- source/packages/business-logic/**/*
when: always
- when: never
stage: test
extends: ".business-logic"
needs:
- build_business_logic
script:
- echo "Compiling the business-logics code..."
deploy_business_logic:
rules:
- if: "$CI_COMMIT_TAG"
changes:
- source/packages/business-logic/**/*
when: always
- when: never
stage: deploy
extends: ".business-logic"
needs:
- test_business_logic
script:
- echo "Compiling the business-logics code..."
".components":
rules:
- if: "$CI_COMMIT_TAG"
changes:
- source/packages/components/**/*
when: always
- when: never
build_components:
rules:
- if: "$CI_COMMIT_TAG"
changes:
- source/packages/components/**/*
when: always
- when: never
stage: build
extends: ".components"
needs: []
script:
- echo "Compiling the components code..."
test_components:
rules:
- if: "$CI_COMMIT_TAG"
changes:
- source/packages/components/**/*
when: always
- when: never
stage: test
extends: ".components"
needs:
- build_components
script:
- echo "Compiling the components code..."
deploy_components:
rules:
- if: "$CI_COMMIT_TAG"
changes:
- source/packages/components/**/*
when: always
- when: never
stage: deploy
extends: ".components"
script:
- echo "Compiling the components code..."
stages:
- ".pre"
- build
- test
- deploy
- ".post"
before_script:
Can someone tell me what i'm doing wrong ?
Thanks 100 times :D

Use rules: in this case to combine a changes: clause with an if: rule to check if there is a tag.
myjob:
rules:
- if: "$CI_COMMIT_TAG"
changes:
- source/services/user-service/**/*
# ...

Related

Parse Set variables output to template - Azure yml pipeline

I have a pipeline that runs a template pipeline. It looks like this:
resources:
repositories:
- repository: repoName
type: git
name: projectName/repoName
ref: branchName
stages:
- stage: GetLastCommitId
jobs:
- job: lastCommitId
steps:
- checkout: repoName
- bash: |
cd repoName
echo "##vso[task.setvariable variable=commitId;isOutput=true]$(git rev-parse HEAD)"
name: a
- bash: |
echo $(a.commitId)
- checkout: self
- template: templates/bicep.yml
parameters:
environment: dev
lastCommitId: $[stageDependencies.GetLastCommitId.lastCommitId.outputs['a.commitId']]
bash returns me the required ID. Everything as it is meant to be.
Now I want to pass this output value to the template as parameter - lastCommitId.
Is there a way to do this?
Here's what I tried:
- template: templates/bicep.yml
parameters:
environment: dev
lastCommitId: $(a.commitId)
Error: Empty string
- template: templates/bicep.yml
dependsOn: GetLastCommitId
parameters:
environment: dev
lastCommitId: $[stageDependencies.GetLastCommitId.lastCommitId.outputs['a.commitId']]
Error: Can not start pipeline. dependsOn not expected here
- template: templates/bicep.yml
parameters:
environment: dev
dependsOn: GetLastCommitId
lastCommitId: $[stageDependencies.GetLastCommitId.lastCommitId.outputs['a.commitId']]
Error: syntax error: invalid arithmetic operator (error token is ".GetLastCommitId.lastCommitId.outputs['a.commitId']
syntax according to microsoft doc: $[stageDependencies.A.A1.outputs['MyOutputVar.myStageVal']]
Here is documentation from Microsoft: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/set-variables-scripts?view=azure-devops&tabs=bash
The value of the parameter your are trying to set is done before the actually run of the tasks. Hence, the lastCommitId is not set as the value and is resulting in your different approaches into a dissapointed result.
But, depending on what is in your bicep.yml, there is a solution!
With the azure-pipeline.yml and bicep.yml below, you are able to use your lastCommitId in the template:
trigger:
- main
pool:
vmImage: ubuntu-latest
resources:
repositories:
- repository: repoName
type: git
name: projectName/repoName
ref: branchName
stages:
- stage: GetLastCommitId
jobs:
- job: lastCommitId
steps:
- checkout: repoName
- bash: |
cd repoName
echo "##vso[task.setvariable variable=commitId;isOutput=true]$(git rev-parse HEAD)"
name: a
- bash: |
echo $(a.commitId)
- checkout: self
- template: templates/bicep.yml
And the bicep.yml:
stages:
- stage: Template
jobs:
- job: JobInTemplate
variables:
lastCommitId: $[stageDependencies.GetLastCommitId.lastCommitId.outputs['a.commitId']]
steps:
- script: echo the value is $(lastCommitId)

How to trigger a github workflow job only for specific branch using patterns

I would like to trigger a github action wf for a specific branch.
My branch has the name
refs/heads/release/rc22-15.0.0
I would like to trigger the wf or a specific step for all releases refs/heads/release/**
The execution of the wf triggers both steps.
name: wf_logic_test_releasebranch
on:
workflow_dispatch:
push:
branches:
- 'release/**'
- 'feature/**'
- 'main'
jobs:
test_condition:
name: job_test_condition
runs-on: [atc-ubuntu-20.04]
environment: sandbox
steps:
- name: Branch name
if: github.ref == 'refs/heads/release/rc22-15.0.0'
run: echo running on branch ${GITHUB_REF##*/}
- run: echo ${{github.ref}}
By changing the code to:
name: wf_logic_test_releasebranch
on:
workflow_dispatch:
push:
branches:
- 'release/**'
- 'feature/**'
- 'main'
jobs:
test_condition:
name: job_test_condition
runs-on: [atc-ubuntu-20.04]
environment: sandbox
steps:
- name: Branch name
if: github.ref == 'refs/heads/release/**'
run: echo running on branch ${GITHUB_REF##*/}
- run: echo ${{github.ref}}
The execution of the above wf triggers only the second step.
This is what worked for me:
name: wf_logic_test_releasebranch
on:
workflow_dispatch:
push:
branches:
- 'release/**'
- 'feature/**'
- 'main'
jobs:
test_condition:
name: job_test_condition
runs-on: [atc-ubuntu-20.04]
environment: sandbox
steps:
- name: Always call branch name
run: echo running on branch ${GITHUB_REF##*/}
#WF executes step only if branch name is main
- name: Determine IP of runner only for release branch
if:
contains(github.ref, 'release')
run: curl https://api.ipify.org
- name: Show branch name for main and feature branch only
if:
contains(github.ref, 'main') || contains(github.ref, 'feature')
run: echo running on branch ${GITHUB_REF##*/}
job_only_for_main:
name: job_test_only_for_main
runs-on: [atc-ubuntu-20.04]
environment: sandbox
if:
contains(github.ref, 'main')
steps:
- name: Always call branch name
run: echo running on branch ${GITHUB_REF##*/}

where do i put the "environment: " line to deploy to an environment in Azure Devops

I've looked at the instructions here https://learn.microsoft.com/en-us/azure/devops/pipelines/process/environments?view=azure-devops and set up an environment called test. However when I put this environment: test line in the below pipeline I get an error "unexpected value". Where do I need to put the environment: test ?
pr:
branches:
include:
- '*'
trigger:
branches:
include:
- master
pool:
vmImage: ubuntu-latest
stages:
- stage: Build
jobs:
- job: Build
steps:
- template: templates/build.yml
- stage: Release
condition: and(succeeded('Build'), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
jobs:
- job: DeployDev
environment: test
variables:
You need to change your ordinary job into a deployment job :
jobs:
- deployment: DeployDev
environment: test

Sequentially launch of builds of multiple projects in Gitlab CI/CD

I have one project (parent) that triggers a number of child projects (child_1, child_2) I can build parallel, and one (sub_child) more I need to build after successfully completing the previous child builds.
I made follow ci scripts:
parent
variables:
FLAGGER_REBUILD: "true"
COMMON_ONLY: "true"
stages:
- build_parent
- build_child
- build_sub_child
build parent:
stage: build_parent
retry: 2
when: on_success
only:
- master
script:
- echo "PARENT"
- date
.sub_build_template: &build_conf
stage: build_child
when: on_success
only:
refs:
- master
variables:
- $COMMON_ONLY == "true"
rebuild child_1:
<<: *build_conf
trigger:
project: ci_test/child_1
rebuild child_2:
<<: *build_conf
trigger:
project: ci_test/child_2
rebuild sub_child:
stage: build_sub_child
when: on_success
only:
refs:
- master
variables:
- $COMMON_ONLY == "true"
trigger:
project: ci_test/sub_child
child_1, 2
variables:
FLAGGER_REBUILD: "false"
stages:
- build_child_1
- build_sub_child
build image:
stage: build_child_1
retry: 2
when: on_success
only:
- master
script:
- echo "CHILD 1"
- date
rebuild flagger:
stage: build_sub_child
when: on_success
only:
refs:
- master
variables:
- $FLAGGER_REBUILD == "true"
trigger:
project: ci_test/sub_child
sub_child
variables:
FLAGGER_REBUILD: "false"
stages:
- build_sub_child
build flagger:
stage: build_sub_child
retry: 2
when: on_success
script:
- echo "SUB CHILD"
- date
Gitlab made the following scheme of launching:
img_1
But all children and sub_child tasks launch at one time. It's not good.
So, how do I need to change parent, children, or sub_child scripts to gen correct launching of builds?
img_2
img_3
I use Gitlab version 11.10.8-ee and I am not allowed to upgrade the version at the moment. Please help me decide on this task.
Thank you.

GitHub action to add a condition

I have a build yaml file as follows:
name: CI
on:
push:
branches:
- master
jobs:
BuildAndRelease:
strategy:
matrix:
BuildConfiguration:
- debug
- release
max-parallel: 2
runs-on: windows-2019
steps:
- name: List contents of a folder
run: dir
How do I add a condition in this step?
if: and(succeeded(), eq(variables['BuildConfiguration'], 'debug'))
steps:
- name: A conditional step
if: ${{ success() && matrix.BuildConfiguration == 'debug' }}
The expression syntax, operators and functions are documented here: https://docs.github.com/en/actions/learn-github-actions/expressions