Create Zip file of Github Reporitory using a workflow which runs on [windows-latest] - github

I'm trying to create a zip file of my repository using git hub workflow. Below is the code:
name: .NET
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: [windows-latest]
steps:
- uses: actions/checkout#v3
- name: Set Up MS Build
uses: microsoft/setup-msbuild#v1
- name: Restore dependencies
run: nuget restore Solution.sln
- name: Build Solution
run: msbuild Solution.sln
- name: Creating Zip
run: zip -r release.zip . -x ".git/*" ".github/*"
This gives me an error :
> The term 'zip' is not recognized as a name of a cmdlet, function,
> script file, or executable program
.
Figured out that, zip command is not available on Windows.
Tried googling, but no luck. Any resources would be helpful.

Related

GitHub Action - Invalid workflow file - YAML syntax error

I am trying to setup my first GitHub Workflow and I am facing many YAML syntax issues even I am using the official documentation.
I am using the below YAML:
# This is a basic workflow to help you get started with Actions
name: TestWorkflowGithub
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the "main" branch
pull_request:
branches:
- 'testbranch/**'
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# 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:
- name: Checkout the code
uses: actions/checkout#v3
- name: Install PMD
run: |
PMD_VERSION=`cat pmd/pmd-version.txt`
wget https://github.com/pmd/pmd/releases/download/pmd_releases%2F6.54.0/pmd-bin-6.54.0.zip
unzip pmd-bin-6.54.0.zip -d ~
mv ~/pmd-bin-$6.54.0 ~/pmd
~/pmd/bin/run.sh pmd --version
# Run PMD scandd
- name: Run PMD scan
run: ~/pmd/bin/run.sh pmd -d force-app -R pmd/ruleset.xml -f text
GitHub is showing me the below error:
You have an error in your yaml syntax on line 14
Note: the line 14 is "runs-on: ubuntu-latest"
Which is the syntax issue in the above YAML file?
You are missing the job identifier:
jobs:
foo: # <-- This
runs-on: ubuntu-latest
steps:
- name: Checkout the code
uses: actions/checkout#v3
steps:
You can use actionlint or vscode-yaml to avoid such syntax issues next time :)

access script stored in .github directory in github action

I have a my_script.sh file in .github/my_script.sh in my repo.
Below is my YAML file:
jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
with:
path: master
- name: Set sync script in env
run: |
myscript=$(cat .github/my_script.sh)
echo "::set-env name=MY_SCRIPT::$myscript"
But I got this error:
cat: .github/my_script.sh: No such file
Any clue why?
According to https://github.com/actions/checkout, path is the "relative path under $GITHUB_WORKSPACE to place the repository". Note that does not change the working directory.
This means that using path: master will put your repo in a folder named master. I suspect that you meant to check out the master branch instead. Checkout will automatically checkout the branch the workflow was ran on so most of the time, specifying it specifically is not required.
You either want to remove the path argument or change your code to use the correct path: myscript=$(cat master/.github/my_script.sh)
I think specifying working directory will work.
jobs:
main:
runs-on: ubuntu-latest
defaults:
run:
working-directory: .github
steps:
- uses: actions/checkout#v3
- name: step sync script in env
run: cat my_script.sh
I have checked it and it works.

Why github actions cant timeout a single job

I have a workflow in which a run request runs infinitely. i want to stop that run after 5 minutes of it running.
my workflow file:-
name: MSBuild
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
env:
# Path to the solution file relative to the root of the project.
SOLUTION_FILE_PATH: ./genshincheat.sln
# Configuration type to build.
# You can convert this to a build matrix if you need coverage of multiple configuration types.
# https://docs.github.com/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
BUILD_CONFIGURATION: Release
permissions:
contents: read
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout#v1
with:
submodules: recursive
- name: Add MSBuild to PATH
uses: microsoft/setup-msbuild#v1.0.2
- name: Restore NuGet packages
working-directory: ${{env.GITHUB_WORKSPACE}}
run: nuget restore ${{env.SOLUTION_FILE_PATH}}
- name: Build
working-directory: ${{env.GITHUB_WORKSPACE}}
# Add additional options to the MSBuild command line here (like platform or verbosity level).
# See https://learn.microsoft.com/visualstudio/msbuild/msbuild-command-line-reference
run: msbuild /m /p:Configuration=${{env.BUILD_CONFIGURATION}} ${{env.SOLUTION_FILE_PATH}}
- uses: montudor/action-zip#v1
with:
args: zip -qq -r bin.zip dir
- uses: actions/checkout#v2
- run: mkdir -p path/to/artifact
- run: echo hello > path/to/artifact/world.txt
- uses: actions/upload-artifact#v3
with:
name: bin.zip
path: ./bin.zip
the "build" runs infinitely any way to stop it after 5 mins so it can carry out next jobs? it runs infinitely becauseafter build it runs the built program so i cant exit that ;-;. any help is appreciated
There are different fields that can help you achieve what you want.
At the job level: job.<id>.timeout-minutes (defining a job timeout)
At the step level: job.<id>.steps.timeout-minutes (defining a step timeout)
Which would look like this in your case:
At the job level:
build:
runs-on: windows-latest
timeout-minutes: 5
steps:
[...]
At the step which never ends (example):
- name: Build
timeout-minutes: 5
working-directory: ${{env.GITHUB_WORKSPACE}}
# Add additional options to the MSBuild command line here (like platform or verbosity level).
# See https://learn.microsoft.com/visualstudio/msbuild/msbuild-command-line-reference
run: msbuild /m /p:Configuration=${{env.BUILD_CONFIGURATION}} ${{env.SOLUTION_FILE_PATH}}
Another reference on the Github Community

GitHub Actions - How clean up unchanged/uncommited files before upload to SFTP Server

I´m tring to config a GitHub Action to deploy my application to SFTP file.
My application has 6700 files and I would like to upload only changed/commited files.
How can I remove unchanged and/or uncommited files before upload to SFTP?
This way, my one file modification deploy would be so faster than upload 6k files.
name: CI
on:
push:
branches: [ main ]
workflow_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
name: Deploy Job
steps:
- name: Checkout
uses: actions/checkout#v2
with:
fetch-depth: 2
- name: Deploy files
uses: wlixcc/SFTP-Deploy-Action#v1.0
with:
username: 'deploy_user'
server: 'server_ip'
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
local_path: './www/*'
remote_path: '/www'
args: '-o ConnectTimeout=10'
To list down all the files that are updated/committed in the given commit, you can use this command:
$ git diff-tree --no-commit-id --name-only -r $GITHUB_SHA
index.html
src/application.js
Then you can use this to delete all the files that are not on this list. This needs some bash digging. One hack I can think of is to make a temporary directory to copy updated files and only upload these files.

How can I use Github Actions to trigger downloading a static copy of a website and push to S3?

I am trying to use Github actions on the Push event to my master branch for running a wget command to mirror a website and download its contents as static files and then zip them together for uploading to an s3 bucket. Here is my test_events.yml file stored under .github/workflows in my git repo:
name: create a mirror of website and zip the saved contents
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
mirror-website:
name: mirrors the website
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout#v2
- name: wget
uses: wei/wget#v1
with:
args: -O logfile.txt -P ./actions/ https://adappt.co.uk
zip-files:
name: Zips the Saved file
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout#v2
- name: Prepares a zip file
uses: montudor/action-zip#v1.0.0
with:
args: zip -qq -r adappt_co_uk.zip . -i /actions/adappt.co.uk
Problem is whenever I push a change in my repo, the action gets triggered and gets completed without errors. But it does not save the web files to the given location, and hence, doesn't perform the zip operation as well.
What am I doing wrong here? Is there any other way apart from using wget?