I have this main.yml workflow right here:
name: Testing
on:
push:
branches:
- main
jobs:
upgrade-kubectl:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Install kubectl version
uses: ./.github/actions/promote-image
with:
kubectl: 'latest'
and my action.yml metadata file:
name: "Helm, Kubectl or Devspace installation setup"
description: "Install a specific version of Helm, Kubectl or Devspace. Acceptable values are latest or version strings like 1.15.0"
inputs:
kubectl:
description: "Version of Kubectl"
required: false
helm:
description: "Version of Helm"
required: false
devspace:
description: "Version of Devspace"
required: false
runs:
using: "composite"
steps:
- name: Setting up kubectl
uses: azure/setup-kubectl#v1
with:
version: ${{ inputs.kubectl }}
- name: Setting up Helm
uses: azure/setup-helm#v1
with:
version: ${{ inputs.helm }}
- name: Setting up Devspace
uses: loft-sh/setup-devspace#main
with:
version: ${{ inputs.devspace }}
Currently I am just supplying the kubectl version in my workflow, but when the action is triggered it is running all 3 steps instead.
How do I make it so that if I supply one version it only runs the one step, supply two version it runs two steps respectively, etc.
Any help would be appreciated !
It seems that now conditions are supported on composite actions.
Therefore, you could add if conditions at each step level according to the input used.
In that case, your action.yml workflow file would look like this:
name: "Helm, Kubectl or Devspace installation setup"
description: "Install a specific version of Helm, Kubectl or Devspace. Acceptable values are latest or version strings like 1.15.0"
inputs:
kubectl:
description: "Version of Kubectl"
required: false
helm:
description: "Version of Helm"
required: false
devspace:
description: "Version of Devspace"
required: false
runs:
using: "composite"
steps:
- name: Setting up kubectl
if: ${{ inputs.kubectl != '' }}
uses: azure/setup-kubectl#v1
with:
version: ${{ inputs.kubectl }}
- name: Setting up Helm
if: ${{ inputs.helm != '' }}
uses: azure/setup-helm#v1
with:
version: ${{ inputs.helm }}
- name: Setting up Devspace
if: ${{ inputs.devspace != '' }}
uses: loft-sh/setup-devspace#main
with:
version: ${{ inputs.devspace }}
Note that the syntax:
if: ${{ inputs.kubectl != '' }} works
if: ${{ inputs.kubectl }} != '' doesn't
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'm trying to integrate Lighthouse CI into my CI/CD to generate reports on my applications performance. I'm using GitHub Actions, and other jobs like building the app and generating a SonarCloud scan are working.
However Lighthouse CI is not working. The error is: Error: fatal: could not read Username for 'https://github.com': terminal prompts disabled
Code:
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: Node.js CI
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
sonarcloud:
name: SonarCloud
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action#master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
lighthouse:
name: Lighthouse CI
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout#v3
with:
token: ${{ secrets.LHCI_GITHUB_APP_TOKEN }}
submodules: recursive
- name: Use Node.js 16.x
uses: actions/setup-node#v3
with:
node-version: 16.x
- name: Run the Lighthouse CI
run: |
npm install -g #lhci/cli#0.6.x
lhci autorun
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
strategy:
matrix:
node-version: [ 16.x ]
steps:
- uses: actions/checkout#v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
- name: Log in to the Container registry
uses: docker/login-action#f054a8b539a109f9f41c372932f1ae047eff08c9
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action#98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=sha
- name: Build and push Docker image
uses: docker/build-push-action#ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
Screenshot of GitHub Actions:
hi,i find a issue for the problem。
https://github.com/actions/checkout/issues/664
“For a simple checkout indeed no PAT is required.”
so you can try remove token
lighthouse:
name: Lighthouse CI
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout#v3
with:
# token: ${{ secrets.LHCI_GITHUB_APP_TOKEN }} // remove
submodules: recursive
- name: Use Node.js 16.x
uses: actions/setup-node#v3
with:
node-version: 16.x
- name: Run the Lighthouse CI
run: |
npm install -g #lhci/cli#0.6.x
lhci autorun
I have a workflow that uses cache.
The workflow works fine when it triggered on push/manually with 'workflow dispatch'
but when it triggered with 'repository dispatch' meaning it is triggered by another job, I never get a cache hit and all the dependencies are installed from scratch.
This is my workflow:
name: Caching with npm
name: build and trigger release
on:
repository_dispatch:
types: [ release ]
workflow_dispatch:
branches:
- test-branch
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: Configure AWS CLI
uses: aws-actions/configure-aws-credentials#v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Login to Amazon Public ECR
uses: docker/login-action#v1
with:
registry: ****.dkr.ecr.us-east-1.amazonaws.com
username: ${{ secrets.AWS_ACCESS_KEY_ID }}
password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Create ecr repo incase it doesn't exist
uses: int128/create-ecr-repository-action#v1
with:
repository: sharon-test
lifecycle-policy: lifecycle-policy.json
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action#master
- name: Cache Docker layers
uses: actions/cache#v2
with:
path: /tmp/.buildx-sharon-test-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Build Docker image
uses: docker/build-push-action#v2
with:
context: .
builder: ${{ steps.buildx.outputs.name }}
push: true
tags: ****.dkr.ecr.us-east-2.amazonaws.com/sharon-test:latest, ****.dkr.ecr.us-east-2.amazonaws.com/sharon-test:${{ github.event.client_payload.tag }}
cache-from: type=local,src=/tmp/.buildx-sharon-test-cache
cache-to: type=local,dest=/tmp/.buildx-sharon-test-cache-new
file: Dockerfile.api
- name: Move cache
run: |
rm -rf /tmp/.buildx-sharon-test-cache
mv /tmp/.buildx-sharon-test-cache-new /tmp/.buildx-sharon-test-cache
I would also like to mention that In this example you see I am using the local cache
but in the 'Build docker image' part I also used github actions global cache:
- name: Build Docker image
uses: docker/build-push-action#v2
with:
context: .
builder: ${{ steps.buildx.outputs.name }}
push: true
tags: ****.dkr.ecr.us-east-2.amazonaws.com/sharon-test:latest, ****.dkr.ecr.us-east-2.amazonaws.com/sharon-test:${{ github.event.client_payload.tag }}
cache-from: type=gha
cache-to: type=gha,mode=max
file: Dockerfile.api
but it doesn't change a thing,
the cache hit fails in 'Cache Docker layers' evreytime.
Does anyone have a clue of what might be the issue with repository dispatch? or any other issue?
Thank you!
Let's take this example composite action found on Github's documentation:
name: 'Hello World'
description: 'Greet someone'
inputs:
who-to-greet: # id of input
description: 'Who to greet'
required: true
default: 'World'
outputs:
random-number:
description: "Random number"
value: ${{ steps.random-number-generator.outputs.random-id }}
runs:
using: "composite"
steps:
- run: echo Hello ${{ inputs.who-to-greet }}.
shell: bash
- id: random-number-generator
run: echo "::set-output name=random-id::$(echo $RANDOM)"
shell: bash
- run: ${{ github.action_path }}/goodbye.sh
shell: bash
How can we use that specific output random-number in an external workflow that calls this action? I tried the following snippet but currently it seems the workflow cannot read the output variable from the action as it just comes out empty - 'Output - '
jobs:
test-job:
runs-on: self-hosted
steps:
- name: Call Hello World
id: hello-world
uses: actions/hello-world-action#v1
- name: Comment
if: ${{ github.event_name == 'pull_request' }}
uses: actions/github-script#v3
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Output - ${{ steps.hello-world.outputs.random-number.value }}'
})
It seems my attempt was correct with the exception of one detail:
Instead of:
${{ steps.hello-world.outputs.random-number.value }}
It should be referenced without the .value:
${{ steps.hello-world.outputs.random-number}}
Now it works.
I am trying to execute some option steps if the previous step is failing.
I need to download the old artifacts in order to avoid that my Terraform build action is not detecting changes. Therefore I added a diff action to identify if the docker file to build the zip-layer has changed, if there is no change the old artifacts from a previous execution should be downloaded. In some cases the last execution does not contain the artifacts e.g. failure of the jobs. In that case I would like to get the latest version based on the existing docker image.
Note: The code is part of a matrix execution but for simplicity I reduced the action to the problem area.
job_prepare:
.....
job_layers:
needs: job_prepare
runs-on: ubuntu-latest
strategy:
matrix: ${{fromJson(needs.job_prepare.outputs.layer_matrix)}}
steps:
- name: Checkout
uses: actions/checkout#v2
- name: matrix name
run: |
echo $GITHUB_WORKSPACE
echo ${{ matrix.path }}
- uses: technote-space/get-diff-action#v3
id: git_diff
with:
PREFIX_FILTER: ${{ matrix.prefix }}
SUFFIX_FILTER: Dockerfile
- name: LayerDockerBuild
id: layerDocker
if: steps.git_diff.outputs.diff
run: |
docker build ...
docker push ...
- name: Layer via Artifacts
if: (steps.git_diff.outputs.diff == false)
uses: dawidd6/action-download-artifact#v2
with:
workflow: review.yml
name: ${{ matrix.name }}
- name: Layer via Docker
if: steps.git_diff.outputs.diff && ${{ failure() }}
id: layerD
run: |
....
docker pull "docker.pkg.github.com/$REPO_NAME/$IMAGE_ID:$VERSION"
docker run --rm -v $GITHUB_WORKSPACE:/data docker.pkg.github.com/$REPO_NAME/$IMAGE_ID:$VERSION cp /packages/${{ matrix.name }}.zip /data
- name: Upload layer zip
if: ${{ always() }}
uses: actions/upload-artifact#v2
with:
name: ${{ matrix.name }}
path: ${{ matrix.name }}.zip
The problem is basically the logic in the line if: steps.git_diff.outputs.diff && ${{ failure() }}
thanks for your help on any hints how to make the step option when the diff is false and the step ~Layer via Artifacts~ is not failing.
To execute your step if the previous one was not a failure you can just set failure() without brackets. With the if, no need of brackets.
if: steps.git_diff.outputs.diff && failure()
Hope it will help