File reference with Github Actions - json

I'm trying to deploy a file via bash to an external endpoint using github actions.
This file is located up two directories from where the action exists. Locally I can get to it using the path ../../src/indexer/templates/library.json. I've tried using the workspace as a root but the file is never actually set to the json variable. Current step:
- name: Deploy template to cluster
run: |
json="<${{GITHUB_WORKSPACE}}/src/indexer/templates/library.json"
echo "deploying template to cluster ${{ inputs.environment }}"
curl -X PUT \
-H 'Content-Type: application/json' \
-u "${{ steps.secrets.outputs.USERNAME }}:${{ steps.secrets.outputs.PASSWORD }}" \
-d '$json' \
"${{ secrets.CLUSTER_URL }}";
Is there some syntax I'm missing here, or is there a better way to do this in a separate step?

try the working-directory keyword to change to the directory before running your command
https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions
- name: Clean temp directory
run: rm -rf *
working-directory: ./temp

Related

Github Actions workflow will not trigger

I am trying to trigger a workflow every time a commit is pushed to the repo. This little workflow adds a label to a jira ticket every time a commit is pushed. Whenever i would commit this workflow file and add a commit message, it would trigger the workflow but when i try it with other files in the repo it does absolutely nothing and I don't have a clue why. When i would also make changes to other workflow files it would also trigger this specific workflow.
I'm new to github actions so any help would be appreciated. Below is my full workflow. I have also tried with just "on: push:".
name: Update Jira with branch name
on:
push:
branches:
- '*'
jobs:
update_jira:
runs-on: ubuntu-latest
steps:
- name: Get branch name
run: echo "Branch name:${GITHUB_REF#refs/heads/}"
- name: Login
uses: atlassian/gajira-login#master
env:
JIRA_BASE_URL: x
JIRA_USER_EMAIL: y
JIRA_API_TOKEN: z
- name: Find in commit messages
uses: atlassian/gajira-find-issue-key#v3
with:
from: commits
- name: Update Jira with branch name
run: |
JIRA_URL=x
JIRA_USERNAME=y
JIRA_TOKEN=z
curl -X PUT -H "Authorization: Basic $(echo -n "$JIRA_USERNAME:$JIRA_TOKEN" | base64)" -H "Content-Type: application/json" -d "{\"fields\":{\"labels\":[\"${GITHUB_REF#refs/heads/}\"]}}" "$JIRA_URL/rest/api/latest/issue/$(cat /home/runner/jira/config.yml | grep "issue:" | awk '{print $2}')"
# Get the response code
response_code=$(curl -X PUT -H "Authorization: Basic $(echo -n "$JIRA_USERNAME:$JIRA_TOKEN" | base64)" -H "Content-Type: application/json" -d "{\"fields\":{\"labels\":[\"${GITHUB_REF#refs/heads/}\"]}}" "$JIRA_URL/rest/api/latest/issue/$(cat /home/runner/jira/config.yml | grep "issue:" | awk '{print $2}')" -s -o /dev/null -w "%{http_code}")
# Print the response code
echo "Response code: $response_code"
The workflow only runs with this 'on: push' setup if the workflow is stored on that branch. Check if the workflow exists on those other branches as well, and add it if missing.

How to store an AWS CLI query result to a GitHub environment variable to be used in the same workflow?

I'm trying to query AWS to get a certain CloudFront Distribution Id and store it in a GitHub Actions environment variable so I could us it to invalidate that CloudFront Distribution. This is what I've tried so far:
name: Store CloudFront Distribution Id in env variable
env:
DIST_ID: ""
run: |
aws cloudfront list-distributions --query "DistributionList.Items[*].{id:Id,origin:Origins.Items[0].Id}[?origin=='$AWS_S3_BUCKET'].id" --output text > "$DIST_ID"
Any ideas on how I can accomplish this? Thanks
If you want to set an environment variable from a script, it's easiest to append it to the special file behind $GITHUB_ENV as described in the docs:
echo "{environment_variable_name}={value}" >> $GITHUB_ENV
In your case, this would mean:
steps:
- run: |
DIST_ID=$(aws cloudfront list-distributions --query "DistributionList.Items[*].{id:Id,origin:Origins.Items[0].Id}[?origin=='$AWS_S3_BUCKET'].id" --output text)
echo "DIST_ID=$DIST_ID" >> $GITHUB_ENV
- run: echo ${{ env.DIST_ID }}

Github dispatches workflow Invalid request

I'm trying to trigger a workflow event in Github.
for some reason, I'm able to GET information about my organization repository workflow but can not use '/dispatches'
Work is based on: https://docs.github.com/en/rest/actions/workflows#create-a-workflow-dispatch-event
Here is the curl code:
curl -X POST \
-H "Accept:application/vnd.github.v3+json" \
-H 'Authorization:token ${{ github.token }}' \
'https://api.github.com/repos/[owner/org]/[repo]/actions/workflows/9999999/dispatches' \
-d '{"event_type":"semantic-release"}'
Getting error:
422 Unprocessable Entity
"message": "Invalid request.\n\nFor 'links/0/schema', nil is not an object.",
"documentation_url": "https://docs.github.com/rest/reference/repos#create-a-repository-dispatch-event"
Am I missing some basic information for this to work and trigger an event?
Instead of trying to call the GitHub API directly, try and use the GitHub CLI gh (that you can install first to test locally).
You can also use GitHub CLI in workflows.
GitHub CLI is preinstalled on all GitHub-hosted runners.
For each step that uses GitHub CLI, you must set an environment variable called GITHUB_TOKEN to a token with the required scopes
It has a gh workflow run, which does create a workflow_dispatch event for a given workflow.
Authenticates first (gh auth login, if you are doing a local test):
# authenticate against github.com by reading the token from a file
$ gh auth login --with-token < mytoken.txt
Examples:
# Run the workflow file 'triage.yml' at the remote's default branch
$ gh workflow run triage.yml
# Run the workflow file 'triage.yml' at a specified ref
$ gh workflow run triage.yml --ref my-branch
# Run the workflow file 'triage.yml' with command line inputs
$ gh workflow run triage.yml -f name=scully -f greeting=hello
# Run the workflow file 'triage.yml' with JSON via standard input
$ echo '{"name":"scully", "greeting":"hello"}' | gh workflow run triage.yml --json
In your case (GitHub Action):
jobs:
push:
runs-on: ubuntu-latest
steps:
- run: gh workflow run triage.yml
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
As explained by hanayama in the comments:
Found out the secrets. GITHUB_TOKEN doesn't work, even with permissions edited for the entire workflow.
Using a personal access token worked.

GitHub Action: How to get value from expression evaluation and assign it to the environment variable

An environment expression is often assigned directly like the example below
- name: set up env var
env:
TAG: v1.2.3
run: echo $TAG
But how can I get the value from shell script evaluation? For example in my terminal I can get the current Tag by git describe --exact-match --tags $(git log -n1 --pretty='%h')
but when I try to put this script into the env as follow
- name: set up env var
env:
TAG: $(git describe --exact-match --tags $(git log -n1 --pretty='%h'))
run: echo $TAG
the echo printed out $(git describe --exact-match --tags $(git log -n1 --pretty='%h')) which means it is not evaluated but treated as a string.
How can I get the value of git describe --exact-match --tags $(git log -n1 --pretty='%h') and assign it to the environment variable TAG ?
You can add variables to the GITHUB_ENV by using: echo "{name}={value}" >> $GITHUB_ENV
This allows to create or update an environment variable for any actions running next in a job. The action that creates or updates the environment variable does not have access to the new value, but all subsequent actions in a job will have access. Environment variables are case-sensitive and you can include punctuation.
Source
So your workflow could for example look like this:
- name: set up env var
run: echo "TAG=$(echo git describe --exact-match --tags $(git log -n1 --pretty='%h'))" >> $GITHUB_ENV
- name: use env var
run: echo ${{ env.TAG }}

Gitlab CI variable before script with curl raises YAML syntax error

I am defining a variable JWT, I will store in it a token which I will use later inside the code.
I'm going to obtain it at the before_script step through a curl call.
The problem is that when I try to run the pipeline, it fails with the error:
Found errors in your .gitlab-ci.yml: Included file .gitlab-ci.yml
does not have valid YAML syntax!
I have already read this Stack Overflow answer in order to properly interpolate USER and PASS environment variables.
This is my .gitlab-ci.yml file:
build-dist:
environment:
name: develop
variables:
JWT: ""
stage: build
image: node:16-alpine
cache:
key:
files:
- yarn.lock
paths:
- node_modules
- .yarn
before_script:
- if [ -z "$USER" ] || [ -z "$PASS" ]; then exit 1; fi
- apk add curl
- JWT=$(curl -s -X POST -H "Content-Type: application/json" -d '{"username": "'"$USER"'","password": "'"$PASS"'"}' "https://example.com/token")
script:
- yarn install --pure-lockfile --cache-folder .yarn
- yarn build
How should I correct the follow line inside my .gitlab-ci.yml in order to make it work?
- JWT=$(curl -s -X POST -H "Content-Type: application/json" -d '{"username": "'"$USER"'","password": "'"$PASS"'"}' "https://example.com/token")
First, a handy tip for such GitLab CI issues:
assuming your have a GitLab repo on https://gitlab.com/user/project,
you can browse the page https://gitlab.com/user/project/-/ci/lint
then paste the contents of the offending .gitlab-ci.yml file, and click on "Validate" to get more feedback (typically, the error line number, etc.)
Regarding the YAML snippet at stake, the crux of the issue is the - JWT=$(…) line as you mentioned in the question, more precisely:
from a YAML point of view, the string JWT=… is not explicitly quoted,
and as this text contains a :,
the YAML parser then sees it as a map, i.e., as if you had written:
- username: "NAME"
other_key: "val"
To solve this, it appears you'd just need to "quote" this sequence item with ' or " and thus write a line of the form - "JWT=…", then escape the quotes accordingly inside… but I'd rather suggest (to avoid ugly escapes!) to rely on the so-called block style of YAML → this leads to:
- |
JWT=$(curl -s -X POST -H "Content-Type: application/json" -d '{"username": "'"$USER"'","password": "'"$PASS"'"}' "https://example.com/token")