I have a github actions workflow like this:
name: Print test type
on: [push]
jobs:
build:
strategy:
fail-fast: false
matrix:
test_type: ['test-alpha', 'test-beta']
os: [ubuntu-latest]
python-version: ['3.7', '3.9']
include:
- os: windows-latest
python-version: '3.8'
runs-on: ${{ matrix.os }}
steps:
- name: Print Alpha
if: ${{ matrix.test_type == 'test-alpha'}}
run: echo "test type is alpha. the os name is ${{ runner.os }}"
- name: Print Beta
if: ${{ matrix.test_type == 'test-beta'}}
run: echo "test type is beta. the os name is ${{ runner.os }}"
When I run this workflow, the windows CI starts and completes but it is not showing any output. In particular, the if statements are not getting executed. Am I missing some point here? Because when I include the test-type in matrix, I expect to also see windows X py3.8 X {alpha, beta} combination in workflow.
In the ubuntu CI, it works fine - getting executed for all combinations in the matrix (ubuntu X {py3.7,py3.9} X {alpha,beta}.)
The include method does not work here because Github tries to extend the already existing run configurations. If this does not work (which is the case here), then a new configuration is created for each include. (For more information: https://docs.github.com/en/actions/using-jobs/using-a-matrix-for-your-jobs#expanding-or-adding-matrix-configurations)
In your case, however, the new created configurations do not contain a specified test-type. Now there are two possibilities. Either add the test-type manually:
name: Print test type
on: [push]
jobs:
build:
strategy:
fail-fast: false
matrix:
test_type: ['test-alpha', 'test-beta']
os: [ubuntu-latest]
python-version: ['3.7', '3.9']
include:
- os: windows-latest
python-version: '3.8'
test_type: 'test-alpha'
- os: windows-latest
python-version: '3.8'
test_type: 'test-beta'
runs-on: ${{ matrix.os }}
steps:
- name: Print Alpha
if: ${{ matrix.test_type == 'test-alpha'}}
run: echo "test type is alpha. the os name is ${{ runner.os }}"
- name: Print Beta
if: ${{ matrix.test_type == 'test-beta'}}
run: echo "test type is beta. the os name is ${{ runner.os }}"
or remove the unwanted combinations:
name: Print test type
on: [push]
jobs:
build:
strategy:
fail-fast: false
matrix:
test_type: ['test-alpha', 'test-beta']
os: [ubuntu-latest, windows-latest]
python-version: ['3.7', '3.8', '3.9']
exclude:
- os: windows-latest
python-version: '3.7'
- os: windows-latest
python-version: '3.9'
- os: ubuntu-latest
python-version: '3.8'
runs-on: ${{ matrix.os }}
steps:
- name: Print Alpha
if: ${{ matrix.test_type == 'test-alpha'}}
run: echo "test type is alpha. the os name is ${{ runner.os }}"
- name: Print Beta
if: ${{ matrix.test_type == 'test-beta'}}
run: echo "test type is beta. the os name is ${{ runner.os }}"
Related
I am using Github Actions to push an image into GCP Artifact Registry and later deploy to Cloud Run
All the process goes fine, except the automatic deploying to Cloud Run.
Below is the link for the example that guided me
https://github.com/codeedu/live-imersao-fullcycle10-nestjs-tests/blob/main/.github/workflows/ci_cd.yml
The error is as below:
Deploying...
failed
Deployment failed
ERROR: (gcloud.run.deploy) spec.template.spec.containers[0].image: Must provide an image URL to deploy
I appreciate any help to accomplish this task
Below is the workflow file:
name: CI and CD
on:
workflow_dispatch:
push:
branches: [main, develop]
env:
REGISTRY: gcr.io
IMAGE_NAME: ${{ secrets.GCP_PROJECT_NAME }}/${{ secrets.CLOUD_RUN_SERVICE }}
REGION: us-central1
# REGISTRY_GIT: ghcr.io
# IMAGE_NAME_GIT: ${{ github.repository }}
jobs:
test-code:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout#v3
- name: Use Node.js 16.x
uses: actions/setup-node#v3
with:
node-version: 16.x
- run: npm ci
- run: npm run test
build-image:
needs: test-code
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-20.04
outputs:
tags: ${{ steps.meta.outputs.tags }}
concurrency: build-image-process
steps:
- name: Checkout repository
uses: actions/checkout#v3
# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/setup-buildx-action#79abd3f86f79a9d68a23c75a09a9a85889262adf
# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action#28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
with:
registry: ${{ env.REGISTRY }}
username: _json_key
#username: ${{ github.actor }}
password: ${{ secrets.GCP_SERVICE_ACCOUNT }}
#password: ${{ secrets.GITHUB_TOKEN }}
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action#98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action#ac9327eae2b366085ac7f6a2d02df8aa8ead720a
if: ${{ github.event_name != 'pull_request' }}
with:
context: .
file: ./Dockerfile.prod
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Outputs tags
run: echo "${{ steps.meta.outputs.tags }}"
deploy-image:
needs: build-image
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-20.04
steps:
- name: Checkout repository
uses: actions/checkout#v3
- id: 'auth'
uses: 'google-github-actions/auth#v0'
with:
credentials_json: '${{ secrets.GCP_SERVICE_ACCOUNT }}'
- name: 'Deploy to Cloud Run'
uses: 'google-github-actions/deploy-cloudrun#v0'
with:
service: ${{ secrets.CLOUD_RUN_SERVICE }}
image: ${{ needs.build-image.outputs.tags }}
region: ${{ env.REGION }}
I have a Github actions workflow that will be called via either workflow_call or workflow_dispatch and it will take an input of environment, and I want to map that to details about this environment.
I tried...
Example workflow:
name: Deploy Console
on:
workflow_dispatch:
inputs:
environment:
description: "The environment you are releasing to"
required: true
type: environment
release-tag:
description: "The tag that you are releasing"
required: true
type: string
workflow_call: # Only used for Dev.
inputs:
environment:
description: "The environment you are releasing to"
required: true
type: string
release-tag:
description: "The tag that you are releasing"
required: true
type: string
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
environment:
name: ${{ inputs.environment }}
url: https://console.${{ inputs.environment == 'prod' && '' || format('{0}.', inputs.environment) }}website.com
env:
ARTIFACT_FILENAME: website-console-${{ matrix.angular-build }}-${{ inputs.release-tag }}.tgz
strategy:
matrix:
environment:
- ${{ inputs.environment }}
include:
- environment: dev
s3-bucket: website-dev-console
cloudfront-distribution: 10101010101010
angular-build: dev
- environment: smoke
s3-bucket: website-smoke-console
cloudfront-distribution: 10101010101011
angular-build: prod
- environment: prod
s3-bucket: website-prod-console
cloudfront-distribution: 10101010101012
angular-build: prod
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials#v1
with:
role-to-assume: arn:aws:iam::123412341234:role/github-oidc-console
aws-region: us-west-2
- uses: robinraju/release-downloader#v1.6
with:
tag: ${{ inputs.release-tag }}
fileName: ${{ env.ARTIFACT_FILENAME }}
- run: tar -xzf ${{ env.ARTIFACT_FILENAME }}
- run: aws s3 sync dist s3://${{ matrix.s3-bucket }} --quiet
- run: aws cloudfront create-invalidation --distribution-id ${{ matrix.cloudfront-distribution }} --paths '/*'
My goal is that when the workflow is called, there is one-and-only-one job spawned. I want some map of configuration that is first-class yaml rather than using an additional step in the job to define it. It doesn't need to be written in matrix form, but that's what I thought would work, although this iteration has the workflow spawning 3 jobs, one for each environment.
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.
Given the following quite standard workflow:
name: Test
on: [push, pull_request]
jobs:
test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
ruby: [3.0, 3.1]
runs-on: ${{ matrix.os }}
steps:
- name: Check out
uses: actions/checkout#v2
- name: Set up Ruby ${{ matrix.ruby }}
uses: ruby/setup-ruby#v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run tests
run: SPEC_SCOPE=all bundle exec rake
The separate runs of test according to the matrix are labelled like so:
For the sake of better understanding, can the way the matrix is labelled be changed from "ubuntu-latest, 3.1" to something like "ubuntu-latest, ruby-3.1"?
#dhannanjay answered this on Github
You can change the name for a job by providing a name property. You can access all the metadata in the job configuration.
name: Run tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 13.x]
mongodb-version: [4.0, 4.2]
name: Node.js ${{ matrix.node-version }} - MongoDB ${{ matrix.mongodb-version }}
steps:
- name: …
(source: https://futurestud.io/tutorials/github-actions-customize-the-job-name)
I have the following job on GitHub actions. And I would to like to skip the macOS x86 configuration: is there a way to do this?
build-and-push-native-libraries:
name: Build and push native library on ${{ matrix.os }} ${{ matrix.architecture }}
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, macOS, windows-latest]
java: [15]
architecture: [x86, x64]
include:
- compiler: gcc
gcc: 8
runs-on: ${{ matrix.os }}
steps:
- name: Set up JDK ${{ matrix.java }}
uses: actions/setup-java#v1
with:
java-version: ${{ matrix.java }}
architecture: ${{ matrix.architecture }}
- uses: actions/checkout#v2
- if: startsWith(matrix.os, 'ubuntu') == true
name: Change the permissions of the build script of external classes
run: chmod +x ./java/src/main/resources/compileExternalClasses.sh
- name: Build and push native library
run: |
mvn -B clean package -DskipTests=true --file ./native/pom.xml
git config user.name "${{ github.event.head_commit.committer.name }}"
git config user.email "${{ github.event.head_commit.committer.email }}"
git pull origin experimental
git commit -am "Generated library for ${{ matrix.os }} ${{ matrix.architecture }}" --allow-empty
git push
You can use exclude
You can remove a specific configurations defined in the build matrix using the exclude option. Using exclude removes a job defined by the build matrix. The number of jobs is the cross product of the number of operating systems (os) included in the arrays you provide, minus any subtractions (exclude).
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, windows-latest, ubuntu-18.04]
node: [8, 10, 12, 14]
exclude:
# excludes node 8 on macOS
- os: macos-latest
node: 8
In your case it would be:
matrix:
os: [ubuntu-latest, macOS, windows-latest]
java: [15]
architecture: [x86, x64]
include:
- compiler: gcc
gcc: 8
exclude:
- os: macOS