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.
Related
I have a project where I have two GitHub actions yml file where the first file is called build.yml and it contains instructions to compile, build and test the project. It is as simple as this:
name: build my-project
on:
push:
paths-ignore:
- 'images/**'
- README.md
branches:
- master
pull_request:
branches:
- master
release:
types: [ created ]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: cache ivy2
uses: actions/cache#v1
with:
path: ~/.ivy2/cache
key: ${{ runner.os }}-sbt-ivy-cache-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }}
- name: sbt Test
run: sbt clean test
I now have another yml file that contains the instructions to do a release based on annotated tags. It is like this:
name: release my-project
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- 'v[0-9]+.[0-9]+.[0-9]+-[a-zA-Z]*'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
build:
uses: ./.github/workflows/build.yml
publish:
runs-on: ubuntu-latest
needs: test # See build.yml file where the test job is defined
# If there is a tag and if that tag comes from master branch
if: startsWith(github.ref, 'refs/tags/v')
steps:
- name: checkout
uses: actions/checkout#v3
- name: capture changelog
id: changelog
uses: metcalfc/changelog-generator#v4.0.1
with:
myToken: ${{ secrets.GITHUB_TOKEN }}
- name: sbt ci-publish-github
run: sbt publish
- name: ci-release-github
id: create-release
uses: actions/create-release#latest
with:
allowUpdates: true
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
body: |
## What's Changed
${{ steps.changelog.outputs.changelog }}
draft: false
prerelease: false
I just created an annotated tag which then resulted in an error like this:
Invalid workflow file: .github/workflows/publish.yml#L14
error parsing called workflow "./.github/workflows/build.yml": workflow is not reusable as it is missing a `on.workflow_call` trigger
So basically what I want is, when I push an annotated tag, I want to first run the test job from build.yml and then once that succeeds, I would like to run the publish job. Any suggestions on how to get this straight?
So basically what I want is, when I push an annotated tag, I want to first run the test job from build.yml and then once that succeeds, I would like to run the publish job. Any suggestions on how to get this straight?
You almost got it right with your implementation. You just need a few modifications:
The build job needs to depends on the publish job:
name: release my-project
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- 'v[0-9]+.[0-9]+.[0-9]+-[a-zA-Z]*'
jobs:
publish:
[ ... ]
build:
needs:
- publish
uses: ./.github/workflows/build.yml
The build needs the workflow_call trigger (as stated by the error message - Reference):
on:
workflow_call:
push:
[ ... ]
Note: You could even share the tag value from the previous workflow, sending it as input to the second one by using:
on:
workflow_call:
inputs:
tag:
required: true
type: string
Calling the reusable workflow that way from the main workflow:
build:
needs:
- publish
uses: ./.github/workflows/build.yml
with:
tag: 'MY TAG'
I was able to fix it by adding the following in my publish.yml:
jobs:
tests:
uses: ./.github/workflows/build.yml
publish:
runs-on: ubuntu-latest
needs: [tests] # See build.yml file where the test job is defined
In my build.yml, I had to add the following:
on:
push:
paths-ignore:
- 'images/**'
- README.md
branches:
- master
pull_request:
branches:
- master
release:
types: [ created ]
workflow_call:
Notice that workflow_call: entry that needs to be added explicitly.
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"
I would like to use custom environment variables related to specific environment based on environment selection from dropdown during deployment. How to get the custom variables related to specific environment from environment variable section. Any suggestions on how this can be achieved.
name: Adios CD pipeline.
on:
workflow_dispatch:
inputs:
ENVIRONMENT:
type: choice
description: 'Select the Environment to deploy'
required: true
options:
- dev
- qa
- uat
- load
- prod
default: 'dev'
env:
Dev:
AWS_REGION: "us-east-1"
STAGE: "${{ github.event.inputs.ENVIRONMENT }}"
SYSTEM: "ADIOSAPP"
QA:
AWS_REGION: "us-east-1"
STAGE: "${{ github.event.inputs.ENVIRONMENT }}"
SYSTEM: "ADIOSAPP"
jobs:
build:
name: "Deploying ${{ github.ref_name }} branch to ${{ github.event.inputs.ENVIRONMENT }} environment"
runs-on: ubuntu-latest
environment: ${{ github.event.inputs.ENVIRONMENT }}
steps:
- name: Checkout
uses: actions/checkout#v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials#v1
with:
aws-region: ${{ env.AWS_REGION }}
As you already know mapping is not allowed at this place, byt you could use fromJson to mimic this:
on:
workflow_dispatch:
inputs:
ENVIRONMENT:
type: choice
description: 'Select the Environment to deploy'
required: true
options:
- dev
- qa
- uat
- load
- prod
default: 'dev'
env:
AWS_REGION: ${{ fromJSON('{"dev":"us-east-1","qa":"us-east-1"}')[github.event.inputs.ENVIRONMENT] }}
STAGE: "${{ github.event.inputs.ENVIRONMENT }}"
SYSTEM: ${{ fromJSON('{"dev":"ADIOSAPP","qa":"ADIOSAPP"}')[github.event.inputs.ENVIRONMENT] }}
jobs:
build:
name: "Deploying ${{ github.ref_name }} branch to ${{ github.event.inputs.ENVIRONMENT }} environment"
runs-on: ubuntu-latest
#environment: ${{ github.event.inputs.ENVIRONMENT }}
steps:
- name: Checkout
uses: actions/checkout#v3
- name: Print env
run: echo "${{ env.AWS_REGION }}"
I think you can also use secrets in different environments.
I'm trying to create a GitHub Action where a script outputs a list of sites that need to be deployed and then that list is used a the basis for matrix that does the deploying. Here's an example illustrating my point:
jobs:
setup:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.matrix.outputs.value }}
steps:
- id: matrix
run: |
echo '::set-output name=value::["site1", "site2", "site3"]'
build:
needs: [ setup ]
runs-on: ubuntu-latest
strategy:
matrix:
value: ${{fromJSON(needs.setup.outputs.matrix)}}
steps:
- run: |
echo "${{ matrix.value }}"
However, the editor underlines fromJSON and complains Invalid type found: array was expected but string was found.
If I copy/paste the fromJSON example from GitHub's documentation verbatim into my YAML file, I get a similar error and it doesn't work:
jobs:
job1:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- id: set-matrix
run: echo "::set-output name=matrix::{\"include\":[{\"project\":\"foo\",\"config\":\"Debug\"},{\"project\":\"bar\",\"config\":\"Release\"}]}"
job2:
needs: job1
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJSON(needs.job1.outputs.matrix) }}
steps:
- run: build
Any clues as to what I'm missing here?
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