Created
November 23, 2022 19:38
-
-
Save AlexAtkinson/73b8fde4c010e5983cfc22e0928dfc5c to your computer and use it in GitHub Desktop.
Github Workflow WITH authorization check.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: "Full Job Example" | |
on: | |
workflow_dispatch: | |
inputs: | |
version: | |
description: Version (See Releases) | |
required: true | |
jobs: | |
setup: | |
name: SETUP | |
runs-on: ubuntu-latest | |
outputs: | |
REPOSITORY: ${{ steps.setup.outputs.REPOSITORY }} | |
FUNCTION: ${{ steps.setup.outputs.FUNCTION }} | |
ZIPFILE: ${{ steps.setup.outputs.ZIPFILE }} | |
steps: | |
- name: Setup | |
id: setup | |
run: | | |
REPOSITORY=${PWD##*/} | |
echo "::set-output name=REPOSITORY::$(echo $REPOSITORY)" | |
FUNCTION=$(cut -d- -f2 <<< ${PWD##*/}) | |
echo "::set-output name=FUNCTION::$(echo $FUNCTION)" | |
ZIPFILE="${FUNCTION}.${{ github.event.inputs.version }}.zip" | |
echo "::set-output name=ZIPFILE::$(echo $ZIPFILE)" | |
authorization: | |
name: AUTHORIZATION | |
needs: setup | |
runs-on: ubuntu-latest | |
steps: | |
- name: Authorization | |
id: authorization | |
if: | | |
github.actor == 'AlexAtkinson' || | |
github.actor == 'user2' || | |
github.actor == 'user3' | |
run: | | |
echo "::set-output name=AUTHORIZED::true" | |
- name: UNAUTHORIZED | |
if: ${{ steps.authorization.outputs.AUTHORIZED != 'true' }} | |
run: | | |
echo "${{ github.actor }}, you are not authorized to run this job!" | |
exit 1 | |
work: | |
name: WORK | |
needs: [authorization,setup] | |
runs-on: ubuntu-latest | |
steps: | |
- name: Work | |
run: | | |
echo FUNCTION: ${{ needs.setup.outputs.FUNCTION }} | |
echo VERSION: ${{ github.event.inputs.version }} | |
work-notice: | |
name: JOB NOTICE | |
needs: [work,setup] | |
runs-on: ubuntu-latest | |
steps: | |
- name: "Slack Notification" | |
uses: "rtCamp/action-slack-notify@v2" | |
env: | |
SLACK_CHANNEL: ${{ secrets.SLACK_CHANNEL }} | |
SLACK_COLOR: ${{ needs.work.result }} # or a specific color like 'good' or '#ff00ff' | |
SLACK_ICON_EMOJI: ":hal9000:" | |
SLACK_TITLE: "DEPLOY NOTICE" | |
SLACK_MESSAGE: "Environment: TEST\nTester: ${{ needs.setup.outputs.FUNCTION }} ${{ github.event.inputs.version }}" | |
SLACK_USERNAME: DevOps_Bot | |
SLACK_WEBHOOK: ${{ secrets.DEVOPS_SLACK_WEBHOOK }} | |
auth-notice: | |
name: UNAUTHORIZED NOTICE | |
if: failure() | |
needs: [authorization,setup] | |
runs-on: ubuntu-latest | |
steps: | |
- name: "Slack Notification" | |
uses: "rtCamp/action-slack-notify@v2" | |
env: | |
SLACK_CHANNEL: ${{ secrets.SLACK_CHANNEL }} | |
SLACK_COLOR: failure | |
SLACK_ICON_EMOJI: ":hal9000:" | |
SLACK_TITLE: ":wave: UNAUTHORIZED DEPLOY ATTEMPT" | |
SLACK_MESSAGE: "Environment: TEST\nTester: ${{ needs.setup.outputs.FUNCTION }} ${{ github.event.inputs.version }}" | |
SLACK_USERNAME: DevOps_Bot | |
SLACK_WEBHOOK: ${{ secrets.DEVOPS_SLACK_WEBHOOK }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can a adjust this logic easily enough to use a secret with a list of users, rather than embedding it in a workflow if.