My current GitHub workflow executes test cases even for .md file or .txt file.
I want to skip the test for any changes made to documentation files. My folder structure is:
project
| source-codes
│ docs
│ file001.txt
| file.rst
| file.md
The GitHub workflow must skip for docs folder or anything with .rst or .md file.
I have the code below but doesn't seem to work. I am currently trying this from my feature branch.
.github/workflows/main.yml
# This is a basic workflow to help you get started with Actions
name: Documentation Test
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the "main" branch
push:
branches:
- 'main'
# Allows you to run this workflow manually from the Actions tab
# workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "Documentation Testcase"
documentation_testcase:
# The type of runner that the job will run on
name: Documentation Job
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Checkout
uses: actions/checkout#v3
with:
# Checkout as many commits as needed for the diff
fetch-depth: 2
- shell: pwsh
# Give an id to the step, so we can reference it later.
id: check_file_changed
run: |
# Diff HEAD with the previous commit
$diff = git diff --name-only HEAD^ HEAD
# Check if a file under docs/ or with the .md extension has changed (added, modified, deleted)
$SourceDiff = $diff | Where-Object { $_ -match '^docs/' -or $_ -match '.rst$' }
$HasDiff = $SourceDiff.Length -gt 0
# Set the output named "docs_changed"
Write-Host "::set-output name=docs_changed::$HasDiff"
# Run the step only with "docs_changed" equals "True"
- shell: pwsh
# steps.<step_id>.outputs.<output name>
if: steps.check_file_changed.outputs.docs_changed == 'True'
run: echo publish docs
Instead of complex shell script, I'd recommend using paths-ignore setting.
Read more about this configuration here: Link
Example Action: Link
Code Example:
on:
push:
paths-ignore:
- 'README.md'
- 'backup/**'
- 'masterDir/contentDir/**/*.draft.md'
Related
This question already has an answer here:
GitHub Actions - "Node.js 12 actions are deprecated." although I upgraded everything to v18
(1 answer)
Closed 23 days ago.
This post was edited and submitted for review 23 days ago and failed to reopen the post:
Original close reason(s) were not resolved
I am practicing GitHub and use the introduction-to-GitHub repository to create a repository.
When I make a repository, I receive an en error message in the Accion tab which says:
"Process completed with exit code 129."
Node.js 12 actions are deprecated. Please update the following actions to use Node.js 16: actions/checkout#v2. For more information, see https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/.
Here's my workflow:
name: Step 1, Create a branch
on:
workflow_dispatch:
create:
# Reference https://docs.github.com/en/actions/security-guides/automatic-token-authentication
permissions:
# Need `contents: read` to checkout the repository
# Need `contents: write` to update the step metadata
contents: write
jobs:
on_create_a_branch:
name: On create a branch
# We will only run this action when:
# 1. This repository isn't the template repository
# 2. The event is a branch
# 3. The branch name is `my-first-branch`
# Reference https://docs.github.com/en/actions/learn-github-actions/contexts
# Reference https://docs.github.com/en/actions/learn-github-actions/expressions
if: ${{ !github.event.repository.is_template && github.ref_type == 'branch' && github.ref_name == 'my-first-branch' }}
# We'll run Ubuntu for performance instead of Mac or Windows
runs-on: ubuntu-latest
steps:
# We'll need to check out the repository so that we can edit the README
- name: Checkout
uses: actions/checkout#v2
with:
fetch-depth: 0 # Let's get all the branches
# Update README to close <details id=1> and open <details id=2>
# and set STEP to '2'
- name: Update to step 2
uses: skills/action-update-step#v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
from_step: 1
to_step: 2
branch_name: my-first-branch
Here are the logs from the step that's failing:
Run skills/action-update-step#v1
Run echo "Check that all required env variables are set"
Check that all required env variables are set
Check that we are on FROM_STEP
Make sure we are on the base branch ()
Your branch is up to date with 'origin/my-first-branch'.
Remove 'open' from any <details> tags
Add 'open' to step TO_STEP
Update all HTML comments to hide everything
Show the current TO_STEP
Update the STEP file to TO_STEP
Commit the files, and push to base branch
[my-first-branch a0860a2] Update to 2 in STEP and README.md
2 files changed, 3 insertions(+), 3 deletions(-)
To https://github.com/halavehzadeh/lesson1
7c47deb..a0860a2 my-first-branch -> my-first-branch
If BRANCH_NAME, update that branch as well
Already on 'my-first-branch'
Your branch is up to date with 'origin/my-first-branch'.
usage: git cherry-pick [--edit] [-n] [-m <parent-number>] [-s] [-x] [--ff]
[-S[<keyid>]] <commit>...
or: git cherry-pick (--continue | --skip | --abort | --quit)
--quit end revert or cherry-pick sequence
--continue resume revert or cherry-pick sequence
--abort cancel revert or cherry-pick sequence
--skip skip current commit and continue
--cleanup <mode> how to strip spaces and #comments from message
-n, --no-commit don't automatically commit
-e, --edit edit the commit message
-s, --signoff add a Signed-off-by trailer
-m, --mainline <parent-number>
select mainline parent
--rerere-autoupdate update the index with reused conflict resolution if possible
--strategy <strategy>
merge strategy
-X, --strategy-option <option>
option for merge strategy
-S, --gpg-sign[=<key-id>]
GPG sign commit
-x append commit name
--ff allow fast-forward
--allow-empty preserve initially empty commits
--allow-empty-message
allow commits with empty messages
--keep-redundant-commits
keep redundant, empty commits
Error: Process completed with exit code 129.
I notice I should change action/cheout#v2 to version 3, but I don't know should I do that.
I also modified all files (changed in action/cheout#v2 to version 3) but still get the same error message.
Here is my workflow file:
name: Step 1, Create a branch
on:
workflow_dispatch:
create:
# Reference https://docs.github.com/en/actions/security-guides/automatic-token-authentication
permissions:
# Need `contents: read` to checkout the repository
# Need `contents: write` to update the step metadata
contents: write
jobs:
on_create_a_branch:
name: On create a branch
# We will only run this action when:
# 1. This repository isn't the template repository
# 2. The event is a branch
# 3. The branch name is `my-first-branch`
# Reference https://docs.github.com/en/actions/learn-github-actions/contexts
# Reference https://docs.github.com/en/actions/learn-github-actions/expressions
if: ${{ !github.event.repository.is_template && github.ref_type == 'branch' && github.ref_name == 'my-first-branch' }}
# We'll run Ubuntu for performance instead of Mac or Windows
runs-on: ubuntu-latest
steps:
# We'll need to check out the repository so that we can edit the README
- name: Checkout
uses: actions/checkout#v2
with:
fetch-depth: 0 # Let's get all the branches
# Update README to close <details id=1> and open <details id=2>
# and set STEP to '2'
- name: Update to step 2
uses: skills/action-update-step#v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
from_step: 1
to_step: 2
branch_name: my-first-branch
I have this workflow in a repo called terraform-do-database and I'm trying to use a reusable workflow coming from the public repo foo/git-workflows/.github/workflows/tag_validation.yaml#master
name: Tag Validation
on:
pull_request:
branches: [master]
push:
branches:
- '*' # matches every branch that doesn't contain a '/'
- '*/*' # matches every branch containing a single '/'
- '**' # matches every branch
- '!master' # excludes master
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
tag_check:
uses: foo/git-workflows/.github/workflows/tag_validation.yaml#master
And this is the reusable workflow file from the public git-workflows repo that has the script that should run on it. What is happening is that the workflow is trying to use a script inside the repo terraform-do-database
name: Tag Validation
on:
pull_request:
branches: [master]
workflow_call:
jobs:
tag_check:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout#v3
# Runs a single command using the runners shell
- name: Verify the tag value
run: ./scripts/tag_verify.sh
So the question: How can I make the workflow use the script stored in the git-worflows repo instead of the terraform-do-database?
I want to have a single repo where I can call the workflow and the scripts, I don't want to have everything duplicated inside all my repos.
I have found that if I wrap the script into a composite action. I can use GitHub context github.action_path to locate the scripts.
Example:
run: ${{ github.action_path }}/scripts/foo.sh
One way to go about this is perform a checkout inside your reusable workflow that essentially clones the content of the repo where your scripts are and only then you can access it. It's not the cleanest solution but it works.
Perform a second checkout, to clone your repo that has the reusable workflow into a dir reusable-workflow-repo
- name: Checkout reusable workflow dir
uses: actions/checkout#v3
with:
repository: <your-org>/terraform-do-database
token: ${{ secrets.GIT_ACCESS_TOKEN }}
path: reusable-workflow-repo
Now you have all the code you need inside reusable-workflow-repo. Use ${GITHUB_WORKSPACE} to find the current path and simply append the path to the script.
- name: Verify the tag value
run: ${GITHUB_WORKSPACE}/reusable-workflow-repo/scripts/tag_verify.sh
I was able to solve it adding a few more commands to manually download the script and execute it.
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout#v3
# Runs a single command using the runners shell
- name: Check current directory
run: pwd
- name: Download the script
run: curl -o $PWD/tag_verify.sh https://raw.githubusercontent.com/foo/git-workflows/master/scripts/tag_verify.sh
- name: Give script permissions
run: chmod +x $PWD/tag_verify.sh
- name: Execute script
run: $PWD/tag_verify.sh
Following Kaleby Cadorin example but for the case where the script is in a private repository
- name: Download & run script
run: |
curl --header "Authorization: token ${{ secrets.MY_PAT }}" \
--header 'Accept: application/vnd.github.raw' \
--remote-name \
--location https://raw.githubusercontent.com/COMPANY/REPO/BRANCH/PATH/script.sh
chmod +x script.sh
./script.sh
Note: GITHUB_TOKEN doesn't seem to work here, a PAT is required.
According to this thread on github-community the script needs to be downloaded/checked out separatly.
The "reusable" workflow you posted is not reusable in this sense, because since it is not downloading the script the workflow can only run within its own repository (or a repository that already has the script).
I am trying to create a bug tracker that allows me to record the error messages of the python script I run. Here is my YAML file at the moment:
name: Bug Tracker
#Controls when the workflow will run
on:
# Triggers the workflow on push request events
push:
branches: [ main ]
# Allows you to run this workflow manually from the Actions tab (for testing)
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
build:
# Self Hosted Runner
runs-on: windows-latest
# Steps for tracker to get activated
steps:
# Checks-out your repository under BugTracker so the job can find it
- uses: actions/checkout#v2
- name: setup python
uses: actions/setup-python#v2
with:
python-version: 3.8
# Runs main script to look for
- name: Run File and collect bug
id: response
run: |
echo Running File...
python script.py
echo "${{steps.response.outputs.result}}"
Every time I run the workflow I can't save the error code. By save the error code, I mean for example... if the python script produces "Process completed with exit code 1." then I can save that to a txt file. I've seen cases where I could save if it runs successfully. I've thought about getting the error in the python script but I don't want to have to add the same code to every file if I don't have to. Any thoughts? Greatly appreciate any help or suggestions.
Update: I have been able to successfully use code in python to save to the txt file. However, I'm still looking to do this in Github if anyone has any suggestions.
You could :
redirect the output to a log file while capturing the exit code
set an output with the exit code value like:
echo ::set-output name=status::$status
in another step, commit the log file
in a final step, check that the exit code is success (0) otherwise exit the script with this exit code
Using ubuntu-latest, it would be like this:
name: Bug Tracker
on: [push,workflow_dispatch]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: setup python
uses: actions/setup-python#v2
with:
python-version: 3.8
- name: Run File and collect logs
id: run
run: |
echo Running File...
status=$(python script.py > log.txt 2>&1; echo $?)
cat log.txt
echo ::set-output name=status::$status
- name: Commit log
run: |
git config --global user.name 'GitHub Action'
git config --global user.email 'action#github.com'
git add -A
git checkout master
git diff-index --quiet HEAD || git commit -am "deploy workflow logs"
git push
- name: Check run status
if: steps.run.outputs.status != '0'
run: exit "${{ steps.run.outputs.status }}"
On windows, I think you would need to update this part:
status=$(python script.py > log.txt 2>&1; echo $?)
cat log.txt
I have two ps1 scripts in Github Actions.
My scenario:
The first script executes before build
Project builds
The second script executes after build.
I need to set the value inside the first script and use it inside the second script.
So I decided to use BUILD_NUMBER environment variable and set it to 10 as a default value.
jobs:
Droid:
runs-on: windows-latest
env:
BUILD_NUMBER: "10"
Inside the first script I tried to set this variable in several ways but in the second script the value of BUILD_NUMBER was 10.
My attempts to set it:
[Environment]::SetEnvironmentVariable($env:BUILD_NUMBER, $buildNumber, 'Machine')
$env:BUILD_NUMBER: '123'
But inside the second script I was getting 10 value by this $newName = "${env:BUILD_NUMBER}"
The whole code of Github Actions side:
name: CI
# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
push:
branches:
- 'master'
- 'develop'
- 'feature/*'
- 'rc/*'
pull_request:
branches:
- 'master'
- 'develop'
- 'feature/*'
- 'rc/*'
jobs:
Droid:
runs-on: windows-latest
env:
DOTNET_CLI_TELEMETRY_OPTOUT: 'true'
BUILD_NUMBER: "10"
steps:
- uses: actions/checkout#v1
- name: Run a calculate version and set sign in password script
run: .\Scripts\CalculateVersionAndSetSignPassword.ps1
shell: powershell
# Build goes here. It is skipped by me for testing purposes
- uses: actions/checkout#v1
- name: Run a change apk name script
run: |
.\Scripts\ChangeApkName.ps1
shell: powershell
set-env was depricated - please check GitHub Actions: Deprecating set-env and add-path commands
As a replacement you may use
echo "BUILD_NUMBER=yellow" >> $GITHUB_ENV
and then:
jobs:
show:
runs-on: ubuntu-latest
steps:
- name: Is variable exported?
run: |
echo "BUILD_NUMBER=yellow" >> $GITHUB_ENV
- name: PowerShell script
# You may pin to the exact commit or the version.
# uses: Amadevus/pwsh-script#25a636480c7bc678a60bbf4e3e5ac03aca6cf2cd
uses: Amadevus/pwsh-script#v2.0.0
continue-on-error: true
with:
# PowerShell script to execute in Actions-hydrated context
script: |
Write-Host $env:BUILD_NUMBER
- name: Read exported variable
run: |
echo "${{ env.BUILD_NUMBER}}"
To set environment variables in a step that can be referenced in another, you will need to use the ::set-env syntax.
In your case, your first script will have to run this command:
Write-Output "::set-env name=BUILD_NUMBER::$buildNumber"
And the second script should be able to reference it with $env:BUILD_NUMBER.
[6/20/20] Update with full example.
Action yaml file (Inline powershell will have similar behavior than with a ps1):
name: StackOverFlow
on:
push:
branches: [ master ]
jobs:
build:
runs-on: windows-latest
steps:
- run: |
$buildNumber = "12345"
Write-Output "::set-env name=BUILD_NUMBER::$buildNumber"
- run: Write-Output "Doing something else..."
- run: Write-Output "The build number is $env:BUILD_NUMBER"
Output logs:
2020-06-20T23:13:23.3209811Z ##[section]Starting: Request a runner to run this job
2020-06-20T23:13:23.5144969Z Can't find any online and idle self-hosted runner in current repository that matches the required labels: 'windows-latest'
2020-06-20T23:13:23.5145013Z Can't find any online and idle self-hosted runner in current repository's account/organization that matches the required labels: 'windows-latest'
2020-06-20T23:13:23.5145038Z Found online and idle hosted runner in current repository's account/organization that matches the required labels: 'windows-latest'
2020-06-20T23:13:23.6348644Z ##[section]Finishing: Request a runner to run this job
2020-06-20T23:13:29.9867339Z Current runner version: '2.263.0'
2020-06-20T23:13:29.9982614Z ##[group]Operating System
2020-06-20T23:13:29.9983190Z Microsoft Windows Server 2019
2020-06-20T23:13:29.9983380Z 10.0.17763
2020-06-20T23:13:29.9983515Z Datacenter
2020-06-20T23:13:29.9983691Z ##[endgroup]
2020-06-20T23:13:29.9983875Z ##[group]Virtual Environment
2020-06-20T23:13:29.9984067Z Environment: windows-2019
2020-06-20T23:13:29.9984247Z Version: 20200608.1
2020-06-20T23:13:29.9984524Z Included Software: https://github.com/actions/virtual-environments/blob/win19/20200608.1/images/win/Windows2019-Readme.md
2020-06-20T23:13:29.9984752Z ##[endgroup]
2020-06-20T23:13:29.9985890Z Prepare workflow directory
2020-06-20T23:13:30.0151643Z Prepare all required actions
2020-06-20T23:13:30.9154166Z ##[group]Run $buildNumber = "12345"
2020-06-20T23:13:30.9154566Z [36;1m$buildNumber = "12345"[0m
2020-06-20T23:13:30.9154784Z [36;1mWrite-Output "::set-env name=BUILD_NUMBER::$buildNumber"[0m
2020-06-20T23:13:30.9820753Z shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
2020-06-20T23:13:30.9821156Z ##[endgroup]
2020-06-20T23:13:43.2981407Z ##[group]Run Write-Output "Doing something else..."
2020-06-20T23:13:43.2981812Z [36;1mWrite-Output "Doing something else..."[0m
2020-06-20T23:13:43.3022226Z shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
2020-06-20T23:13:43.3022501Z env:
2020-06-20T23:13:43.3022706Z BUILD_NUMBER: 12345
2020-06-20T23:13:43.3022906Z ##[endgroup]
2020-06-20T23:13:43.8091340Z Doing something else...
2020-06-20T23:13:43.8671648Z ##[group]Run Write-Output "The build number is $env:BUILD_NUMBER"
2020-06-20T23:13:43.8671986Z [36;1mWrite-Output "The build number is $($env:BUILD_NUMBER)"[0m
2020-06-20T23:13:43.8717102Z shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
2020-06-20T23:13:43.8717288Z env:
2020-06-20T23:13:43.8718175Z BUILD_NUMBER: 12345
2020-06-20T23:13:43.8718286Z ##[endgroup]
2020-06-20T23:13:44.4148124Z The build number is 12345
2020-06-20T23:13:44.4368449Z Cleaning up orphan processes
Found the resolution in Michael Stum`s repo that he provided in this question:
The key was Get-ChildItem Env: | Where-Object {$_.Name -Match "^MH_"} | %{ echo "::set-output name=$($_.Name)::$($_.Value)" } in .yml and $Env:MH_BUILD_VERSION = $version in .ps1 script file in his repository.
So I successfully retrieved an output from .ps1 script and used it in Github Actions.
I want to run periodically ipynb file in my github repository (Like every 30 minutes).
I know that I can use Github Action to create yml file for this progress but I have no idea how to reorganize yml file.
How can I do it?
Here is my test tml file defined below.
# This is a basic workflow to help you get started with Actions
name: CI
# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
schedule:
- cron: '*/5 * * * *'
push:
branches: [ master ]
pull_request:
branches: [ master ]
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout#v2
# Runs a single command using the runners shell
- name: Run a one-line script
run: echo Hello, world!
# Runs a set of commands using the runners shell
- name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project.
You can check out this GitHub action that runs your jupyter notebook and lets you upload the artifacts. As for how to organize your workflow file, you can read the documentation here.