I have been racking my brain around this and I feel like I am going nowhere. I have a project whose code is hosted on Gitlab. My project has two branches, develop and main. I make changes on my develop branch, push these changes which updates my staging instance. When I am ready to deploy live changes, I merge with main which is meant to trigger a deployment to my live instance. This is my gitlab-ci.yml file
default:
image: docker:19.03.10
tags:
- docker
.deployment_script: &deployment_script
- echo "Starting deployment to ECS ..."
- echo "So many lines here that deploy code to aws ecs"
services:
- docker:dind
variables:
REPOSITORY_URL: **********.dkr.ecr.**-****-*.amazonaws.com/********/****
before_script:
- echo "Creating image tag ..."
- IMAGE_TAG="$(echo $CI_COMMIT_SHA | head -c 8)"
stages:
- build
- deploy
build:
stage: build
script:
- echo $DEPLOY_ENVIRONMENT ...
- echo "The script here build my docker container and publishes to aws ecr"
- echo "Build is only meant to be ran when pushing to develop"
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
when: always
deploy-staging:
environment: staging
dependencies:
- "build"
image: registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest
script:
- *deployment_script
rules:
- if: '$CI_COMMIT_BRANCH == "develop"'
when: manual
deploy-production:
environment: production
image: registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest
script:
- *deployment_script
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
when: manual
Every time I deploy to the develop branch, the pipelines are triggered and I successfully deploy my staging application. However, no matter how many times I push to main, nothing is triggered. I have no idea why. I hope someone can point me in the right direction too figure this out.
Related
I have a task:
Scaffold out a job policy pattern that uses feature branches and tags
to gate review, release and staging/production job execution
but I don't really understand this question - what should be done here?
#Edit
There was an answer from #live but now it's removed for some reason.
Anyway, he wrote:
Use GitLab's feature branching and tagging features to manage the
different versions of your code. Whenever you start working on a new
feature, create a new feature branch in GitLab and push your code
changes to that branch. When the feature is complete and ready to be
merged into the main branch, create a new tag in GitLab to mark the
point in the code where the feature was added.
Use the .gitlab-ci.yml file to define rules for when each job should
be run. For example, you might specify that the build job should only
be run when code is pushed to a feature branch, and the deploy job
should only be run when a new tag is created.
Does it mean just to create a feature branch and then in gitlab-ci.yml file add e.g
only:
- master
To run some stage only for e.g master or other specified branch?
I found: https://about.gitlab.com/handbook/customer-success/professional-services-engineering/education-services/gitlabcicdhandsonlab6.html
there is example:
deploy review:
stage: review
# only:
# - branches
# except:
# - master
script:
- echo "Do your average deploy here"
rules:
- if: '$CI_COMMIT_REF_NAME == "master"'
when: never
- if: '$CI_COMMIT_TAG'
when: never
- when: always
environment:
name: review/$CI_COMMIT_REF_NAME
deploy release:
stage: deploy
# only:
# - tags
# except:
# - master
script:
- echo "Deploy to a production environment"
rules:
- if: '$CI_COMMIT_REF_NAME != "master" && $CI_COMMIT_TAG'
when: manual
environment:
name: production
deploy staging:
stage: deploy
# only:
# - master
script:
- echo "Deploy to a staging environment"
rules:
- if: '$CI_COMMIT_REF_NAME == "master"'
when: always
- when: never
environment:
name: staging
what was expected
I am trying to run the below script as gitlab-ci.yml but somehow I am not able to trigger the CICD pipeline with a merge request.
Steps I am following:
Create a branch from feature/new and name it as feature/abc
Make test changes to feature/abc
Create Merge request with target branch as feature/new.
stages: # List of stages for jobs, and their order of execution
- deployment
- testing
workflow:
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "feature/new"'
deployment-dev: # This job runs in the deploy stage.
stage: deployment
image: google/cloud-sdk
services:
- docker:dind
script:
- echo hello
environment:
name: main/$CI_COMMIT_REF_NAME
when: on_success
testing-dev: # This job also runs in the test stage.
stage: testing
image: google/cloud-sdk
services:
- docker:dind
script:
- echo testing in progress
I have like this gitlab ci cd configuration file:
image: docker:git
stages:
- develop
- production
default:
before_script:
- apk update && apk upgrade && apk add git curl
deploy:
stage: develop
script:
- echo "Hello World"
backup:
stage: develop
when:
- manual
- on_success
remove:
stage: develop
when:
- delayed
- on_success
start_in: 30 minutes
In my case job deploy runs automaticaly and job backup must runs manually only when successfully completed job deploy. But in my case this configuration doesn't works and I get error with message:
Found errors in your .gitlab-ci.yml:
jobs:backup when should be one of:
on_success
on_failure
always
manual
delayed
How I can use multiple when option arguments in my case?
Basically you can't because when does not expect an array. You can work around it though with needs. But this solution does only work if you run your jobs in different stages.
image: docker:git
stages:
- deploy
- backup
- remove
deploy:develop:
stage: deploy
script:
- exit 1
backup:develop:
stage: backup
script:
- echo "backup"
when: manual
needs: ["deploy:develop"]
remove:develop:
stage: remove
script:
- echo "remove"
when: delayed
needs: ["backup:develop"]
start_in: 30 minutes
I have a Gitlab CI config that kinda looks like this:
stages:
- test
- deploy
test:
stage: test
only:
- merge_request
- master
script:
- jest --coverage
deploy:
stage: deploy
only:
- master
dependencies:
- test
script:
- make deploy
I only want the tests to be run when a merge request is opened or if we merge to master because I'm only on the free plan on gitlab.com and I'd like to conserve my runner minutes.
If the unit tests ran for every commit we made, we'd always run out of minutes on the 3rd or 4th week.
For the most part, it works. The problem comes from pushing to master directly (which can happen every now and then); test runs twice and at the same time.
I couldn't find anything on Gitlab docs on how to properly approach this. Any help would be great.
I actually don't see why a direct push to master will run your tests twice except when you have an open merge-request which master as source branch.
You can prevent this from happening by using workflow. Furthermore you should use rules instead of only/except as they are not actively developed any more.
workflow:
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
- if: '$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS'
when: never
- if: '$CI_COMMIT_BRANCH'
stages:
- test
- deploy
test:
stage: test
script:
- jest --coverage
rules:
- if: '$CI_COMMIT_BRANCH == "master" || $CI_PIPELINE_SOURCE == "merge_request_event"'
deploy:
stage: deploy
dependencies:
- test
script:
- make deploy
rules:
- if: '$CI_COMMIT_BRANCH == "master"'
I want the build process to run for all branches when I add a tag. Except for the master branch.
deploy_qas:
stage: deploy
script:
- echo "Implatado em QAS"
environment:
name: qas
url: https://env.br
only:
- tags
except:
- master
This way it is not working.
I am using GitLab 13 ce.
The new Gitlab syntax uses rules.
only and except are not being actively developed. rules is the preferred keyword to control when to add jobs to pipelines.
Solution with rules:
deploy_qas:
stage: deploy
script:
- echo "Implatado em QAS"
environment:
name: qas
url: https://env.br
rules:
- if: $CI_COMMIT_BRANCH == 'master'
when: never
- if: $CI_COMMIT_TAG
# `when: always` is implied here