I want to run a specific job in a pipeline , I thought assigning a tag for the job and then specifying this tag again in the post method will fulfill my needs .The problem is when I trigger using the api(post) , all the jobs in the pipeline are triggered event though only one of this tagged .
gitlab-ci.yml :
job1:
script:
- echo "helloworld!"
tags : [myTag]
job2:
script:
- echo "hello gitlab!"
the api call :
curl -X POST -F token="xxx" -F ref="myTag" https://gitlab.com/api/v4/projects/12345678/trigger/pipeline
add a variable to your trigger api call as shown here:
https://docs.gitlab.com/ee/ci/triggers/#making-use-of-trigger-variables
then use the only prperty
inside your gitlab.yml file
as shown here :
https://docs.gitlab.com/ee/ci/variables/#environment-variables-expressions
then only if the variable exists the job will be execute
for example
job1:
script: echo "HELLO"
only:
variables:
- $variables[API_CALL]=true
Probably changes in GitLab makes answers above not working.
The
only:
variables:
- $variables[....]
syntax trigger CI Lint.
For others that come here like me, here's how I trigger a specific job:
job1:
script:
- echo "HELLO for job1"
- "curl
--request POST
--form token=$CI_JOB_TOKEN
--form ref=master
--form variables[TRIGGER_JOB]=job2
https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/trigger/pipeline"
except:
- pipelines
job2:
script: echo "HELLO for job2"
only:
variables:
- $TRIGGER_JOB == "job2"
⚠️ Note the except - pipelines in job1, else, you go in infinite Child pipeline loop!
By using variables you can do:
Use this curl command to trigger the pipeline with a variable
curl --request POST --form token=${TOKEN} --form ref=master --form "variables[TRIGERRED_JOB]=job1" "https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/trigger/pipeline"
Ofcourse you have to set the variable accordingly.
Define your jobs with the appropriate variable:
job1:
script: echo "HELLO for job1"
only:
variables:
- $variables[TRIGERRED_JOB] == "JOB1"
job2:
script: echo "HELLO for job2"
only:
variables:
- $variables[TRIGERRED_JOB] == "JOB2"
if you are running the curl from inside another/same job you can use ${CI_JOB_TOKEN} instead of $TOKEN and
https://docs.gitlab.com/ee/ci/triggers/#making-use-of-trigger-variables
Related
I have 2 different Jenkins pipeline (Say Pipeline1 and Pipeline2) in 2 different Jenkins hosts - Say Host - Jenkins1 and Jenkins2.
Jenkins1 with Pipeline1 has different stages for Deployment in an environment.
Jenkins2 with Pipeline2 has the stages for smoke testing for the same environment for different types.
I have taken the urls from Jenkins2 with Pipeline2 and have them ready as below:
These are the urls to be triggered after the deployment in Jenkins1 - Pipeline1.
https://test-jenkins2.com/job/TestRepo/buildWithParameters?token=test&testType=4
https://test-jenkins2.com/job/TestRepo/buildWithParameters?token=test&testType=5
https://test-jenkins2.com/job/TestRepo/buildWithParameters?token=test&testType=6
https://test-jenkins2.com/job/TestRepo/buildWithParameters?token=test&testType=7
So how do I trigger these urls in a stage step in Jenkins1 and Pipeline1 with my credentials?
stages {
stage('Trigger Testing URLs') {
agent { label '' }
steps {
??????HOW To Trigger the above Jenkins URL in this stage???????
}
}
You can simply use curl to achieve this. Since you already have the URL to call:
curl -X POST -u username:password -g https://test-jenkins2.com/job/TestRepo/buildWithParameters?token=test&testType=4
Curl is available for both Windows and Linux systems so this should be applicable either way.
stage('Trigger Testing URLs') {
agent { label '' }
steps {
curl -X POST -u username:password -g https://test-jenkins2.com/job/TestRepo/buildWithParameters?token=test&testType=4
curl -X POST -u username:password -g https://test-jenkins2.com/job/TestRepo/buildWithParameters?token=test&testType=5
...
}
}
I've managed to get what docker images have been deployed but it has to be written in groovy.
I have the following:
sh script: '''
export PATH=\"$PATH\":\"${WORKSPACE}\"
for docker-image in interface data keycloak artifactory ; do
DOCKERHOST=`echo ${DOCKERURL}/images-rancher/$docker-image | sed 's!^localhost://!!g'`
DOCKERVERSION=`docker image ls ${DOCKERHOST} --format '{{ json .Tag }}' | head -1`
echo "${DOCKERHOST} - ${DOCKERVERSION}"
done
'''
Changing it into groovy:
def image = [ "interface", "data" , "keycloak", "artifactory" ]
.
.
.
for docker-image in image
println docker-image
How would you put that in a groovy script?
Thanks
Here's how you can get most of the way to using Groovy instead of bash. The doRegexManipulation() function is left as an exercise for you to implement.
Note that the docker image ls sh step is still required, and cannot be translated to "pure" Groovy.
withEnv(["PATH=${env.PATH}:${env.WORKSPACE}"]) {
def images = [ "interface", "data" , "keycloak", "artifactory" ]
for (String docker_image : images) {
def DOCKERHOST = doRegexManipulation("${DOCKERURL}/images-rancher/$docker_image")
def DOCKERVERSION = sh(
script: """docker image ls '${DOCKERHOST}' --format '{{ json .Tag }}' | head -1""",
returnStdout: true,
)
echo "${DOCKERHOST} - ${DOCKERVERSION}"
}
}
If you wanted to, you can go one step further and replace the head -1 part with Groovy code, since that can be done in Groovy as well.
The withEnv step is documented here. It is used to set environment variables for a block of Groovy code, thereby making those environment variables available to any child processes spawned in the block of Groovy code.
Basically I'm trying to skip the build if it's not a pull request or a certain branch, however I don't seem to be able to skip a job or a part of the workflow if this fails, so far the problem is that circleci step halt does nothing in my pipelines, example config here:
version: 2.1
orbs:
hello: circleci/hello-build#0.0.5
jobs:
build:
docker:
- image: docker:17.05.0-ce-git
steps:
- checkout
- setup_remote_docker
- run:
command: |
if [[ $(echo "$CIRCLE_PULL_REQUEST $CIRCLE_PULL_REQUESTS" | grep -c "pull") -gt 0 ]]; then
echo "Do stuff if it's a PR"
else
echo "Not a PR, Skipping."
circleci step halt # does nothing
circleci-agent step halt # does nothing
exit 0
fi
workflows:
"Hello Workflow":
jobs:
- hello/hello-build:
requires:
- build
filters:
branches:
only:
- testing
- /^(?!pull\/).*$/
tags:
only:
- /^pull\/.*$/
- build:
filters:
branches:
only:
- testing
- /^(?!pull\/).*$/
tags:
only:
- /^pull\/.*$/
This does not fail, and it works on pull requests but the hello/hello-build is executed anyway despite the circleci step halt commands.
Any help would be appreciated, thanks!
After creating a thread in their forums this is what worked: https://discuss.circleci.com/t/does-circleci-step-halt-works-with-version-2-1/36674/4
Go to account settings -> Personal API Tokens -> New token. Once you have the token go to the project and create a new environment variable something like CIRCLE_TOKEN and save it there.
Then in the config.yml you can run something like this to cancel the current workflow:
curl -X POST https://circleci.com/api/v2/workflow/${CIRCLE_WORKFLOW_ID}/cancel -H 'Accept: application/json' -u '${CIRCLE_TOKEN}:'
Then you will see something like:
I'm currently digging in Gitlab CI. I would like to add a way in my YAML files to tag my docker images with a version number composed in the following fashion : MajorVersion.Minorversion.AutoincrementedGlobalversionNumber
I would like to auto-increment the globally defined variable "AutoincrementedGlobalversionNumber" each time I deploy.
I have used CI_PIPELINE_IID however it keeps incrementing for each pipeline request, I need something to keep a version where I can keep track of and it should increment only when I pack and deploy.
variables:
CI_VERSION: "1.0.${CI_PIPELINE_IID}"
build-master:
stage: build
script:
- docker build --pull -t "$CI_REGISTRY_IMAGE" -t "$CI_REGISTRY_IMAGE:$CI_VERSION" ./postfix
- docker push "$CI_REGISTRY_IMAGE"
only:
- master
You probably can't do this with the default GitLab CI variables, but there could be a workaround along the lines of (untested):
Get the registry ID with something like:
$ registry_id=$(curl -s -XGET --header "PRIVATE-TOKEN: $TOKEN" "https://gitlab.com/$PROJECT_PATH/container_registry.json" | jq '.[].id')
Query said registry to get the name:
curl -s -XGET --header "PRIVATE-TOKEN: $TOKEN" "https://gitlab.com/$PROJECT_PATH/registry/repository/$registry_id/tags?format=json" | jq
eg returns the following and you can grep the name for GlobalVersionNumber:
[
{
"name": "latest",
"location": "registry.gitlab.com/mwasilewski/helm:latest",
"revision": "85a403337a56e9e6409dfb8185bf9aa5c2135f9a437bd75da82d27471c71feb4",
"short_revision": "85a403337",
"total_size": 152246865,
"created_at": "2016-12-11T08:31:30.126+00:00",
"destroy_path": "/mwasilewski/helm/registry/repository/31074/tags/latest"
}
]
Continue with your Docker build and push, after incrementing the GlobalVersionNumber you get back.
NB: this assumes you are using GitLab's Container Registry
Resources:
https://gitlab.com/gitlab-org/gitlab-ce/issues/40826
I have a stage test and production. I would like to manually confirm the deployment to production. Is there way to achieve this?
You can make use of Conditional Deployments. This allows you to specify whether you push to production or test.
Combine it with e.g. a check-live-deployment.sh-script and differentiate between branches and/or tagged commits.
For example:
#!/bin/bash
set -e
contains() {
if [[ $TRAVIS_TAG = *"-live"* ]]
then
#"-live" is in $TRAVIS_TAG
echo "true"
else
#"-live" is not in $TRAVIS_TAG
echo "false"
fi
}
echo "============== CHECKING IF DEPLOYMENT CONDITION IS MET =============="
export LIVE=$(contains)
and .travis.yml for a dev/staging/live-deployment to Cloud Foundry:
sudo: false
language: node_js
node_js:
- '8.9.4'
branches:
only:
- master
- "/v*/"
script:
- printenv
before_install:
- chmod +x -R ci
install:
- source ci/check_live_deployment.sh
- ./ci/check_live_deployment.sh
deploy:
- provider: script
skip_cleanup: true
script: env CF_SPACE=$CF_SPACE_DEV CF_MANIFEST=manifest-dev.yml ci/deploy_to_cf.sh
on:
tags: false
- provider: script
skip_cleanup: true
script: env CF_SPACE=$CF_SPACE_STAGING CF_MANIFEST=manifest-staging.yml ci/deploy_to_cf.sh
on:
tags: true
- provider: script
skip_cleanup: true
script: env CF_SPACE=$CF_SPACE_LIVE CF_MANIFEST=manifest-live.yml ci/deploy_to_cf.sh
on:
tags: true
condition: $LIVE = true
This example pushes to dev if branch is master && no tag is present, staging if its a tagged commit, and staging+live if it is a tagged commit on master (a release) and the deployment-condition is met.
Granted:
Maybe not the prettiest solution, but it definitely works. And this is not Travis waiting for you to manually confirm live-deployment (which would kind of ridicule the whole automated deployment principle imo), but it is a way to guarantee, that you have to manually trigger the pipeline in a specific way.