Github Actions
Github Expressions and Contexts
- Refer Here for github contexts
- Context is a way to access information inside an expression
- Contexts are like namespaces/objects that hold variables we can query
- Expression syntax
${{ context.property }}
Exploring Contexts
- Use the following github action in any repo and push the changes
---
on:
push:
branches:
- main
jobs:
context-job:
runs-on: ubuntu-latest
steps:
- run: "echo Repo: ${{ github.repository }} by ${{ github.actor }}"
- name: Show complete github context
run: echo '${{ toJson(github) }}'
- name: Show complete env context
run: echo '${{ toJson(env) }}'
- name: Show complete runner context
run: echo '${{ toJson(runner) }}'
Variable types in Github
Environmental variables
- We can define variables at workflow, job or step level with env and they can be accessed using
envcontext - Execute the following
---
on:
push:
branches:
- main
env:
GLOBAL_ENV_VAR: learning thoughts global
ORG_NAME: xyz limited
jobs:
context-job:
runs-on: ubuntu-latest
env:
JOB_ENV_VAR: learning thoughts job
ORG_NAME: abc limited
steps:
- name: steps example
env:
STEP_VAR: learning thoughts step
run: echo '${{ env.GLOBAL_ENV_VAR }} ${{ env.JOB_ENV_VAR }} ${{ env.STEP_VAR }}'
- name: same environment
env:
ORG_NAME: 123 limited
run: echo '${{ env.ORG_NAME }}'
Variables and Secrets
- Variables can be defined at org level or repo level
- Secrets can be defined at org level or repo level
- Secrets will be masked as it is for sensitive information
- Create variables and secrets as discussed
- Execute the following workflow
---
on:
push:
branches:
- main
env:
GLOBAL_ENV_VAR: learning thoughts global
ORG_NAME: xyz limited
jobs:
vars-job:
runs-on: ubuntu-latest
steps:
- name: print vars
run: echo 'group = ${{ vars.GROUP_NAME }} user = ${{ vars.USERNAME }} token = ${{ secrets.TOKEN }}'
- Github automatically sets some environment variables which start with
GITHUB_- GITHUB_WORKFLOW
- GITHUB_RUN_ID
- GITHUB_JOB
- RUNNER_OS
Use output of one step in another
- Use the following yaml
MAVEN CI using github actions
- Lets create some variables at repo level with some default values for maven specifics
- Uploading test results
- Uploading test results example
---
name: Manual maven steps for ci
on:
pull_request:
branches:
- main
workflow_dispatch:
jobs:
manual:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v5
- uses: actions/setup-java@v5
with:
distribution: 'temurin'
java-version: '21'
- name: build package
run: mvn ${{ vars.GOAL }}
- name: publish test results
uses: EnricoMi/publish-unit-test-result-action@v2
with:
files: |
**/target/surefire-reports/*.xml
- Now to upload jar files
- We can cache maven as there is support in setup-java action`
---
name: Manual maven steps for ci
on:
pull_request:
branches:
- main
workflow_dispatch:
permissions:
contents: read
checks: write
jobs:
manual:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v5
- uses: actions/setup-java@v5
with:
distribution: 'temurin'
java-version: '21'
cache: maven
- name: build package
run: mvn ${{ vars.GOAL }}
- name: publish test results
uses: EnricoMi/publish-unit-test-result-action@v2
with:
files: |
**/target/surefire-reports/*.xml
- name: Uploading packaged jar files
uses: actions/upload-artifact@v4
with:
name: spring-petclinic-jar
path: '**/target/*.jar'
matrix
- Refer Here for matrix
---
name: Manual maven steps for ci
on:
pull_request:
branches:
- main
workflow_dispatch:
permissions:
contents: read
checks: write
pull-requests: write
jobs:
manual:
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
java: [17, 21]
steps:
- uses: actions/checkout@v5
- uses: actions/setup-java@v5
with:
distribution: 'temurin'
java-version: ${{ matrix.java }}
cache: maven
- name: build package
run: mvn ${{ vars.GOAL }}
- name: publish test results
uses: EnricoMi/publish-unit-test-result-action@v2
with:
files: |
**/target/surefire-reports/*.xml
- name: Uploading packaged jar files
uses: actions/upload-artifact@v4
with:
name: spring-petclinic-${{ matrix.java }}.jar
path: '**/target/*.jar'
Conditions and functions
- Expressions Refer Here
