Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use variables in "with" block in github actions

I'm struggling a lot with github actions. This seems to work:

      - name: Tag & Push docker image
        run: |
          docker push myrepo/myapp:${GITHUB_SHA::8}

However, this does not:

      - name: create release
        uses: some-custom-action
        with:
          release_version: 1.0.0-${GITHUB_SHA::8}

nor this:

      - name: create release
        uses: some-custom-action
        with:
          release_version: "1.0.0-${{ env.GITHUB_SHA }}"

I'm completely new to github actions and more than a little surprised at the lacking documentation etc.

I simply need to pass a variable into the "with" parameters of a github action.

If anyone is able to help me figure out what I'm doing wrong, I'd be very grateful!

like image 280
Trondh Avatar asked Mar 13 '20 15:03

Trondh


People also ask

How do I use conditions in GitHub Actions?

if conditional to prevent a job from running unless a condition is met. You can use any supported context and expression to create a conditional. When you use expressions in an if conditional, you may omit the expression syntax ( ${{ }} ) because GitHub automatically evaluates the if conditional as an expression.

How do I echo an object in action in GitHub?

You should use run: echo "$GITHUB. REPOSITORY" and run: echo "$GITHUB. REPOSITORY_OWNER" to see them directly on your workflow. Tip: You can identify most of the variables that can be shown with echo through the Github Context using run: echo "$GITHUB_CONTEXT" in your workflow.

What are the different types of environment variables in GitHub actions?

There are mainly three types of environment variables in GitHub Actions. Custom Environment Variables . Default Environment Variables. Encrypted Environment Variables. 1. Custom Environment Variables Custom environment variables can be declared by ourselves inside any workflow file.

How to create encrypted environment variables in GitHub?

In GitHub Actions, we can create encrypted environment variables as well. We can use GitHub Secrets to store API keys and passwords kind of things. Click on the settings in the repository Click on the secrets

What is variable substitution in GitHub?

Variable substitution lets you insert values, including GitHub secrets, into files in your repository during the workflow run. For example, you could insert an API login and password into a JSON file during the workflow run. Variable substitution only works for keys predefined in the object hierarchy.

How to integrate GitHub actions with NodeJS?

On your local machine in the dev environment you write the NodeJS script that calls the API endpoint and write the .env files to the desired GitHub Actions secret variable (say as above into WEBSITE_ENV_STAGE or to both stage and production variables at once); This is pretty wide choice of ways to engage the .env files's variables in the workflow.


1 Answers

When you use a run context, you're invoking a shell. (For macOS and Linux hosts, this is /bin/bash.) So for this step:

- name: Tag & Push docker image
  run: |
    docker push myrepo/myapp:${GITHUB_SHA::8}

you're using a shell and ${GITHUB_SHA::8} will be passed to it literally. The shell will then parse that and interpolate it with its normal parsing rules.

However, when you specify an action to run, instead of a script to execute, you're just invoking a different program. There's no shell, so there's nothing that will parse ${GITHUB_SHA::8}.

You can use ${{ ... }} to reference things in the contexts that are available. For example, there's an env context that is open for you to set key/value pairs and re-use them. (But the env context is not part of a bash shell, so there's no ${{ env.PWD }} for instance.)

There is a mapping, however, between the github context and environment variables that are set when you do run a shell. The ${{ github.sha }} context variable will be set in your shell as the $GITHUB_SHA environment variable.

So in your example, this should work:

- name: create release
  uses: some-custom-action
  with:
    release_version: "1.0.0-${{ github.sha }}"
like image 174
Edward Thomson Avatar answered Sep 24 '22 22:09

Edward Thomson