Describing standard workflow syntax elements in Github
GitHub Workflows are defined using YAML syntax and have a specific structure to automate tasks. Below are the standard syntax elements and their roles.
1. name
Defines the name of the workflow. This is optional but helps identify workflows in the GitHub Actions interface.
xxxxxxxxxx11nameCI Workflow2. on
Specifies the events that trigger the workflow. Events can be repository activities, manual triggers, or scheduled runs.
Examples:
Trigger on a
pushevent:xxxxxxxxxx11onpushTrigger on a
pull_requestevent:xxxxxxxxxx11onpull_requestMultiple events:
xxxxxxxxxx71on2push3branches4main5pull_request6branches7mainScheduled triggers (cron syntax):
xxxxxxxxxx31on2schedule3cron"0 0 " # Runs daily at midnightManual triggers:
xxxxxxxxxx21on2workflow_dispatch
3. jobs
Defines one or more jobs that the workflow will execute. Jobs can run in parallel or sequentially based on dependencies.
xxxxxxxxxx61jobs2 build3 runs-onubuntu-latest4 steps5nameCheckout code6 usesactions/checkout@v3Key Elements:
runs-on: Specifies the environment for the job (e.g.,ubuntu-latest,windows-latest,macos-latest).needs: Defines dependencies between jobs.
xxxxxxxxxx61jobs2 test3 runs-onubuntu-latest4 build5 runs-onubuntu-latest6 needstest4. steps
Lists individual tasks within a job. Steps can use actions or custom shell commands.
xxxxxxxxxx51steps2nameCheckout code3 usesactions/checkout@v34nameRun a shell command5 runecho "Hello, World!"Key Elements:
name: A descriptive name for the step.uses: Specifies a prebuilt action to use.run: Runs a shell command or script.with: Provides input parameters for actions.
xxxxxxxxxx41nameSet up Node.js2 usesactions/setup-node@v33 with4 node-version'16'5. env
Sets environment variables for workflows, jobs, or steps.
Workflow-level:
xxxxxxxxxx21env2 NODE_ENVproductionJob-level:
xxxxxxxxxx51jobs2 build3 runs-onubuntu-latest4 env5 NODE_ENVproductionStep-level:
xxxxxxxxxx51steps2namePrint environment variable3 runecho $NODE_ENV4 env5 NODE_ENVproduction6. outputs
Defines output values from jobs that can be used in other jobs.
xxxxxxxxxx131jobs2 job13 runs-onubuntu-latest4 outputs5 example_output$ steps.example_step.outputs.result 6 steps7idexample_step8 runecho "::set-output name=result::value"9 job210 runs-onubuntu-latest11 needsjob112 steps13runecho "Output from job1 is ${{ needs.job1.outputs.example_output }}"7. defaults
Sets default values for run steps.
xxxxxxxxxx41defaults2 run3 shellbash4 working-directoryscripts8. permissions
Specifies permissions for the workflow's access to the GitHub API.
xxxxxxxxxx21permissions2 contentsread9. secrets
Accesses sensitive information stored in the repository.
xxxxxxxxxx51steps2nameUse secret3 runecho $MY_SECRET4 env5 MY_SECRET$ secrets.MY_SECRET 10. if
Adds conditional execution for jobs or steps.
xxxxxxxxxx41steps2nameRun only on main branch3 ifgithub.ref == 'refs/heads/main'4 runecho "On main branch"11. strategy
Defines a matrix of configurations to test multiple environments or inputs.
xxxxxxxxxx111jobs2 test3 runs-onubuntu-latest4 strategy5 matrix6 node-version12 14 167 steps8nameSet up Node.js9 usesactions/setup-node@v310 with11 node-version$ matrix.node-version 12. timeout-minutes
Sets a timeout for a job.
xxxxxxxxxx41jobs2 build3 runs-onubuntu-latest4 timeout-minutes10Summary
These elements work together to create flexible and powerful workflows. You can customize them to fit the needs of your automation.






















Leave a Reply