Skip to content

Instantly share code, notes, and snippets.

@mambelli
Last active February 24, 2022 18:07
Show Gist options
  • Save mambelli/b08abd9533450ab0909a1eb326d270ed to your computer and use it in GitHub Desktop.
Save mambelli/b08abd9533450ab0909a1eb326d270ed to your computer and use it in GitHub Desktop.
GitHub workflow example using env variables and a repository dispatch trigger for the workflow
# This workflow can be triggered w/ a dispatch event, e.g.:
# curl -X POST -H "Authorization: token $(cat /tmp/token_file)" \
# -H "Content-Type: application/json" -H "Accept: application/vnd.github.v3+json" \
# https://api.github.com/repos/{owner}/{repo}/dispatches \
# -d '{"event_type":"test-trigger", "client_payload": {"label": "latest", "date_tag":true}}'
# A valid GitHub token must be saved in the file (so that is presented in the authorization)
# otherwise a misleading "Not Found" message is returned. No reply is provided for successful posts.
# The client_payload label and date_tag are optional
# label is used as Docker Hub label instead of "latest"
# if date_tag id true a date tag is added to the Docker Hub label (+%Y%m%d-%H%M)
# -X POST is also optional
name: Variable test
on:
push:
branches: [ mybranch ]
repository_dispatch:
types:
- test-trigger
env:
MY_LABEL: latest
jobs:
testvar:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: change1
run: |
echo "MY_LABEL=changed" >> $GITHUB_ENV
- name: change2
continue-on-error: true
if: ${{ github.event.client_payload.label }}
run: |
echo "MY_LABEL=${{ github.event.client_payload.label }}" >> $GITHUB_ENV
- name: make date tag
id: mkdatetag
#if: success() || failure() # run regardless of prior step success/failure
run: echo "::set-output name=dtag::$(date +%Y%m%d-%H%M)"
- name: add date tag
continue-on-error: true
if: ${{ github.event.client_payload.date_tag }}
run: |
echo "MY_LABEL=$MY_LABEL-${{ steps.mkdatetag.outputs.dtag }}" >> $GITHUB_ENV
- name: echos
run: |
echo "<MY_LABEL is $MY_LABEL, label is ${{ github.event.client_payload.label }}, dtag is ${{ steps.mkdatetag.outputs.dtag }}>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment