Skip to content

Instantly share code, notes, and snippets.

@parjun8840
Created April 2, 2023 04:15
Show Gist options
  • Save parjun8840/459b4f7cd6724140629c7fac39554054 to your computer and use it in GitHub Desktop.
Save parjun8840/459b4f7cd6724140629c7fac39554054 to your computer and use it in GitHub Desktop.
Jenkins Pipeline GitHub Actions
#Please create the required secrets
#Script path- jenkins-series/scripts/send_slack.sh
#!/bin/bash
set -e
usage() {
echo "usage: ${0##*/} -u <SLACK_URL> -t <TEXT> -w <GITHUB_WOFKFLOW>"
exit 1
}
send_slack() {
echo "Sending msg to slack"
curl -k -X POST -H 'Content-type: application/json' --data "{ \"text\": \"${text}\" }" "${slack_url}"
}
while getopts ":u:t:w:" opt; do
case $opt in
u) slack_url="$OPTARG"
;;
t) text="$OPTARG"
;;
w) workflow="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2 && usage
;;
esac
done
if [ -z "$slack_url" ] || [ -z "$text" ]
then
usage
fi
[ -z "${workflow}" ] && workflow="TBD"
env
send_slack
inputs='{"ref": "main", "inputs": {"text": "'$text'"}}'
echo "-------Executing GitHub Action-------"
api_url=https://api.github.com/repos/parjun8840/gha01/actions/workflows/$WORKFLOW_ID/dispatches
curl -k -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $PERSONAL_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
$api_url \
-d "$inputs"
#Jenkins pipeline code
pipeline {
environment {
SLACK_URL = credentials("slack-id")
PERSONAL_TOKEN = credentials("personal_token")
WORKFLOW_ID = "53067516"
}
agent {
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
spec:
containers:
- name: git
image: alpine/git:latest
command:
- cat
tty: true
- name: curl
image: parjun8840/utility:latest
command:
- cat
tty: true
'''
}
}
stages {
stage("Checkout Code") {
steps {
container('git') {
git branch: 'main',
url: "https://github.com/parjun8840/jenkins-series.git"
}
}
}
stage("Execute script") {
steps {
container('curl'){
echo "Executing the slack send script"
sh "ls -lrtR *"
sh "chmod a+x ./scripts/send_slack.sh"
sh "./scripts/send_slack.sh -u ${env.SLACK_URL} -t 'Demo from Jenkins series' -w ${env.WORKFLOW_ID}"
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment