Giter VIP home page Giter VIP logo

container-action's Introduction

Container Action Template

GitHub Super-Linter CI

Use this template to bootstrap the creation of a container action. ๐Ÿš€

This template includes compilation support, tests, a validation workflow, publishing, and versioning guidance.

If you are new, there's also a simpler introduction in the Hello World Docker Action repository.

If you would like to use the GitHub Actions Toolkit in your container action, see the Container Toolkit Action repository.

Create Your Own Action

To create your own action, you can use this repository as a template! Just follow the below instructions:

  1. Click the Use this template button at the top of the repository
  2. Select Create a new repository
  3. Select an owner and name for your new repository
  4. Click Create repository
  5. Clone your new repository

Important

Make sure to remove or update the CODEOWNERS file! For details on how to use this file, see About code owners.

Initial Setup

After you've cloned the repository to your local machine or codespace, you'll need to perform some initial setup steps before you can develop your action.

Note

You'll need to have a reasonably modern version of Docker handy (e.g. docker engine version 20 or later).

  1. ๐Ÿ› ๏ธ Build the container

    Make sure to replace actions/container-action with an appropriate label for your container.

    docker build -t actions/container-action .
  2. โœ… Test the container

    You can pass individual environment variables using the --env or -e flag.

    $ docker run --env INPUT_WHO_TO_GREET="Mona Lisa Octocat" actions/container-action
    ::notice file=entrypoint.sh,line=7::Hello, Mona Lisa Octocat!

    Or you can pass a file with environment variables using --env-file.

    $ cat ./.env.test
    INPUT_WHO_TO_GREET="Mona Lisa Octocat"
    
    $ docker run --env-file ./.env.test actions/container-action
    ::notice file=entrypoint.sh,line=7::Hello, Mona Lisa Octocat!

Update the Action Metadata

The action.yml file defines metadata about your action, such as input(s) and output(s). For details about this file, see Metadata syntax for GitHub Actions.

When you copy this repository, update action.yml with the name, description, inputs, and outputs for your action.

Update the Action Code

In this template, the container action runs a shell script, entrypoint.sh, when the container is launched. Since you can choose any base Docker image and language you like, you can change this to suite your needs. There are a few main things to remember when writing code for container actions:

  • Inputs are accessed using argument identifiers or environment variables (depending on what you set in your action.yml). For example, the first input to this action, who-to-greet, can be accessed in the entrypoint script using the $INPUT_WHO_TO_GREET environment variable.

    GREETING="Hello, $INPUT_WHO_TO_GREET!"
  • GitHub Actions supports a number of different workflow commands such as creating outputs, setting environment variables, and more. These are accomplished by writing to different GITHUB_* environment variables. For more information, see Workflow commands.

    Scenario Example
    Set environment vars echo "MY_VAR=my-value" >> "$GITHUB_ENV"
    Set outputs echo "greeting=$GREETING" >> "$GITHUB_OUTPUT"
    Prepend to PATH echo "$HOME/.local/bin" >> "$GITHUB_PATH"
    Set pre/post vars echo "MY_VAR=my-value" >> "$GITHUB_STATE"
    Set step summary echo "{markdown}" >> "$GITHUB_STEP_SUMMARY"

    You can write multiline strings using the following syntax:

    {
      echo "JSON_RESPONSE<<EOF"
      curl https://example.com
      echo "EOF"
    } >> "$GITHUB_ENV"
  • Make sure that the script being run is executable!

    git add entrypoint.sh
    git update-index --chmod=+x entrypoint.sh

So, what are you waiting for? Go ahead and start customizing your action!

  1. Create a new branch

    git checkout -b releases/v1
  2. Replace the contents of entrypoint.sh with your action code

  3. Build and test the container

    docker build -t actions/container-action .
    docker run actions/container-action "Mona Lisa Octocat"
  4. Commit your changes

    git add .
    git commit -m "My first action is ready!"
  5. Push them to your repository

    git push -u origin releases/v1
  6. Create a pull request and get feedback on your action

  7. Merge the pull request into the main branch

Your action is now published! ๐Ÿš€

For information about versioning your action, see Versioning in the GitHub Actions toolkit.

Validate the Action

You can now validate the action by referencing it in a workflow file. For example, ci.yml demonstrates how to reference an action in the same repository.

steps:
  - name: Checkout
    id: checkout
    uses: actions/checkout@v3

  - name: Test Local Action
    id: test-action
    uses: ./
    with:
      who-to-greet: Mona Lisa Octocat

  - name: Print Output
    id: output
    run: echo "${{ steps.test-action.outputs.greeting }}"

For example workflow runs, check out the Actions tab! ๐Ÿš€

Usage

After testing, you can create version tag(s) that developers can use to reference different stable versions of your action. For more information, see Versioning in the GitHub Actions toolkit.

To include the action in a workflow in another repository, you can use the uses syntax with the @ symbol to reference a specific branch, tag, or commit hash.

steps:
  - name: Checkout
    id: checkout
    uses: actions/checkout@v3

  - name: Test Local Action
    id: test-action
    uses: actions/container-action@v1 # Commit with the `v1` tag
    with:
      who-to-greet: Mona Lisa Octocat

  - name: Print Output
    id: output
    run: echo "${{ steps.test-action.outputs.greeting }}"

container-action's People

Contributors

chrispat avatar dependabot[bot] avatar hross avatar meihei3 avatar ncalteen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

container-action's Issues

Add documentation to detail docker run command

It is not documented how the run command is used when a Github Action is executed.

In my particular use case, I wanted to figure out how to get files off a container Action. The answer is to send any output files to /github/workspace. It would be helpful to see that somewhere in the documentation.

Having some more information about the rest of the commands would be useful as well I think.

/usr/bin/docker run
    --name <hash> \
    --label <hash> \
    --workdir /github/workspace \
    --rm \
    -e HOME \
    -e GITHUB_REF \
    -e GITHUB_SHA \
    -e GITHUB_REPOSITORY \
    -e GITHUB_ACTOR \
    -e GITHUB_WORKFLOW \
    -e GITHUB_HEAD_REF \
    -e GITHUB_BASE_REF \
    -e GITHUB_EVENT_NAME \
    -e GITHUB_WORKSPACE \
    -e GITHUB_ACTION \
    -e GITHUB_EVENT_PATH \
    -e RUNNER_OS \
    -e RUNNER_TOOL_CACHE \
    -e RUNNER_TEMP \
    -e RUNNER_WORKSPACE \
    -v "/var/run/docker.sock":"/var/run/docker.sock" \
    -v "/home/runner/work/_temp/_github_home":"/github/home" \
    -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" \
    -v "/home/runner/work/sauce/sauce":"/github/workspace" \
    <hash>

Support Windows agents

Hello,

When I try to run a container action or to create my own and invoke a container, it fails saying that it is only supported on Linux agents.

Any ideas when windows will be supported?

I can run docker run just fine from a Windows agent...

Thank you!

Rename default branch

๐Ÿ‘‹ This issue is to track the move over to using main as the default branch for this repo. Weโ€™d love your team's help in completing this transition.

Do not remove your old default branch, customers are going to be using it. We will be sending messages out about these changes, but if you want to message in your repository, that's fine as well.

  • Create a main branch.
  • You might need to rebase any pull requests you want to merge before changing the default branch.
  • Change the default branch in settings to main.
  • Update any documentation in this repo to refer to the new branch name, although using the version tag is still preferred.
  • Check that this Action works correctly for users who have a repository with a custom default branch name.
  • Close this issue and celebrate ๐ŸŽ‰

We are aiming to complete this work by August 28th.

Env vars visible at build time

Hi,

Is it possible to pass environment variables to the docker build command?
I have some variables like CUDA version, library xyz version, ... and would like to access it during the docker build step. With the env or args options, I can access it during the run step, but not while the image is being built.

Installing the whole thing in docker run does not make a big difference on the result, but I'd like to leverage docker cache

Specify container uid and gid

Can it be made possible to specify user and group to be used in the docker run command?

Such as using an options command?

name: 'Container Action Template'
description: 'Get started with Container actions'
author: 'GitHub'
inputs: 
  myInput:
    description: 'Input to use'
    default: 'world'
runs:
  using: 'docker'
  image: 'Dockerfile'
  options: --user 1000:1000
  args:
    - ${{ inputs.myInput }}

My use case is as follows: We have a container where we specify the user (uid=1000,gid=1000) and one of the workflow steps we wish for is to start the container up and send some args to it. If it's successful, we know the PR hasn't broken it.

However when using container-action github is starting the container and mounting workspace, workflow, home etc as uid=1001 and gid=121 and therefore our user (who is non-root) cannot then perform actions in a script.

Thanks,

Matt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.