Hi all. I’ve managed to establish a pipeline to automate my deployment and upon failed or success, circle ci will send a notification to my slack channel with its status. The issue im facing right now, is trying to grab the user / username from the gitlab repo, who’ve triggered the pipeline. So far I’m assuming it’s possible since it’s visible via the CircleCI dashboard, but is there a way to add that as part of my slack integration ? Here’s my current yml file, obfuscated.
orbs:
slack: circleci/slack@5.1.1
jobs:
deploy:
circleci_ip_ranges: true
docker:
- image: cimg/node:20.11.0
steps:
# check versionings
- run: node -v
- run: yarn -v
- run: ssh -V
- checkout
- run:
name: Install Dependencies
command: |
echo "Installing dependencies ..."
yarn install --frozen-lockfile
- run:
name: Building React app for staging
command: |
yarn build:staging
- run:
name: Package build directory
command: |
tar -czf build.tar.gz build
- add_ssh_keys:
fingerprints:
- "SHA256:IxxxxxxxxxxxxxxxY"
- run:
name: Add bastion to known host
command: |
ssh-keyscan $BASTION_STAGING_SERVER >> ~/.ssh/known_hosts
- run:
name: SCP build to staging server
command: |
scp -o ProxyJump=ubuntu@$BASTION_STAGING_SERVER -o StrictHostKeyChecking=no build.tar.gz ubuntu@$BPAM_STAGING_SERVER:~/
- run:
name: SSH to BPAM server and deploy
command: |
ssh -o StrictHostKeyChecking=no -J ubuntu@$BASTION_STAGING_SERVER ubuntu@$BPAM_STAGING_SERVER "bash -l -c '
echo Logged into server !;
sudo tar -xzf build.tar.gz --overwrite --directory /var/lib/bpam/ && \
rm build.tar.gz && \
sudo systemctl restart nginx
'"
- run:
name: Exit any dangling process
command: exit 0
- slack/notify:
channel: C01xxxxD
event: pass
custom: |
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Status:*🟢 Job Success !\n*$CIRCLE_PROJECT_REPONAME* on branch *$CIRCLE_BRANCH*"
}
}, {
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Project:*\n$CIRCLE_PROJECT_REPONAME"
}, {
"type": "mrkdwn",
"text": "*Branch:*\n$CIRCLE_BRANCH"
}, {
"type": "mrkdwn",
"text": "*Trigger User:*\n$CIRCLE_USERNAME"
}, {
"type": "mrkdwn",
"text": "*Job:*\n$CIRCLE_JOB"
}
]
}
]
}
- slack/notify:
channel: C01xxxxD
event: fail
custom: |
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Status:*\n🔴 Job Failed !\n*$CIRCLE_PROJECT_REPONAME* on branch *$CIRCLE_BRANCH*"
}
}, {
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Project:*\n$CIRCLE_PROJECT_REPONAME"
}, {
"type": "mrkdwn",
"text": "*Branch:*\n$CIRCLE_BRANCH"
}, {
"type": "mrkdwn",
"text": "*Trigger User:*\n$CIRCLE_USERNAME"
}, {
"type": "mrkdwn",
"text": "*Job:*\n$CIRCLE_JOB"
}
]
}
]
}
workflows:
test:
jobs:
- deploy:
filters:
branches:
only: staging # only run this job on the staging branch
context:
- slack-secrets