How to upload source version to Azure Setting using curl? - bash

My objective is simple: get hold of the last commit hash when I run my app
attempts:
I started to use git-last-commit package but where the app run, it's a normal directory, and the repository is outside this folder
/config
/deployments
/diagnostics
/ipaddr_0
/locks
/repository
/wwwroot
the website runs inside wwwroot and git repo is in repository. I couldn't get hold of it programmatically.
So I tried the Kudu API and it's as easy as a curl POST ... but how can I pass the commit has as a curl data?
I've tried:
$ git log -n1 --pretty=format:"%H" | curl -X POST -H 'Content-Type: application/json' https://$AZURE_LOGIN:$AZURE_PASS#$AZURE_APPNAME.scm.azurewebsites.net/api/settings -d '{ "SOURCE_VERSION":"&> /dev/stdin" }'
and
$ git log -n1 --pretty=format:"%H" | curl -X POST -H 'Content-Type: application/json' https://$AZURE_LOGIN:$AZURE_PASS#$AZURE_APPNAME.scm.azurewebsites.net/api/settings -d '{ "SOURCE_VERSION":"#d" }'
only to find that it sends literally what I write and not the piped value
The idea was to have this as a Bitbucket pipeline step to be executed for every deployment...
Does any of you have some trick to accomplish this?

Related

Is it possible to tail Ansible AWX logs via curl?

I would like to create a curl output live in a single shell command, to log a output from an Ansible job in realtime filling a log file.
I've tried this command:
curl -f -k -N -H 'Content-Type: application/json' -XPOST \
--user admin:awxsecret \
http://192.168.42.100/api/v2/jobs/1620/
...but it only returns the output generated thus far, not waiting for newly-generated content.
As #charles-duffy said: "AWX does support websockets" I will work with this solution.

Git post-receive hook, send curl commit message to Discord Webhook

I am trying to post an information-message in our discord every time someone pushes to the master.
I have a post-receive bash script looking like this:
#!/bin/bash
while read oldrev newrev ref
do
if [[ $ref =~ .*/master$ ]];
then
tail=$(git log -1 --pretty=format:'%h %cn: %s%b' $newrev)
url='https://discordapp.com/api/webhooks/validapikey'
curl -i \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST \
--data '{"content": "'$tail'"}' $url
fi
done
If I output tail to a file I get the expected string
6baf5 user: last commit message
but the message does not get posted on discord
If I replace $tail with "hello" it gets posted.
3 suggestions:
a) -d "{\"content\": \"${tail}\"}"
b) You can write this in the same language your project is, like Python or NodeJS, which is always better than bash (use the same name and make it executable)
c) To avoid this to be maintained in each dev machine, you can version this logic inside your repo using https://pypi.org/project/hooks4git or any other too that provides git hook management.

POST multiple files with -d in curl

I'm using curl to create several classifications. I have written the json for the many classifications and they are in one folder. I would like to create all the classifications in one go. But using curl I can only create them one at a time. How could I make them in one request?
curl -u admin:admin -H "Content-Type: application/json" -X POST -d #pii.json http://127.0.0.1:21000/api/atlas/v2/types/typedefs
The curl manual for -d says 'Multiple files can also be specified'. How can I do this? All my attempts have failed.
Do I need a bash script instead? If so, could you help me - I'm not a coder and I'm struggling without an example!
Thanks in advance.
You probably don't want to use multiple -d with JSON data since curl concatenates multiple ones with a & in between. As described in the man page for -d/--data:
If any of these options is used more than once on the same command
line, the data pieces specified will be merged together with a
separating &-symbol. Thus, using '-d name=daniel -d skill=lousy' would
generate a post chunk that looks like 'name=daniel&skill=lousy'.
You can however easily and conveniently pass several files on stdin to let curl use them all in one go:
cat a.json b.json c.json | curl -d#- -u admin:admin -H "Content-Type: application/json" http://127.0.0.1:21000/api/atlas/v2/types/typedefs
(please note that -X POST has no place on a command line that uses -d)
I found the following to work in the end:
<fileToUpload.dat xargs -I % curl -X POST -T "{%}" -u admin:admin -H "Content-Type: application/json" http://127.0.0.1:21000/api/atlas/v2/types/typedefs
Where fileToUpload.dat contained a list of the .json files.
This seemed to work over Daniel's answer, probably due to the contents of the files. Hopefully this is useful to others if Daniel's solution doesn't work for them.
I needed to upload all the *.json files from a folder via curl and I made this little script.
nfiles=*.json
echo "Enter user:"
read user
echo "Enter password:"
read -s password
for file in $nfiles
do
echo -e "\n----$file----"
curl --user $user:$password -i -X POST "https://foo.bar/foo/bar" -H "Content-Type: application/json" -d "#$file"
done
Maybe fits your needs.

Triggering builds of dependent projects in Travis CI

We have our single page javascript app in one repository and our backend server in another. Is there any way for a passing build on the backend server to trigger a build of the single page app?
We don't want to combine them into a single repository, but we do want to make sure that changes to one don't break the other.
Yes, it is possible to trigger another Travis job after a first one succeeds. You can use the trigger-travis.sh script.
The script's documentation tells how to use it -- set an environment variable and add a few lines to your .travis.yml file.
It's possible yes and it's also possible to wait related build result.
I discover trigger-travis.sh from the previous answer but before that I was implementing my own solution (for full working source code: cf. pending pull request PR196 and live result)
References
Based on travis API v3 documentation:
trigger a build triggering-builds
get build information resource/builds
You will need a travis token, and setup this token as secreet environment variable on travis portal.
Following this doc, I were able to trigger a build, and wait for him.
1) make .travis_hook_qa.sh
(extract) - to trigger a new build :
REQUEST_RESULT=$(curl -s -X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Travis-API-Version: 3" \
-H "Authorization: token ${QA_TOKEN}" \
-d "$body" \
https://api.travis-ci.org/repo/${QA_SLUG}/requests)
(it's trigger-travis.sh equivalent) You could make some customization on the build definition (with $body)
2) make .travis_wait_build.sh
(extract) - to wait a just created build, get build info :
BUILD_INFO=$(curl -s -X GET \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Travis-API-Version: 3" \
-H "Authorization: token ${QA_TOKEN}" \
https://api.travis-ci.org/repo/${QA_SLUG}/builds?include=build.state\&include=build.id\&include=build.started_at\&branch.name=master\&sort_by=started_atdesc\&limit=1 )
BUILD_STATE=$(echo "${BUILD_INFO}" | grep -Po '"state":.*?[^\\]",'|head -n1| awk -F "\"" '{print $4}')
BUILD_ID=$(echo "${BUILD_INFO}" | grep '"id": '|head -n1| awk -F'[ ,]' '{print $8}')
You will have to wait until your timeout or expected final state..
Reminder: possible travis build states are created|started (and then) passed|failed

create bitbucket repository and service in one curl request

Is it possible to create the repository and post service in one request. Here's what I currently have. I'm worried that the 2nd call might happen before the 1st is finished and fail.
curl -u$BB_USER:$BB_PASS -X POST https://api.bitbucket.org/1.0/repositories/ -d "name=$PROJECT_NAME" -d "owner=$BB_Owner" -d 'is_private=1' -d 'scm=git'
curl -u$BB_USER:$BB_PASS -X POST https://api.bitbucket.org/1.0/repositories/$BB_Owner/$PROJECT_FOLDER/services -d type=POST -d URL=$POST_HOOK_URL
Chain them with '&&' so that the second command executes only if the first command completed successfully.
curl -u$BB_USER:$BB_PASS -X POST https://api.bitbucket.org/1.0/repositories/ -d "name=$PROJECT_NAME" -d "owner=$BB_Owner" -d 'is_private=1' -d 'scm=git' && curl -u$BB_USER:$BB_PASS -X POST https://api.bitbucket.org/1.0/repositories/$BB_Owner/$PROJECT_FOLDER/services -d type=POST -d URL=$POST_HOOK_URL

Resources