Github yaml file pulling a file from another branch - yaml

I have 2 branches, main and yamltesting. In main I have a test.py file and my yaml file is triggered on push. The yaml file should build an ubuntu linux OS checkout my repo and run a git script to extract the test.py file from "main" branch.
name: CI for Hello World
on:
push:
branches:
- yamlTesting
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-python#v2
with:
python-version: 3.11
- name: Install Libraries
run: |
pip install flake8 pytest pytest-cov
- name: Checkout own repo
uses: actions/checkout#v2
with:
fetch-depth: "0"
- name: git
run: |
# setup the username and email. I tend to use 'GitHub Actions Bot' with no email by default
git --version
git config user.name "Username"
git config user.email "email#gmail.com"
git status
git tag
git checkout main -- test.py
- name: list our contents
run: |
ls
My issue is that when the action runs this error appears
error: pathspec 'main' did not match any file(s) known to git
error: pathspec 'test.py' did not match any file(s) known to git
Error: Process completed with exit code 1.
I tried the following variations I found.
git checkout "main" -- test.py
git checkout main test.py

Extract file from remote branch needs to use remotes/origin/<branchName> instead of just the branch name

Related

run pre-commit.com script for golang in github actions

I'm trying to run pre-commit.com script with some hooks related to golang in github actions. Seems like the testing environment lack of some tools to execute go-imports and golangci-lint.
I've added steps for setting up required tools in the environment prior to pre-commit step, but it doesn't help.
.pre-commit-config.yaml:
repos:
- repo: https://github.com/dnephin/pre-commit-golang
rev: v0.5.0
hooks:
- id: go-imports
- id: golangci-lint
- id: go-unit-tests
github action file config:
name: pre-commit
on:
pull_request:
push:
branches: [main]
jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-python#v2
- uses: actions/setup-go#v3
- run: go install golang.org/x/tools/cmd/goimports#latest
- run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.49.0
- uses: pre-commit/action#v2.0.2
Gihub Action Output:
all go invironments set-up steps completed successfully
Details of pre-commit/action#v2.0.2:
[...]
[INFO] This may take a few minutes...
go imports...............................................................Failed
- hook id: go-imports
- exit code: 127
/home/runner/.cache/pre-commit/repow0byklud/run-go-imports.sh: line 8: goimports: command not found
golangci-lint............................................................Failed
- hook id: golangci-lint
- exit code: 127
/home/runner/.cache/pre-commit/repow0byklud/run-golangci-lint.sh: 2: exec: golangci-lint: not found
go-unit-tests............................................................Passed
[...]
So, the issue was that .../go/bin directory are not being added to $PATH in the execution environment after go tools installation (so goimports and golangci-lint are not visible for BASH)
($PATH is itself being wrapped in the $GITHUB_ENV due to github actions specific.)
This statement prior to pre-commit action execution can resolve the issue (see full code in the end):
run: echo "PATH=$PATH:/home/runner/go/bin" >> $GITHUB_ENV
Thanks for #Anthony Sottile in the comments to original question
Github Action settings code:
name: pre-commit
on:
pull_request:
push:
branches: [main]
jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-python#v2
- uses: actions/setup-go#v3
- run: go install golang.org/x/tools/cmd/goimports#latest
- run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s - -b $(go env GOPATH)/bin v1.49.0
- run: echo "PATH=$PATH:/home/runner/go/bin" >> $GITHUB_ENV
- uses: pre-commit/action#v2.0.2

How to bump version with GitHub Actions

I want to bump the version by running my shell script. I am manually triggering the GitHub Actions with inputs. I am using GitHub API to get the latest version and I want to run my shell script with the version taken from GitHub API.
I am able to achieve everything. It's just that I am not able to get the version output.
This is my GitHub Workflow:
#############################################################################
# GitHub Action to bump release version
#
#############################################################################
name: "Bump version and Update Milestone"
on:
workflow_dispatch:
inputs:
version:
description: 'New Version'
required: true
default: 'warning'
permissions:
contents: write
pull-requests: write
jobs:
printInputs:
runs-on: ubuntu-latest
steps:
- run: |
echo "Latest Version: ${{ github.event.inputs.version }}"
bump:
name: Bump version
runs-on: ubuntu-latest
steps:
- name: Checkout the latest code
uses: actions/checkout#v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Get the version
id: get_version
run: |
VERSION=$(curl -s https://api.github.com/repos/checkstyle/checkstyle/releases/latest \
| jq ".tag_name")
echo VERSION="$VERSION"
- name: Modify File
run: |
./.ci/bump-version.sh ${{ steps.get_version.VERSION }}
- name: Push commit
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]#users.noreply.github.com'
git commit -am "minor: Bump release version to ${{ steps.get_version.VERSION }}"
git push
- name: GitHub Milestone
run: |
.ci/update-github-milestone.sh
This is my shell script:
#!/bin/bash
set -e
VERSION=$1
echo VERSION="$VERSION"
echo "bump version in pom.xml"
mvn versions:set -DnewVersion="$VERSION" && mvn versions:commit
And these is my tested actions where I want help:
Action link: https://github.com/Rahulkhinchi03/checkstyle/runs/7785606554?check_suite_focus=true
To transfer an output from one step to another, you need to use a workflow command:
echo "::set-output name=VERSION::$VERSION"
and then reference it like this in a later step via the outputs object:
run: |
./.ci/bump-version.sh ${{ steps.get_version.outputs.VERSION }}

Yaml won't validate

For the life of me I can't figure out why this yaml won't validate:
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: Node.js CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
steps:
- uses: actions/checkout#v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v1
with:
node-version: ${{ matrix.node-version }}
- run: >-
git config --global url."https://".insteadOf git://
- run: >-
git config --global url."https://github.com/".insteadOf git#github.com:
- run: npm ci
- run: npm run build --if-present
It gives me a Error : can not read an implicit mapping pair; a colon is missed at line 27 error which doesn't lead to any promising results.
Any help would be greatly appreciated!
YAML parser is picking up the second colon on line 27.
git config --global url."https://".insteadOf git://
You can escape the colon with format.
git config --global url."https://".insteadOf ${{ format('git{0}//', ':') }}

Is it possible to use GitHub secrets inside my shell file?

This is my simple Action on my GitHub repo:
name: CI
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Get /my_account/my_infra
run: |
sudo mkdir /my_account
sudo chmod -R 777 /my_account
cd /my_account
git clone https://github.com/my_account/my_infra
- name: Get /my_account/my_repo
run: |
cd /my_account
git clone https://github.com/my_account/my_repo
- name: Run my build script
run: |
cd /my_account/my_infra
./build.sh /my_account/my_repo
Since GitHub does not provide a way to reuse actions across multiple similar repos, I came up with the idea of creating a base repo, then download that base alongside the current repo, then run a custom shell script from that base repo, passing my current repo as a parameter.
This works perfect. This way I can reuse my base repo across many similar repositories. And I can reuse near 500 lines of build script instead of repeating myself for 50 repositors (which means 25000 lines of CI/CD code).
However, now I need to access some resources (like login into my docker hub account) to pull and push stuff.
Is it possible to use GitHub secrects in my build.sh?
When you set env in your workflow, doc here, they are set as environment variables in your containerised workflow.
This means that if you set a secret in your repository, can be found under settings=> secrets and then assign it to an env in your workflow, they can then be accessed in your build.sh
example:
name: CI
on:
push:
branches: [ main ]
env:
super_secret: ${{ secrets.my_secret }}
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Get /my_account/my_infra
run: |
sudo mkdir /my_account
sudo chmod -R 777 /my_account
cd /my_account
git clone https://github.com/my_account/my_infra
- name: Get /my_account/my_repo
run: |
cd /my_account
git clone https://github.com/my_account/my_repo
- name: Run my build script
run: |
cd /my_account/my_infra
./build.sh /my_account/my_repo
In this case your build.sh can do something like:
#!/bin/bash
npm run build $super_secret
Yes, you just need to assign them to a variable, like
env:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
run: build.sh
Then you can refer to ACCESS_TOKEN variable in the shell script.

GitHub Action unable to execute build executable

Try to use GitHub Actions but getting error when trying to run executable build. I can find executable when doing ls and also changing mod of file executable but didn't got anything. Attached log/output of GitHub action.
Here is my github action workflow file
on:
push:
branches:
- master
name: Build For platforms
jobs:
build:
# We want to define a strategy for our job
strategy:
# this will contain a matrix of all of the combinations
# we wish to test again:
matrix:
go-version: [1.14.x]
platform: [ubuntu-latest]
# Defines the platform for each test run
runs-on: ${{ matrix.platform }}
# the steps that will be run through for each version and platform
# combination
steps:
# sets up go based on the version
- name: Install Go
uses: actions/setup-go#v2
with:
go-version: ${{ matrix.go-version }}
# checks out our code locally so we can work with the files
- name: Checkout code
uses: actions/checkout#v2
- name: Download modules
run: go mod download
- name: Build
run: go build -o executable main.go
- name: Chmod
run: chmod +x executable
- name: List Files
run: ls && pwd
- name: Run
run: ./executable

Resources