I want to create a dropdown list for my GitHub Action Input parameter. This should help in selecting a value from the dropdown just like how the option is there to select the branches.
When using workflow_dispatch, it's now possible to have choice, boolean and environment inputs instead of only just strings. choice is a dropdown, boolean is a checkbox and environment is like choice but will auto-populate with all environments configured in your repos settings.
Here's an example workflow using the new types:
name: CI
on:
workflow_dispatch:
inputs:
environment:
type: environment
description: Select the environment
boolean:
type: boolean
description: True or False
choice:
type: choice
description: Make a choice
options:
- foo
- bar
- baz
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: greet
run: |
echo "environment is ${{ github.event.inputs.environment }} / ${{ inputs.environment }}"
echo "boolean is ${{ github.event.inputs.boolean }}" / ${{ inputs.boolean }}
echo "choice is ${{ github.event.inputs.choice }}" / ${{ inputs.choice }}
Note that inputs can now be accessed directly using the inputs context, instead of using github.event.inputs.
Related
I'm trying to utilize some of the default github actions env vars within my composite github action step. I recall reading (the link is long lost) somewhere that composite steps must be passed all of their parameters and don't have access to all the env vars, so I'm trying to do that. However, I can't get the env var value to successfully get passed.
Notice here I'm using 3 different forms of syntax:
- name: Do a thing
uses: ./.github/actions/my-action
with:
repositoryUrl: ${{ env.GITHUB_SERVER_URL }}/$GITHUB_REPOSITORY
commitSha: ${GITHUB_SHA}
context: ${{ env.DOCKER_CONTEXT_PATH }}
tags: ${{ needs.generate-tag.outputs.DOCKER_IMAGE }}
dockerfile: ${{ env.DOCKERFILE_PATH }}
push: true
platforms: linux/amd64, linux/arm64
Those env vars get used by my action, and they're all read as a normal string except for GITHUB_SERVER_URL which is empty. I know this because I can see this snippet in the build output:
--label org.opencontainers.image.source="/$GITHUB_REPOSITORY" --label org.opencontainers.image.revision="${GITHUB_SHA}"
Then within .github/actions/my-action/action.yml I have:
name: "Build image"
description: "Build and conditionally push an image to a remote ECR registry"
inputs:
repositoryUrl:
description: "The github repository URL"
required: true
commitSha:
description: "The commit sha associated with this image"
required: true
tags:
description: "If a new image is built, it will be assigned all of these tags"
required: true
context:
description: "Docker context to use when building"
required: true
dockerfile:
description: "Path to Dockerfile"
required: true
platforms:
description: "Platforms to build"
required: true
push:
description: "Whether to push the image after it's built"
required: true
default: 'false'
runs:
using: "composite"
steps:
- name: Set up QEMU
uses: docker/setup-qemu-action#v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action#v2
- name: Build docker image
uses: docker/build-push-action#v3
with:
context: ${{ inputs.context }}
tags: ${{ inputs.tags }}
file: ${{ inputs.dockerfile }}
platforms: ${{ inputs.platforms }}
push: ${{ inputs.push }}
labels: |
org.opencontainers.image.source="${{ inputs.repositoryUrl }}"
org.opencontainers.image.revision="${{ inputs.commitSha }}"
How can I ensure these values are passed dynamically without having to hard code them as strings?
Looks like in order to utilize github's native env vars within a composite step's with: section, you need to use ${{ github.server_url }} instead of any form of GITHUB_SERVER_URL env var. This means my action looks like:
runs:
using: "composite"
steps:
- name: Generate labels
id: labels
shell: bash
run: echo "CREATED=$(date +'%Y-%m-%dT%H:%M:%S')" >> $GITHUB_OUTPUT
- name: Set up QEMU
uses: docker/setup-qemu-action#v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action#v2
- name: Build docker image
uses: docker/build-push-action#v3
with:
context: ${{ inputs.context }}
tags: ${{ inputs.tags }}
file: ${{ inputs.dockerfile }}
platforms: ${{ inputs.platforms }}
push: ${{ inputs.push }}
labels: |
org.opencontainers.image.source="${{ github.server_url }}/${{ github.repository }}"
org.opencontainers.image.revision="${{ github.sha }}"
org.opencontainers.image.created="${{ steps.labels.outputs.CREATED }}"
ref: https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
I've really struggled here doing this for the first time and having no background in development.
We have an action that checks the status of several services running on different envs (DEV, TEST, PROD) and sends notifications to Microsoft Teams Channel.
At the moment there is a dedicated action for each env and the goal is to combine them in one.
the action itself:
name: Services Health Check
on:
workflow_dispatch:
schedule:
- cron: '*/30 * * * *'
env:
DEV: https://app.dev.contoso.com
TEST: https://app.test.contoso.com
PROD: https://app.contoso.com
TEAMS_TOKEN_DEV: ${{ secrets.HEALTH_CHECK_TEAMS_WEB_HOOK_URL_DEV }}
TEAMS_TOKEN_TEST: ${{ secrets.HEALTH_CHECK_TEAMS_WEB_HOOK_URL_TEST }}
TEAMS_TOKEN_PROD: ${{ secrets.HEALTH_CHECK_TEAMS_WEB_HOOK_URL_PROD }}
jobs:
#here I want to create a matrix as a JSON array to look like this, but Im not sure if I do it right (I am also not sure if I correctly escape the characters and which one should I escape):
#[
# { dev : https://app.dev.contoso.com, webhook : ${{ secrets.WEB_HOOK_URL_DEV }} },
# {test : https://app.test.contoso.com, webhook : ${{ secrets.WEB_HOOK_URL_TEST }} },
# {prod : https://app.contoso.com, webhook : ${{ secrets.WEB_HOOK_URL_TEST }} }
#]
env-matrix:
name: Setup ENV Matrix
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.matrix.outputs.env }}
steps:
- id: matrix-env
run: |
echo '::set-output name=env::[\{\"env\"\:\"$DEV\", \"webhook\"\:\"$TEAMS_TOKEN_DEV\"\}, \{\"env\"\:\"$DEMO\", \"webhook\"\:\"$TEAMS_TOKEN_DEMO\"\}, \{\"env\"\:\"$TEST\", \"webhook\"\:\"$TEAMS_TOKEN_TEST\"\}, \{\"env\"\:\"$POC\", \"webhook\"\:\"$TEAMS_TOKEN_POC\"\}, \{\"env\"\:\"$PRE\", \"webhook\"\:\"$TEAMS_TOKEN_PRE\"\}, \{\"env\"\:\"$PROD\", \"webhook\"\:\"$TEAMS_TOKEN_PROD\"\}]'
#and the healthcheck job itself
healthcheck:
needs: env-matrix
name: Health Check
runs-on: ubuntu-18.04
strategy:
matrix:
value: ${{ fromJson(needs.env-matrix.outputs.matrix-env)}}
steps:
- name: service1
uses: repo/action
continue-on-error: true
with:
url: '${{ matrix.value.env }}/service1/q/health/ready'
teamsWebHookURL: '${{ matrix.value.webhook }}'
- name: service2
uses: repo/action
continue-on-error: true
with:
url: '${{ matrix.value.env }}/service2/q/health/ready'
teamsWebHookURL: '${{ matrix.value.webhook }}'
so the job must run on DEV with TEAMS_TOKEN_DEV, on TEST with TEAMS_TOKEN_TEST, but I don't know the way to access an array item, so the steps are incorrect.
Any help will be appreciated. If you know a simpler solution pls share.
Thanks for your time and help
Another way of rewriting your workflow is to define the name of the secrets in the matrix and then using Array notation to fetch the actual value of the secrets. Below is a way of doing this and it is not a clone of your workflow. But this should give you an idea.
name: Services Health Check
on:
workflow_dispatch:
jobs:
healthcheck:
name: Health Check
runs-on: ubuntu-18.04
strategy:
matrix:
environment: [dev, test, prod]
include:
- environment: dev
url: https://app.dev.contoso.com
webhook: HEALTH_CHECK_TEAMS_WEB_HOOK_URL_DEV
- environment: test
url: https://app.test.contoso.com
webhook: HEALTH_CHECK_TEAMS_WEB_HOOK_URL_TEST
- environment: prod
url: https://app.prod.contoso.com
webhook: HEALTH_CHECK_TEAMS_WEB_HOOK_URL_PROD
steps:
- name: test_1
run: |
echo ${{ format('{0}{1}', matrix.url, '/service1/q/health/ready') }}
echo ${{secrets[matrix.webhook]}}
- name: test_2
run: |
echo ${{ format('{0}{1}', matrix.url, '/service2/q/health/ready') }}
echo ${{secrets[matrix.webhook]}}
I am trying to automate some tests but I would need to pass some particular parameters to the final test scripts that would fit perfectly as a json file. The issue now is to make github action able to handle json data as a parameter.
The constraint is that the json file as to be local as the workflow has to be triggered from the command gh workflow run ...
So far I tried create my first yml file as such :
name: setup
on:
workflow_dispatch :
inputs:
config_file:
description: 'json file containing the configuration for the runners'
required: true
type: string
...
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
setup-auth:
name: setup-authentication
uses: ./.github/workflows/single-device-authentication.yml
with:
devices: mlops
config_file: ${{ inputs.config_file }}
secrets: inherit
single-device-authentication.yml looks like this, I commented where it fails :
name: single-device-authentication
on:
workflow_call:
inputs:
devices:
required: true
type: string
config_file:
description: 'json file containing the configuration for jetson runners'
required: true
type: string
jobs:
device-authentication:
name: device-authentication
runs-on: ${{ inputs.devices }}
steps:
- uses: PATH/TO/gh_auth#main
with:
app_id: 7
private_key: ${{ secrets.MLOPS_BOT_PRIVATE_KEY }}
json-parser:
name: parser
runs-on: ${{inputs.devices}}
needs: device-authentication
steps:
- name: parser script
run: |
echo ${{ inputs.config_file }}" # This fails
Also, to trigger the workflow, I tried that way :
gh workflow run setup.yml -f config_file="$(cat ${PATH_TO_CONFIG_FILE})"
I want to use environment variables at the job level. Is there a way to do it?
env:
stageEnv: UAT
jobs:
name: Upload Build
if: ${{ env.stageEnv == 'UAT' }}
steps:
....
I get unrecognized named-value: 'env' error. Tried $stageEnv and ${{ env.stageEnv }}
Note: It works when I access within 'steps', but would like this to be accessible at 'jobs' level.
I'm afraid not, but you can do like this:
env:
stageEnv: UAT
jobs:
build:
name: Build
runs-on: ubuntu-latest
outputs:
stageEnv: ${{ steps.init.outputs.stageEnv }}
steps:
- name: Make environment variables global
id: init
run: |
echo "stageEnv=${{ env.stageEnv }}" >> $GITHUB_OUTPUT
And use it in another job:
upload:
name: Upload build
needs: build
if: ${{ needs.build.outputs.stageEnv == 'UAT' }}
Note this is just an example, and I personally prefer environment variables uppercase and output variables lowercase
I'm trying to build a workflow_dispatch (ie manual) Github action that pre-fills the input fields with the SHA of the branch.
ie
name: Manually tag a release
on:
workflow_dispatch:
inputs:
git-sha:
description: Release SHA
default: <prefill with GITHUB_SHA>
required: true
I've tried default: ${{ github.sha }} but that throws an error.
Is this possible and what is the syntax?
I just solved this problem a couple of days ago, but in a different way.
I set the required to false and then coalesced the input with the GITHUB_SHA
name: Deploy To PROD
on:
workflow_dispatch:
inputs:
sha:
description: 'Git SHA to Deploy'
jobs:
deploy_prod:
runs-on: ubuntu-latest
env:
SHA_TO_DEPLOY: ${{ github.event.inputs.sha || github.sha }}
Then ${{ env.SHA_TO_DEPLOY }} references the SHA that was passed in, or the default if none was passed in.