Add example workflow

Signed-off-by: divyansh42 <diagrawa@redhat.com>
This commit is contained in:
divyansh42 2021-03-30 22:45:50 +05:30
parent 565d575198
commit 1b6468c894
10 changed files with 68 additions and 196 deletions

View file

@ -24,7 +24,13 @@ jobs:
username: ${{ env.REGISTRY_USER }} username: ${{ env.REGISTRY_USER }}
password: ${{ env.REGISTRY_PASSWORD }} password: ${{ env.REGISTRY_PASSWORD }}
registry: ${{ env.IMAGE_REGISTRY }} registry: ${{ env.IMAGE_REGISTRY }}
logout: true
- name: Verify # - name: Verify
run: | # shell: bash
echo ${XDG_RUNTIME_DIR}/containers/auth.json # run: |
# pwd
# ls $HOME
# cd $HOME
# tree work
# cat ${XDG_RUNTIME_DIR}/containers/auth.json

View file

@ -1,21 +0,0 @@
# buildah-build Changelog
## v2.2.1
- Add note about multi architecture(s) image built support [1f7c249](https://github.com/redhat-actions/buildah-build/commit/1f7c2499306a8def9affb31cc7d43934bb87907d)
## v2.2
- Add output message if tags is not provided [76570bc](https://github.com/redhat-actions/buildah-build/commit/76570bc65b73d4072c85224b6f6e2fef3cf2b24b)
## v2.1
- Add `archs` input to allow building images for custom architectures [803a141](https://github.com/redhat-actions/buildah-build/commit/803a1413e7c2a594cbfb6680bca358bfdbe36745)
## v2
- Rename `tag` input to `tags`, to allow you to build multiple tags of the same image
- Add outputs `image` and `tags`, which output the image name and all tags of the image that was created [88e0085](https://github.com/redhat-actions/buildah-build/commit/88e00855444b8d915b900c8251f48c291ccedce5)
- (Internal) Add CI checks to the action that includes ESlint, bundle verifier and IO checker [20a8e62](https://github.com/redhat-actions/buildah-build/commit/20a8e62ce082870ed0ff1ee141bb98ae95432501)
## v1
- Initial marketplace release
## v0.1
- Initial pre-release

161
README.md
View file

@ -1,161 +0,0 @@
# buildah-build
[![CI checks](https://github.com/redhat-actions/buildah-build/workflows/CI%20checks/badge.svg)](https://github.com/redhat-actions/buildah-build/actions?query=workflow%3A%22CI+checks%22)
[![Build](https://github.com/redhat-actions/buildah-build/workflows/Build/badge.svg)](https://github.com/redhat-actions/buildah-build/actions?query=workflow%3ABuild)
[![Build from dockerfile](https://github.com/redhat-actions/buildah-build/workflows/Build%20from%20dockerfile/badge.svg)](https://github.com/redhat-actions/buildah-build/actions?query=workflow%3A%22Build+from+dockerfile%22)
[![Link checker](https://github.com/redhat-actions/buildah-build/workflows/Link%20checker/badge.svg)](https://github.com/redhat-actions/buildah-build/actions?query=workflow%3A%22Link+checker%22)
<br>
<br>
[![tag badge](https://img.shields.io/github/v/tag/redhat-actions/buildah-build)](https://github.com/redhat-actions/buildah-build/tags)
[![license badge](https://img.shields.io/github/license/redhat-actions/buildah-build)](./LICENSE)
[![size badge](https://img.shields.io/github/size/redhat-actions/buildah-build/dist/index.js)](./dist)
Buildah is a GitHub Action for building Docker and Kubernetes-compatible images quickly and easily.
Buildah only works on Linux. GitHub's [Ubuntu Environments](https://github.com/actions/virtual-environments#available-environments) (`ubuntu-18.04` and newer) come with buildah installed. If you are not using these environments, or if you want to use a different version, you must first [install buildah](https://github.com/containers/buildah/blob/master/install.md).
After building your image, use [push-to-registry](https://github.com/redhat-actions/push-to-registry) to push the image and make it pullable.
<a id="action-inputs"></a>
## Action Inputs
<a id="dockerfile-build-inputs"></a>
### Inputs for build from dockerfile
| Input Name | Description | Default |
| ---------- | ----------- | ------- |
| archs | Architecture(s) to build the image(s) for. For multiple architectures, separate by a comma. Refer to [Multi arch builds](#multi-arch-builds) to setup the `qemu-user-static` dependency. | `amd64`
| build-args | Build arguments to pass to the Docker build using `--build-arg`, if using a Dockerfile that requires ARGs. Use the form `arg_name=arg_value`, and separate arguments with newlines. | None
| context | Path to directory to use as the build context. | `.`
| dockerfiles | The list of Dockerfile paths to perform a build using docker instructions. This is a multiline input to allow multiple Dockerfiles. | **Must be provided**
| image | Name to give to the output image. | **Must be provided**
| layers | Set to true to cache intermediate layers during the build process. | None
| oci | Build the image using the OCI format, instead of the Docker format. By default, this is `false`, because images built using the OCI format have issues when published to Dockerhub. | `false`
| tags | The tags of the image to build. For multiple tags, separate by a space. For example, `latest ${{ github.sha }}` | `latest`
<a id="scratch-build-inputs"></a>
### Inputs for build without dockerfile
| Input Name | Description | Default |
| ---------- | ----------- | ------- |
| archs | Architecture(s) to build the image(s) for. For multiple architectures, separate by a comma. Refer to [Multi arch builds](#multi-arch-builds) to setup the `qemu-user-static` dependency. | `amd64`
| base-image | The base image to use for the container. | **Must be provided**
| content | Paths to files or directories to copy inside the container to create the file image. This is a multiline input to allow you to copy multiple files/directories.| None
| context | Path to directory to use as the build context. | `.`
| entrypoint | The entry point to set for the container. This is a multiline input; split arguments across lines. | None
| envs | The environment variables to be set when running the container. This is a multiline input to add multiple environment variables. | None
| image | Name to give to the output image. | **Must be provided**
| oci | Build the image using the OCI format, instead of the Docker format. By default, this is `false`, because images built using the OCI format have issues when published to Dockerhub. | `false`
| port | The port to expose when running the container. | None
| tags | The tags of the image to build. For multiple tags, separate by a space. For example, `latest ${{ github.sha }}` | `latest`
| workdir | The working directory to use within the container. | None
<a id="outputs"></a>
## Action Outputs
`image`: The name of the built image.<br>
For example, `spring-image`.
`tags`: A list of the tags that were created, separated by spaces.<br>
For example, `latest ${{ github.sha }}`.
<a id="build-types"></a>
## Build Types
You can configure the `buildah` action to build your image using one or more Dockerfiles, or none at all.
<a id="build-using-dockerfile"></a>
### Building using Dockerfiles
If you have been building your images with an existing Dockerfile, `buildah` can reuse your Dockerfile.
In this case the inputs needed are `image` and `dockerfiles`. `tag` is also recommended. If your Dockerfile requires ARGs, these can be passed using `build-arg`.
```yaml
name: Build Image using Dockerfile
on: [push]
jobs:
build:
name: Build image
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Buildah Action
uses: redhat-actions/buildah-build@v2
with:
image: my-new-image
tags: v1 ${{ github.sha }}
dockerfiles: |
./Dockerfile
build-args: |
some_arg=some_value
```
<a id="scratch-build"></a>
### Building without a Dockerfile
Building without a Dockerfile requires additional inputs, that would normally be specified in the Dockerfile.
Do not set `dockerfiles` if you are doing a build from scratch. Otherwise those Dockerfiles will be used, and the inputs below will be ignored.
- An output `image` name and usually a `tag`.
- `base-image`
- In a Dockerfile, this would be the `FROM` directive.
- `content` to copy into the new image
- In a Dockerfile, this would be `COPY` directives.
- `entrypoint` so the container knows what command to run.
- In a Dockerfile, this would be the `ENTRYPOINT`.
- All other optional configuration inputs, such as `port`, `envs`, and `workdir`.
Example of building a Spring Boot Java app image:
```yaml
name: Build Image
on: [push]
jobs:
build-image:
name: Build image without Dockerfile
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: mvn package
- name: Build Image
uses: redhat-actions/buildah-build@v2
with:
base-image: docker.io/fabric8/java-alpine-openjdk11-jre
image: my-new-image
tags: v1
content: |
target/spring-petclinic-2.3.0.BUILD-SNAPSHOT.jar
entrypoint: java -jar spring-petclinic-2.3.0.BUILD-SNAPSHOT.jar
port: 8080
```
<a id="multi-arch-builds"></a>
## Multi arch builds
If building for an architecture other than `amd64`, install `qemu-user-static` using the following command.
```
sudo podman run --rm --privileged multiarch/qemu-user-static --reset -p yes
```
## Using private images
If your build requires a private image, you have to `docker login` in a step before running this action.
For example:
```yaml
- name: Log in to Red Hat Registry
run: echo "${{ secrets.REGISTRY_REDHAT_IO_PASSWORD }}" | docker login registry.redhat.io -u "${{ secrets.REGISTRY_REDHAT_IO_USER }}" --password-stdin
```

View file

@ -14,6 +14,12 @@ inputs:
password: password:
description: 'Password or personal access token used to log against the Docker registry' description: 'Password or personal access token used to log against the Docker registry'
required: false required: false
logout:
description: 'Set to true if you want to logout once the workflows completes'
required: false
default: 'false'
runs: runs:
using: 'node12' using: 'node12'
main: 'dist/index.js' main: 'dist/index.js'
post: 'dist/index.js'

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -5,6 +5,7 @@ export interface ActionInputs {
registry: string; registry: string;
username: string; username: string;
password: string; password: string;
logout: string;
} }
export function getInputs(): ActionInputs { export function getInputs(): ActionInputs {
@ -12,5 +13,6 @@ export function getInputs(): ActionInputs {
registry: core.getInput(Inputs.REGISTRY), registry: core.getInput(Inputs.REGISTRY),
username: core.getInput(Inputs.USERNAME), username: core.getInput(Inputs.USERNAME),
password: core.getInput(Inputs.PASSWORD), password: core.getInput(Inputs.PASSWORD),
logout: core.getInput(Inputs.LOGOUT),
}; };
} }

View file

@ -1,5 +1,11 @@
// This file was auto-generated by action-io-generator. Do not edit by hand! // This file was auto-generated by action-io-generator. Do not edit by hand!
export enum Inputs { export enum Inputs {
/**
* Set to true if you want to logout once the workflows completes
* Required: false
* Default: "false"
*/
LOGOUT = "logout",
/** /**
* Password or personal access token used to log against the Docker registry * Password or personal access token used to log against the Docker registry
* Required: false * Required: false

View file

@ -8,6 +8,7 @@ import * as io from "@actions/io";
import * as os from "os"; import * as os from "os";
import { getInputs } from "./context"; import { getInputs } from "./context";
import { execute } from "./utils"; import { execute } from "./utils";
import * as stateHelper from './state-helper';
let podmanPath: string | undefined; let podmanPath: string | undefined;
@ -25,7 +26,11 @@ async function run(): Promise<void> {
throw new Error("Only supported on linux platform"); throw new Error("Only supported on linux platform");
} }
const { registry, username, password } = getInputs(); const {
registry, username, password, logout,
} = getInputs();
stateHelper.setLogout(logout);
const args = [ const args = [
"login", "login",
@ -35,13 +40,25 @@ async function run(): Promise<void> {
"-p", "-p",
password, password,
]; ];
try {
await execute(await getPodmanPath(), args); await execute(await getPodmanPath(), args);
core.info(`✅ Successfully logged in to ${registry}`); core.info(`✅ Successfully logged in to ${registry}`);
}
catch (err) { // if (logout) {
core.error(`Failed to login to ${registry}`); // await execute(await getPodmanPath(), [ "logout", registry ]);
} // }
} }
run().catch(core.setFailed); async function logout(): Promise<void> {
if (!stateHelper.logout) {
return;
}
await execute(await getPodmanPath(), [ "logout", "quay.io" ]);
}
if (!stateHelper.IsPost) {
run().catch(core.setFailed);;
}
else {
logout().catch(core.setFailed);;
}

17
src/state-helper.ts Normal file
View file

@ -0,0 +1,17 @@
import * as core from '@actions/core';
export const IsPost = !!process.env['STATE_isPost'];
// export const registry = process.env['STATE_registry'] || '';
export const logout = /true/i.test(process.env['STATE_logout'] || '');
// export function setRegistry(registry: string) {
// core.saveState('registry', registry);
// }
export function setLogout(logout: string) {
core.saveState('logout', logout);
}
if (!IsPost) {
core.saveState('isPost', 'true');
}