Trigger TeamCity build on a specific BitBucket repo branch - teamcity-9.0

I'm trying to trigger a TeamCity build using the REST API on a specific branch in a repository. However, it is always fetching the code from the master branch (which is the default one). Not sure where I am doing it wrong. Is there anything else I need to set up on the TeamCity to turn on this feature?
My curl is:
curl -v -u uname:pwd http://remoteserver.com:8111/httpAuth/app/rest/buildQueue --request POST --header "Content-Type:application/xml" --data-binary #build.xml
And my build.xml is:
<build branchName="testBranch">
<buildType id="TestTc_TestTc"/>
</build>
I'm using TeamCity 9. Could anyone help me on where I am doing it wrong?

I have added "+:refs/heads/(*)" under Branch Specification in the VCS Root settings and it worked fine.

Related

Sorting artifacts using aql and cleanup old artifacts

I'm trying to sort the list of artifacts from jfrog artifactory but getting (The requested URL returned error: 400 Bad Request), in the jfrog documentation (https://www.jfrog.com/confluence/display/JFROG/Artifactory+Comparison+Matrix) says it won't work for open source services. After we get list of artifacts need to delete old artifacts from subfolder in the artifactory repo. Tried with CLI and AQL but nothing worked.
Our repo url looks like this
http://domainname/artifactory/repo/folder/subfolder/test1.zip
Like test 1.zip we have many artifacts(let's say 50)in that subfolder. Looking for help on this, anyone pls me on this issue. Thanks.
While sorting is not supported in OSS versions, if you would like to delete artifacts older than a certain time period, you can use Relative Time Operators, parse the output, and use a script to delete those artifacts.
You can also specify a specific date. There are several Comparison Operators that you can use.
You can use the below AQL for reference:
curl -uadmin:password -XPOST "http://localhost:8082/artifactory/api/search/aql" -d 'items.find({"repo": "repo"}, {"path": "folder/subfolder"}, {"created" : {"$before" : "2minutes"}})' -H "Content-Type: text/plain"

AWS Code Build with Github - get changed files

Tangentially related to: AWS CodeBuild with GitHub - only for specific directory
I have a codebuild project and a github repo with many files in it. A user may update any of these files in the git repo. I want to pass the name of the altered file(s) into my buildspec.yaml somehow; IE my merge job logic, specified in the buildspec.yaml, needs to know what files changed to do a per-file operation.
I am not talking about filters; Ie "only trigger this if X,Y,Z changed". Becuase the filter is there for a large number of XYZ, but I need to know which file(s) changed in my buildspec. IE something like $CHANGED_FILE_LIST.
I don't see this here: https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
Maybe we have to do something like this: how to find out list of all changed files in git for full jenkins build and not for a particular commit?
git diff --name-only $GIT_PREVIOUS_COMMIT $GIT_COMMIT
but one would think this meta info could be provided by codebuild
I don't know if there's a blessed CodeBuild way to do this, but assuming you have access to a GitHub token in the build, you can query the GitHub metadata endpoint to get the info you need. Something like
curl -H "Authorization: token ${yourtoken}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/OWNER/REPO/commits/${CODEBUILD_SOURCE_VERSION} | jq '.files[].filename'
will return a list of files in the commit.
See https://docs.github.com/en/rest/commits/commits#get-a-commit for more details.
git diff --name-status $CODEBUILD_WEBHOOK_BASE_REF should do the trick
You can use git diff --name-only $$CODEBUILD_RESOLVED_SOURCE_VERSION $$CODEBUILD_WEBHOOK_PREV_COMMIT
Where $CODEBUILD_WEBHOOK_PREV_COMMIT is the commit id of the previous commit. And $CODEBUILD_RESOLVED_SOURCE_VERSION is the commit id of the actual one.
Inside a build phase you can check the change with:
-
|
if [ "$(git diff --name-only $CODEBUILD_WEBHOOK_PREV_COMMIT $CODEBUILD_RESOLVED_SOURCE_VERSION | grep -e <filde_path>)" != "" ]; then
#your code;
fi

Completely delete release with GitHub API

I'm trying to use the github web API to delete and existing release if present like:
curl -u user:pw --request DELETE "https://api.github.com/repos/user/repo/releases/RELEASE_ID"
this deletes the release message, but leaves a tag with the assets which seems to require manual removal. Is there some way to completely delete a release with the github API?
It seems the key if to both delete the release id and tag, that is
curl -u user:pw --request DELETE "$URL/releases/$RELEASE_ID_TO_DELETE"
curl -u user:pw --request DELETE "$URL/git/refs/tags/$TAG_TO_DELETE"
You would need to couple that with:
First: listing the assets
deleting each asset
And also: delete the ref represented by the tag associated to this release.
With GitHub CLI gh 2.18.0, you can do a
gh release delete <tag> [--cleanup-tag]
That will cleanup the associated tag.

How to tag a git repo in a bamboo build

I'm trying to tag the git repo of a ruby gem in a Bamboo build. I thought doing something like this in ruby would do the job
`git tag v#{current_version}`
`git push --tags`
But the problem is that the repo does not have the origin. somehow Bamboo is getting rid of the origin
Any clue?
Yes, if you navigate to the job workspace, you will find that Bamboo does not do a straightforward git clone "under the hood", and the the remote is set to an internal file path.
Fortunately, Bamboo does store the original repository URL as ${bamboo.repository.git.repositoryUrl}, so all you need to do is set a remote pointing back at the original and push to there. This is what I've been using with both basic Git repositories and Stash, creating a tag based on the build number.
git tag -f -a ${bamboo.buildNumber} -m "${bamboo.planName} build number ${bamboo.buildNumber} passed automated acceptance testing." ${bamboo.planRepository.revision}
git remote add central ${bamboo.planRepository.repositoryUrl}
git push central ${bamboo.buildNumber}
git ls-remote --exit-code --tags central ${bamboo.buildNumber}
The final line is simply to cause the task to fail if the newly created tag cannot be read back.
EDIT: Do not be tempted to use the variable ${bamboo.repository.git.repositoryUrl}, as this will not necessarily point to the repo checked out in your job.
Also bear in mind that if you're checking out from multiple sources, ${bamboo.planRepository.repositoryUrl} points to the first repo in your "Source Code Checkout" task. The more specific URLs are referenced via:
${bamboo.planRepository.1.repositoryUrl}
${bamboo.planRepository.2.repositoryUrl}
...
and so on.
I know this is an old thread, however, I thought of adding this info.
From Bamboo version 6.7 onwards, it has the Git repository tagging feature Repository Tag.
You can add a repository tagging task to the job and the Bamboo variable as tag name.
You must have Bamboo-Bitbucket integrated via the application link.
It seems that after a checkout by the bamboo agent, the remote repository url for origin is set as file://nothing
[remote "origin"]
url = file://nothing
fetch = +refs/heads/*:refs/remotes/origin/*
That's why we can either update the url using git remote set-url or in my case I just created a new alias so it does not break the existing behavior. There must be a good reason why it is set this way.
[remote "build-origin"]
url = <remote url>
fetch = +refs/heads/*:refs/remotes/build-origin/*
I also noticed that using ${bamboo.planRepository.<position>.repositoryUrl} did not work for me since it was defined in my plan as https. Switching to ssh worked.

Can I cancel a TeamCity build from my msbuild script?

TeamCity allows me to report back from my MsBuild script using the ##teamcity interaction. I can use this to tell TeamCity that the build has FAILED, or indeed SUCCEEDED, however I would like to tell it to CANCEL the build instead. Does anyone know of a way to do this?
I can use this to inform TeamCity of failure...
<Message Text="##teamcity[buildStatus status='FAILURE']" Condition="Something==SomeCondition" />
I would love to do this...
<Message Text="##teamcity[buildStatus status='CANCEL']" Condition="Something==SomeCondition" />
I've tried out the TeamCity Service Tasks but nothing thus far.
EDIT:
So it seems this feature is not available, although a workaround http request can be used to cancel a build. There is also a feature request for Cancelling a build the TC website.
According to JetBrains issue tracker and release page, since TeamCity 2019.1 EAP 1 builds can be stopped with service message as in:
##teamcity[buildStop comment='canceling comment' readdToQueue='true']
You can use the undocumented http request which has changed since it was originally posted. You now need "operationKind=1". I used a powershell runner like so:
$buildId = %teamcity.build.id%
$uri = "http://teamcity/ajax.html?guest=1&comment=Cancelling+build+for+some+reason&submit=Stop&buildId=$buildId&kill&operationKind=1"
$response = Invoke-WebRequest -UseBasicParsing -Uri $uri
Another SO post can tell you how to make an http request from MSBuild
The "guest=1" means I'm using the guest account, which at minimum needs the "Stop build / remove from queue" for the project you're going to cancel.
Since Teamcity 8.1 (Source) it is possible to Cancel the Build via REST API.
Taken from the 9.x Documentation, cancelling a currently running build
curl -v -u user:password --request POST "http://teamcity:8111/app/rest/builds/<buildLocator>" --data "<buildCancelRequest comment='' readdIntoQueue='false' />" --header "Content-Type: application/xml"
Can you not just use the Error task, this should cause the execution of the build to stop.