How to cache java installation using github actions pipeline - github

I am installing JAVA with actions pipeline in github. To make faster the execution, I want to cache the java for next pipeline. The version I wanna use is 17 for JAVA. During first execution of pipeline, the specific version of java is installed correctly. When pipeline executed for 2nd time, the cached java is picked(Which I want too as well) but the it showing me the version 1.8 not 17. Can anyone help. Thanks
name: pipeline to install & cache java
on:
push:
branches:
- do/cs # or the name of your main branch
# manually run from the Actions tab
workflow_dispatch:
jobs:
build:
name: Install packeges
runs-on: windows-2019 # Win 10 IoT is not available on github actions
steps:
- name: Cache Java
id: cache-java
uses: actions/cache#v3
with:
path: 'c:\hostedtoolcache\windows\Java_Microsoft_jdk\17.0.3\x64'
key: ${{ runner.os }}-jdk-17.0.3
restore-keys: ${{ runner.os }}-jdk-17.0.3
- name: Install Java
if: ${{steps.cache-java.outputs.cache-hit != 'true'}} # condition to check if old installation is available in cache
uses: actions/setup-java#v3
with:
distribution: 'microsoft'
java-version: '17'
architecture: 'x64'
- name: Verify Java installation
run: |
echo "java version:"
java -version

Related

Working directory is not working in my Github action

I'm starting to use Github actions. When I ran the process in my Ubuntu server, one of the config options was to select the work folder, by default, I leave _work. But previously, I had my repository in another folder.
So, I'm trying to add a specific value for working-directory to my yml, file but is not using the value in the build process.
YML File:
# This workflow will do a clean installation of node dependencies, cache/restore them, 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: [ "master" ]
pull_request:
branches: [ "master" ]
defaults:
run:
working-directory: /../../../../var/www/mysite.com
jobs:
deployment:
runs-on: self-hosted
steps:
- name: Checkout
uses: actions/checkout#v3
- name: Use Node.js
uses: actions/setup-node#v3
with:
node-version: '14.x'
- name: Install dependencies
run: yarn
- name: Build
run: yarn build
- name: Restart server application
run: pm2 restart pm2_script.json
The process ran ok, but is doing all the process (checkout, build, etc) inside _work and not inside working-directory
What I'm doing wrong?
Thanks!!

EACCES: permission denied, unlink '/home/runner/work/gos_front/gos_front/node_modules/.yarn-integrity

I have this workflow.yaml for github actions:
name: CI/CD
on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14]
steps:
- name: Checkout repository
uses: actions/checkout#v2
- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v2
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
uses: borales/actions-yarn#v2.0.0
with:
cmd: install --ignore-engines
- name: Run linters
uses: borales/actions-yarn#v2.0.0
with:
cmd: lint:prettier
- name: Run cypress
uses: cypress-io/github-action#v2
with:
build: npm run build
start: npm start
- name: Build
uses: borales/actions-yarn#v2.0.0
with:
cmd: build
Then it runs, on run cypress occurs an error:
error An unexpected error occurred: "EACCES: permission denied, unlink
'/home/runner/work/gos_front/gos_front/node_modules/.yarn-integrity'"
.
Before that I add cypress run all was right. I tried to add sudo but it not helped. I use yarn.
cypress-io/github-action tries to install dependencies as well and runs into an access issue. The problem is the way your node_modules folder is created.
I ran into this when using Docker, mapping node_modules from inside the docker image to the outside. This is a known issue.
You use borales/actions-yarn. This is an outdated package, that probably does something similar. You should instead use yarn directly. From their readme:
Please keep in mind that this Action was originally written for GitHub Actions beta (when Docker was the only way of doing things). Consider using actions/setup-node to work with Yarn. This repository will be mostly supporting the existing flows.

Why can't GitHub Action find command of library that I just installed?

I'm trying to set up a GitHub Action for license checking, and I want to use this tool (https://github.com/marketplace/actions/licensed-ci).
I simply want to run, and scan for any obvious licensing errors. They also have a setup-tool that I decide on trying to use (https://github.com/jonabc/setup-licensed). Here, there is this example of usage:
steps:
- uses: actions/checkout#master
- uses: jonabc/setup-licensed#v1
with:
version: '2.x' # required: must satisfy semver.validRange
install-dir: /path/to/install/at # optional: defaults to /usr/local/bin
github_token: # optional: allows users to make authenticated requests to GitHub's APIs
- run: npm install # install dependencies in local environment
- run: licensed list
I try to implement this, but have no GitHub token, and don't know what the installation path is for, so I leave those two out in my version.
My workflow looks like this:
name: "Update and check dependency data"
on:
push:
branches: [main]
jobs:
licensed:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- uses: jonabc/setup-licensed#v1
with:
version: 2.x
- run: licensed list
This fails on "run licensed list" with this:
`find_config': Licensed configuration not found in /home/runner/work/malware/malware (Licensed::Configuration::LoadError)
I have no idea what this error means, should I somehow add a config file somewhere?
Here are the files located in the repository root directory:
-go.mod
-go.sum
-LICENSE
-README.md
-sss.md

How to replace a API key with a Secret in code during Github Actions Job?

I usually hide my API key in an XML file and use .gitignore along with getString() to retrieve my API key.
I'm trying to use Github Actions to automate Gradle build and Release of debug apk's. Due to the XML file not being uploaded to the repo, it obviously fails.
Is there any way to store my key in Github Secrets and then replace the code with the secret?
Current code: headers["token"]= getString(R.string.token)
Replaced code in Github actions server : headers["token"]="MY_API_KEY_FROM_SECRETS"
Here's my YAML file that I was using:
name: Gradle build test and release
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: set up JDK 1.8
uses: actions/setup-java#v1
with:
java-version: 1.8
#I'd like to replace the api key here in code from secrets
- name: Make Gradle executable
run: chmod +x ./gradlew
- name: Build with Gradle
run: ./gradlew build
- name: Build Debug APK
run: ./gradlew assembleDebug
- name: Releasing using Hub
uses: ShaunLWM/action-release-debugapk#master
env:
GITHUB_TOKEN: ${{ secrets.TOKEN }}
APP_FOLDER: app
RELEASE_TITLE: New Build
BODY: github.event.head_commit.message
prerelease: true
Sure, this is possible. You haven't said what file you'd like modified, but this should be easy enough to do with a one-liner (replacing FILENAME with your config file name):
- run: perl -pi -e 's/getString\(R\.string\.token\)/"$ENV{TOKEN}"/' FILENAME
env:
TOKEN: ${{ secrets.TOKEN }}
If you prefer Ruby or some other scripting language, you can use that instead. You could also use sed, but I prefer this approach because it means that the value is never available in ps output (even if this is a locked down VM).

Saving cache on job failure in GitHub Actions

I am using the GitHub cache action, but I noticed that the no cache will be created if the job fails. From the docs:
If the job completes successfully, the action creates a new cache with the contents of the path directory.
A stripped down version of my workflow YAML file:
name: Build
on: [push]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v1
- name: Setup Node.js
uses: actions/setup-node#master
with:
node-version: '10.x'
- name: Get yarn cache path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Restore yarn cache
uses: actions/cache#v1
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install yarn dependencies
run: yarn install
- name: Build
run: yarn build
I noticed that if my Build step fails the cache post-step will be skipped unnecessarily, which causes the installed dependencies to not be cached. This requires subsequent runs to download dependencies again, slowing down the job.
Is there a way to always cache the dependencies, even when the build step fails?
In the official version of the action, no it's not possible to cache dependencies if the build fails. See this line in the cache action's manifest:
runs:
using: 'node12'
main: 'dist/restore/index.js'
post: 'dist/save/index.js'
post-if: 'success()'
It will only run the post-step if the job succeeds. I don't know the original reasoning for this, but there are a few issues opened around this idea. In this issue a user forked the action to change the post-if to always(), which may be what you're after, assuming you're willing to run an unofficial action.