How do I create a github workflow step name with a variable value.
I tried this but it does not work.
name: Publish
on:
push:
branches:
- main
env:
REGISTRY: ghcr.io
jobs:
Publish:
runs-on: ubuntu-latest
steps:
- name: Log into Container registry ${{ env.REGISTRY }}
I know you tried it, but reproducing the workflow here with your implementation (as below) actually worked for me.
name: Publish
on:
push:
env:
REGISTRY: ghcr.io
jobs:
Publish:
runs-on: ubuntu-latest
steps:
- name: Log into Container registry ${{ env.REGISTRY }}
run: echo "Ok"
The job step name was generated dynamically according to the workflow env variable set.
Here is the workflow run
Since this does not seem supported, it is better to add an echo which prints the variable, before one processing it:
name: Publish
on:
push:
branches:
- main
env:
REGISTRY: ghcr.io
jobs:
Publish:
runs-on: ubuntu-latest
steps:
- name: Log into Container registry
run: echo "registry is '$REGISTRY'"
After that, for runtime variables, you can add conditions:
on:
push:
branches:
- actions-test-branch
jobs:
Echo-On-Commit:
runs-on: ubuntu-latest
steps:
- name: "Checkout Repository"
uses: actions/checkout#v2
- name: "Set flag from Commit"
env:
COMMIT_VAR: ${{ contains(github.event.head_commit.message, '[commit var]') }}
run: |
if ${COMMIT_VAR} == true; then
echo "flag=true" >> $GITHUB_ENV
echo "flag set to true"
else
echo "flag=false" >> $GITHUB_ENV
echo "flag set to false"
fi
- name: "Use flag if true"
if: env.flag
run: echo "Flag is available and true"
Related
I would like to get the success/failure output of this action aws-actions/configure-aws-credentials#v1 in my next job. As per the action-output the output is aws-account-id.
hence I created below workflow, doesn't matter what I do, I don't seems to catch the output. My question is how to catch the error/success message for this action.
Right now I'm getting empty even if the job success/failed. Is there any way I can get the output of the steps?
jobs:
deploy_infra:
name: Deploy
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
outputs:
output1: ${{ steps.cac.outputs.aws-account-id }}
steps:
- name: Git Checkout
uses: actions/checkout#v3
- name: AWS Role
id: cac
uses: aws-actions/configure-aws-credentials#v1
with:
role-to-assume: arn:aws:iam::${{ github.event.inputs.account_id }}:role/assume-role
aws-region: ${{ github.event.inputs.aws_region }}
mask-aws-account-id: false
alert_job:
if: always()
needs: deploy_infra
name: Testing the status
runs-on: ubuntu-latest
steps:
- run: echo ${{needs.job1.outputs.output1}}
I want to pass maven image version as as env variable but when i am trying to access that env.MAVEN_VERSION variable getting error
Error- The workflow is not valid. .github/workflows/Merge.yaml (Line: 13 image:) Unrecognized named-value: 'env'. Located at position 1 within expression: env.MAVEN_VERSION
Yaml File ---
on:
push:
branches: [ master ]
env:
MAVEN_VERSION: maven:3.8.6-jdk-11
jobs:
build:
runs-on: ubuntu-latest
container:
image: ${{ env.MAVEN_VERSION }}
steps:
- name: Env Variable
run: echo ${{ env.MAVEN_VERSION }}
While env is not available, outputs from previous jobs are.
Consider the following example
on:
push:
branches: [ master ]
env:
MAVEN_VERSION: maven:3.8.6-jdk-11
jobs:
prepare-image:
runs-on: ubuntu-latest
outputs:
image: ${{ env.MAVEN_VERSION }}
build:
runs-on: ubuntu-latest
needs: [prepare-image]
container:
image: ${{ needs.prepare-image.outputs.image }}
steps:
- name: Echo output
run: echo ${{ needs.prepare-image.outputs.image }}
when i am trying to access that...
That's not what the error is telling you about. The error Unrecognized named-value: 'env' is telling you that GitHub is not recognizing the YAML you wrote at line 13. It's a syntax error.
In a GitHub workflow you can use env either in jobs.<job_id>.env or in jobs.<job_id>.steps[*].env. See here for details.
This YAML should work:
on:
push:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
container:
image: ${{ env.MAVEN_VERSION }}
steps:
- name: Env Variable
env:
MAVEN_VERSION: maven:3.8.6-jdk-11
run: echo ${{ env.MAVEN_VERSION }}
Also, note that when you only specify a container image, you can omit the image keyword.
I am trying to trigger a Github action to run after a successful run of a different action.
the 2 workflows are:
Unit Test Action (Which runs first, and should trigger the Follow on Test action below
name: unit-tests
on:
push:
branches:
- '**'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Setup .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: "3.1.x"
- name: Test
run: dotnet test src/XXXXXXXXXX
Follow on Test Action (This is just a test action)
name: Test action triggered by previous action success
on:
workflow_run:
workflows:
- unit-tests
types:
- completed
jobs:
test-job:
name: Test Step
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: ${{ github.event.workflow_run.head_branch }}
- run: echo "The follow on works!!"
The issue is that when this is triggered on a feature branch and not the default branch (as it should be because I want the actions to run all all branches) it doest work?
Any ideas?
As discussed in the comments:
First: It is necessary to have both workflows on the branch and to first merge the branch into your default branch, then onwards it will work.
Second: It is possible to use if: ${{ github.event.workflow_run.conclusion == 'success' }} to only run the jobs if the previous workflow was successful.
Example:
on:
workflow_run:
workflows: ["Other Workflow Name"]
types: [completed] #requested
jobs:
on-success:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- run: echo "First workflow was a success"
on-failure:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
- run: echo "First workflow was a failure"
I trigger my workflow using
on:
push:
tags:
GITHUB_REF won't contain a branch name in this case, how could I get it?
You will need to do a bit of string manipulation to get this going. Basically during a tag creation push, is like if you were to do git checkout v<tag> in your local but there's no reference to the original branch. This is why you will need to use the -r flag in the git branch contains command.
We get the clean branch with the following two commands.
raw=$(git branch -r --contains ${{ github.ref }})
branch=${raw/origin\/}
Here is a pipeline that creates a branch env
name: Tag
on:
create:
tags:
- v*
jobs:
job1:
runs-on: ubuntu-latest
steps:
- name: checkout source code
uses: actions/checkout#v1
- name: Get Branch
run: |
raw=$(git branch -r --contains ${{ github.ref }})
branch=${raw/origin\/}
echo ::set-env name=BRANCH::$branch
- run: echo ${{ env.BRANCH }}
Working Example
NOTE: I triggered the above pipeline by creating a tag and pushing it to origin
Building on Edward's answer, here is a modern script which allows getting branch name and passing it between jobs, including allowing jobs to be conditionally run based on branch
# This only runs for tags, and the job only processes for "master" branch
name: Example
on:
push:
tags:
- "*"
jobs:
check:
runs-on: ubuntu-latest
outputs:
branch: ${{ steps.check_step.outputs.branch }}
steps:
- name: Checkout
uses: actions/checkout#v2
with:
fetch-depth: 0
- name: Get current branch
id: check_step
run: |
raw=$(git branch -r --contains ${{ github.ref }})
branch=${raw##*/}
echo "::set-output name=branch::$branch"
echo "Branch is $branch."
job2:
runs-on: ubuntu-latest
needs: check # Wait for check step to finish
if: ${{ needs.check.outputs.branch == 'master' }} # only run if branch is master
steps:
- run: echo "Running task..."
You can get it from the github.event variable:
on:
push:
tags:
- '*'
jobs:
main:
runs-on: ubuntu-latest
steps:
- name: Echo branch
run: echo "${{ github.event.base_ref }}"
The code results with:
I have a GitHub workflow for releasing nightly snapshots of the repository. It uses the create-release action. This is how the workflow file looks right now:
name: Release Nightly Snapshot
on:
schedule:
- cron: "0 0 * * *"
jobs:
build:
name: Release Nightly Snapshot
runs-on: ubuntu-latest
steps:
- name: Checkout master Branch
uses: actions/checkout#v2
with:
ref: 'master'
- name: Create Release
id: nightly-snapshot
uses: actions/create-release#latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: 'nightly snapshot'
release_name: 'nightly snapshot'
draft: false
prerelease: false
I want tag_name and release_name to use the current date and time, instead of hard-coded values. However, I couldn't find any documentation on it. How should I do it?
From this post you can create a step that set its output with the value $(date +'%Y-%m-%d')
Then use this output using ${{ steps.date.outputs.date }}. The following show an example for environment variables and for inputs :
on: [push, pull_request]
name: build
jobs:
build:
name: Example
runs-on: ubuntu-latest
steps:
- name: Get current date
id: date
run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
- name: Test with environment variables
run: echo $TAG_NAME - $RELEASE_NAME
env:
TAG_NAME: nightly-tag-${{ steps.date.outputs.date }}
RELEASE_NAME: nightly-release-${{ steps.date.outputs.date }}
- name: Test with input
uses: actions/hello-world-docker-action#master
with:
who-to-greet: Mona-the-Octocat-${{ steps.date.outputs.date }}
Outputs :
* Test with environment variables
nightly-tag-2020-03-31 - nightly-release-2020-03-31
* Test with input
Hello Mona-the-Octocat-2020-03-31
Here's another way to do this via environment variables (from this post):
name: deploy
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Set current date as env variable
run: echo "NOW=$(date +'%Y-%m-%dT%H:%M:%S')" >> $GITHUB_ENV
- name: Echo current date
run: echo $NOW # Gives "2022-12-11T01:42:20"
This sets the date as an environment variable, which is useful if you want to consume it in scripts / programs in subsequent steps.
A clean solution is to use ${{ github.event.repository.updated_at}} which is pretty close to current datetime$(date +%Y%m%d%H%M)
Format is ISO 8601
e.g 2022-05-15T23:33:38Z
Pros:
Doesn't require shell execution
Directly accessible from all your workflow
Allows cross-referencing with GitHub events
Cons:
Strict format
you can still modify the format in a shell
e.g. echo ${{ github.event.repository.updated_at}} | sed 's/:/./g'
Not available with Act testing framework
References:
Github context
Event object
Update
The set-output command is deprecated and will be disabled soon. Please upgrade to using Environment Files.
Documentation can be found here
name: deploy
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Get current date
id: date
run: |
echo "{date}={$(date +'%Y-%m-%d')}" >> $GITHUB_STATE
- name: Test with environment variables
run: echo $TAG_NAME - $RELEASE_NAME
env:
TAG_NAME: nightly-tag-${{ env.date }}
RELEASE_NAME: nightly-release-${{ env.date }}
if $GITHUB_ENV doesn't work, use $GITHUB_OUTPUT instead:
name: Flutter CI
run-name: ${{ github.actor }} is testing GitHub Actions 🚀
on:
push:
tags:
- '*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: "Set current date as env variable"
run: |
echo "builddate=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
id: version # this is used on variable path
- name: Publishing Release
uses: softprops/action-gh-release#v1
with:
tag_name: ${{ steps.version.outputs.builddate }}
name: ${{ steps.version.outputs.builddate }}
body: "your date"
- name: print date
run: |
echo $DEBUG
env:
DEBUG: ${{ steps.version.outputs.builddate }}-DEBUG
RELEASE: ${{ steps.version.outputs.builddate }}-RELEASE