Informing Sentry about the latest release version in GitHub - github-actions

I am using the getsentry/action-release#v1 GitHub Action to inform Sentry about new releases in my GitHub application. However, since I am using tags as the version number, I would like to inform sentry about the latest tag available on the release page. I'm having issues doing that while using environment variables.
Here is my job:
inform_sentry_about_release:
runs-on: ubuntu-latest
env:
ACCESS_TOKEN: ${{ secrets.GH_PAT }}
steps:
- uses: actions/checkout#v2
- name: Set GITHUB_VERSION variable
run: |
echo 'GITHUB_LATEST_RELEASE=$(curl -H "Authorization: token ${ACCESS_TOKEN}" "https://api.github.com/repos/myusername/myreponame/releases" -s | jq -r ".[0].tag_name")' >> $GITHUB_ENV
- name: Create Sentry release
uses: getsentry/action-release#v1
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
SENTRY_PROJECT: ${{ secrets.SENTRY_PROJECT }}
with:
environment: production
version: ${{ GITHUB_LATEST_RELEASE }}
My Github workflow is failing with the following error:
The workflow is not valid. .github/workflows/production-label.yml (Line: 131, Col: 20): Unrecognized named-value: 'GITHUB_LATEST_RELEASE'. Located at position 1 within expression: GITHUB_LATEST_RELEASE
According to How do I dynamically set an environment variable in a github composite action step?, it appears that I should be able to just echo a key=val to the GITHUB_ENV and then call it in the next step, but no luck.
Line 131 specifically refers to this:
version: ${{ GITHUB_LATEST_RELEASE }}
Is there a different way I'm supposed to access this environment variable from this line? I've tried $GITHUB_LATEST_RELEASE and still no luck with that either.

The environment variable should be accessed in the following way:
version: ${{ env.GITHUB_LATEST_RELEASE }}
Please note, that the names of environment variables are case-sensitive, and you can include punctuation.
Example:
steps:
- name: Set the value
id: step_one
run: |
echo "action_state=yellow" >> $GITHUB_ENV
- name: Use the value
id: step_two
run: |
echo "${{ env.action_state }}" # This will output 'yellow'
For more information check the Setting an environment variable.

Related

How to run a Github Action Task if merging to master or the VERSION file contains letter b

I would like to publish a Python package to pypi if merging to master OR a file named VERSION contains letter b. The VERSION file is located in the root of this repo.
I'm able to get the "merging to master" part work with the following code.
publish:
needs: [build]
runs-on: [self-hosted, generic-linux]
container: python:3
steps:
- name: Download artifacts
uses: actions/download-artifact#v2
with:
name: package
path: ./dist
- name: Install requirements
run: |
pip install --upgrade pip
pip install --upgrade --prefer-binary twine
- name: Upload to artifactory
if: ${{ github.ref == 'refs/heads/master' }}
env:
TWINE_REPOSITORY_URL: https://artifactory.example.com/artifactory/api/pypi/pypi-all
TWINE_REPOSITORY: pypi-all
TWINE_USERNAME: "${{ secrets.PUBLISH_USERNAME }}"
TWINE_PASSWORD: "${{ secrets.PUBLISH_BEARER_TOKEN }}"
run: |
twine upload --skip-existing --verbose dist/*
However, I'm not sure how to add an OR condition to check the content of a file. Could someone help?
Thanks.
you could add an extra step to read the content (manually or using some existing GH action like https://github.com/marketplace/actions/read-files-action) of the file and add a condition the the Upload step, to check if the file contains the required string (with https://docs.github.com/en/actions/learn-github-actions/expressions#contains), like:
- name: Checkout code
uses: actions/checkout#v3
- name: Read Version
id: version
uses: komorebitech/read-files-action#v1.5
with:
files: '["VERSION"]'
- name: Echo Version
run: echo "${{ steps.version.outputs.content }}"
- name: Upload to artifactory
if: ${{ github.ref == 'refs/heads/master' || contains(steps.version.outputs.content, 'b')}}
Remember to checkout the code of the repo before try to read the file

SAM does not find python during deployment via GitHub Action

I'm setting up a Github action that will deploy my Python-based SAM application. However, the "sam deploy" command fails with an error that seems to indicate Python is not available to build the application, despite it having just been set up in the previous step:
Building codeuri: /github/workspace/\[...\]/source runtime: python3.7 metadata: {} architecture: x86_64 functions: \['...'\]
Build Failed
Error: PythonPipBuilder:Resolver - Path resolution for runtime: python3.7 of binary: python was not successful
I can see that the python binary path is being passed as an env variable to the "SAM deployment" step: pythonLocation: /opt/hostedtoolcache/Python/3.7.12/x64
I also see that python is available via the "Check python version" step, where I do a simple "python -V", which returns the expected output.
As commented above, following the official workflow on the SAM AWS documentation resolved the issue.
on:
push:
branches:
- main
jobs:
build-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-python#v2
- uses: aws-actions/setup-sam#v1
- uses: aws-actions/configure-aws-credentials#v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ##region##
# sam build
- run: sam build --use-container

Github workflow does not read variables from environments

Following is my simple github workflow. It is intended to print an environment variable.
name: verify
on:
workflow_dispatch:
jobs:
read_env_variables:
environment: build
runs-on: [ self-hosted, onprem_dae, docker ]
steps:
- name: cat on branch file
run: |
echo ${{ env.SOME_VARIABLE }}
I have created an environment named "build". In this environment, I have an environment variable named SOME_VARIABLE set to xyz.
When the workflow is triggered, I expected to echo value xyz but actual value is "". Is there something missing?
Your issue here is related to the syntax.
To use the ${{ env.SOME_VARIABLE }} syntax, you need to set an env variable at the workflow, job or step level.
Here is an example:
name: Environment Workflow
on:
workflow_dispatch:
env:
WORKFLOW_VARIABLE: WORKFLOW
jobs:
job1:
runs-on: ubuntu-latest
env:
JOB_VARIABLE: JOB
steps:
- name: Run Commands with various variables
if: ${{ env.WORKFLOW_VARIABLE == 'WORKFLOW' }}
env:
STEP_VARIABLE: STEP
run: |
echo "Hello World"
echo "This is the $WORKFLOW_VARIABLE environment variable"
echo "This is the $JOB_VARIABLE environment variable"
echo "This is the $STEP_VARIABLE environment variable"
Now, if you want to use the environment secrets for deployment, as explained here on the Github Documentation, the syntax would be different using the job_id.environment as you are already using following this doc.
Here is an example:
job4:
runs-on: ubuntu-latest
environment: build
steps:
- name: Show repo env secret
run: |
echo ${{ secrets.REPO_ENV_SECRET }}
Note that this variable is a secret, therefore you won't be able to see it through an echo command on the step (it will show ***)
Here is the workflow I used to validate all this implementation if you want to take a look:
workflow yaml file
workflow run

GitHub action to run command and add commit if I type comment

My objective is to get to the point where I can type /run-black as a comment on a pull request in GitHub, and then GitHubActions will run black . on the pull request's branch and add a commit.
The use case is that sometimes casual contributors make a small pull request to my library (e.g. fixing a typo), and I'd like to be able to just write a comment like /run-black to have the black formatter run on their files before I merge.
Use the action Slash Command Dispatch. Add a repo scoped PAT with the name PAT to your secrets and create two workflows with the following definitions.
name: Slash Command Dispatch
on:
issue_comment:
types: [created]
jobs:
slashCommandDispatch:
runs-on: ubuntu-latest
steps:
- name: Slash Command Dispatch
uses: peter-evans/slash-command-dispatch#v2
with:
token: ${{ secrets.PAT }}
issue-type: pull-request
commands: |
run-black
on:
repository_dispatch:
types: [run-black-command]
jobs:
runBlack:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
repository: ${{github.event.client_payload.pull_request.head.repo.full_name}}
ref: ${{github.event.client_payload.pull_request.head.ref}}
token: ${{ secrets.PAT }}
- name: Slash Command Dispatch
run: black .
- run: |
git config --local user.email "41898282+github-actions[bot]#users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git commit -m "Run black" -a
git push

How to Extract Branch Name on Delete Event Github Actions

I am trying to extract the branch name on a delete event. Turns out it's not in the GITHUB_REF object as that will be the default branch.
Ordinarily I would run
- name: Extract branch name
shell: bash
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch
But apparently with delete events I need to extract the branch via ${{ github.event.ref }}
- name: Extract branch name
shell: bash
run: echo "##[set-output name=branch;]$(echo ${{ github.event.ref }})" # how to drop refs/heads/?
id: extract_branch
now I don't know how to drop the refs/heads aspect of the branch name.
EDIT: Since in the delete event case, github.event.ref already contains the simple branch name e.g. feature-1-my-branch and not refs/heads/feature-1-my-branch my example code above works.
In the event I want to do some post-processing on this context in a different event type, where github.event.ref returns refs/heads/feature-1-my-branch how would I drop the refs/heads in that case?
You can just use ${{ github.event.ref }} to reference the branch name, the full delete event payload is documented in the GitHub API docs.
I also did a test myself. With workflow defined in here.
steps:
- uses: actions/checkout#v2
- name: run build
run: |
echo "GITHUB_SHA is ${{ github.sha }}"
echo "GITHUB_REF is ${{ github.ref }}"
echo "${{ github.event.ref }} - ${{ github.event.ref_type }}"
I can trigger a run by pushing and deleting a branch (it would also apply for tag as well). It leads to a run like this.
GITHUB_SHA is feb56d132c8142995b8fea6fd67bdd914e5e0d68
GITHUB_REF is refs/heads/master
so-62779643-test-delete-event-test2 - branch
[update]
For strip out the prefix in GITHUB_REF, here is what I did:
- uses: actions/checkout#v2
- name: run build
run: |
echo "::set-env name=GITHUB_REF::${{ github.ref }}"
echo "old GITHUB_REF is $GITHUB_REF"
GITHUB_REF=$(echo $GITHUB_REF | sed -e "s#refs/heads/##g")
echo "new GITHUB_REF is $GITHUB_REF"
run log reference
old GITHUB_REF is refs/heads/master
new GITHUB_REF is master