Skip to content

Commit

Permalink
Add oryx-dockerfile GitHub Action to repository (#483)
Browse files Browse the repository at this point in the history
* Add oryx-dockerfile GitHub Action to repository

* Resolve review feedback
  • Loading branch information
cormacpayne authored Feb 19, 2020
1 parent 4e7d46d commit 9e0468d
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 0 deletions.
5 changes: 5 additions & 0 deletions actions/oryx-dockerfile/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM mcr.microsoft.com/oryx/cli:cli-image-3-patch1

COPY entrypoint.sh /entrypoint.sh

ENTRYPOINT ["sh", "/entrypoint.sh"]
207 changes: 207 additions & 0 deletions actions/oryx-dockerfile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
# GitHub Action for generating a Dockerfile to build and run Azure Web Apps

With the Azure App Service Actions for GitHub, you can automate your workflow to deploy [Azure Web Apps](https://azure.microsoft.com/en-us/services/app-service/web/) using GitHub Actions.

Get started today with a [free Azure account](https://azure.com/free/open-source)!

This repository contains the GitHub Action for [generating a Dockerfile to build and run Azure Web Apps](./action.yml) using the [Oryx](https://github.com/microsoft/Oryx) build system. Currently, the following platforms can be built using this GitHub Action:

- .NET Core
- Node
- PHP
- Python

The generated Dockerfile follows a template similar to the following:

```
ARG RUNTIME=<PLATFORM_NAME>:<PLATFORM_VERSION>
FROM mcr.microsoft.com/oryx/build:<BUILD_TAG> as build
WORKDIR /app
COPY . .
RUN oryx build /app
FROM mcr.microsoft.com/oryx/${RUNTIME}
COPY --from=build /app /app
RUN cd /app && oryx
ENTRYPOINT ["/app/run.sh"]
```

Once this Dockerfile is produced, it can be built and pushed to a registry, such as [Azure Container Registry](https://azure.microsoft.com/en-us/services/container-registry/), and used at a later time to deploy the Azure Web App.

If you are looking for a GitHub Action to build your Azure Web App, consider using [`azure/appservice-build`](https://github.com/Azure/appservice-build).

If you are looking for a GitHub Action to deploy your Azure Web App, consider using [`azure/webapps-deploy`](https://github.com/Azure/webapps-deploy).

The definition of this GitHub Action is in [`action.yml`](./action.yml).

# End-to-End Sample Workflows

## Dependencies on other GitHub Actions

- [`actions/checkout`](https://github.com/actions/checkout)
- Checkout your Git repository content into the GitHub Actions agent

## Other GitHub Actions

- [`azure/login`](https://github.com/Azure/login)
- Authenticate the current workflow to build, test, package, release and deploy to Azure
- [`azure/docker-login`](https://github.com/Azure/docker-login)
- Log in to a private container registry, such as [Azure Container registry](https://azure.microsoft.com/en-us/services/container-registry/)
- [`azure/webapps-container-deploy`](https://github.com/Azure/webapps-container-deploy)
- Deploy a web app container to Azure

### Sample workflow to push an image using Azure CLI

The following is an end-to-end sample of generating the Dockerfile, building the image, and pushing it to Azure Container Registry using Azure CLI whenever a commit is pushed:

```
on: push
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Cloning repository
uses: actions/checkout@v1
- name: Running Oryx to generate a Dockerfile
uses: microsoft/oryx/actions/oryx-dockerfile@master
id: oryx
- name: Azure authentication
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Building image and pushing to ACR
run: |
az acr build -t <IMAGE_NAME>:<TAG> \
-r <ACR_NAME> \
-f {{ steps.oryx.outputs.dockerfile-path }} \
.
```

The following variables should be replaced in your workflow `.yaml` file:

- `<ACR_NAME>`
- Name of the Azure Container Registry that you are pushing to
- `<IMAGE_NAME>`
- Name of the image that will be pushed to your registry
- `<TAG>`
- Name of the image tag

The following variables should be set in the GitHub repository's secrets store:

- `AZURE_CREDENTIALS`
- Used to authenticate calls to Azure; for more information on setting this secret, please see the [`azure/actions/login`](https://github.com/Azure/actions) action

### Sample workflow to push an image using Docker

The following is an end-to-end sample of generating the Dockerfile, building the image, and pushing it to a registry using Docker whenever a commit is pushed:

```
on: push
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Cloning repository
uses: actions/checkout@v1
- name: Running Oryx to generate a Dockerfile
uses: microsoft/oryx/actions/oryx-dockerfile@master
id: oryx
- name: Logging into registry
uses: azure/docker-login@master
with:
login-server: <REGISTRY_NAME>
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
- name: Building image and pushing to registry using Docker
run: |
docker build . -t <REGISTRY_NAME>/<IMAGE_NAME>:<TAG> -f {{ steps.oryx.outputs.dockerfile-path }}
docker push <REGISTRY_NAME>/<IMAGE_NAME>:<TAG>
```

The following variables should be replaced in your workflow:

- `<REGISTRY_NAME>`
- Name of the registry that you are pushing to
- `<IMAGE_NAME>`
- Name of the image that will be pushed to your registry
- `<TAG>`
- Name of the image tag

The following variables should be set in the GitHub repository's secrets store:

- `REGISTRY_USERNAME`
- The username for the container registry; for more information on setting this secret, please see the [`azure/container-actions/docker-login`](https://github.com/Azure/container-actions) action
- `REGISTRY_PASSWORD`
- The password for the container registry; for more information on setting this secret, please see the [`azure/container-actions/docker-login`](https://github.com/Azure/container-actions) action

### Sample workflow to deploy an Azure Web App Container

The following is an end-to-end sample of generating the Dockerfile, building the image, pushing it to a registry using Docker, and deploying the web app to Azure whenever a commit is pushed:

```
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Cloning repository
uses: actions/checkout@v1
- name: Running Oryx to generate a Dockerfile
uses: microsoft/oryx/actions/oryx-dockerfile@master
id: oryx
- name: Logging into Azure
uses: azure/login@master
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Logging into registry
uses: azure/docker-login@master
with:
login-server: <REGISTRY_NAME>
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
- name: Building image and pushing to registry using Docker
run: |
docker build . -t <REGISTRY_NAME>/<IMAGE_NAME>:<TAG> -f {{ steps.oryx.outputs.dockerfile-path }}
docker push <REGISTRY_NAME>/<IMAGE_NAME>:<TAG>
- name: Deploying container web app to Azure
uses: azure/webapps-container-deploy@v1
with:
app-name: <WEB_APP_NAME>
images: <REGISTRY_NAME>/<IMAGE_NAME>:<TAG>
```

The following variables should be replaced in your workflow:

- `<REGISTRY_NAME>`
- Name of the registry that you are pushing to
- `<IMAGE_NAME>`
- Name of the image that will be pushed to your registry
- `<TAG>`
- Name of the image tag
- `<WEB_APP_NAME>`
- Name of the web app that's being deployed

The following variables should be set in the GitHub repository's secrets store:

- `AZURE_CREDENTIALS`
- Used to authenticate calls to Azure; for more information on setting this secret, please see the [`azure/actions/login`](https://github.com/Azure/actions) action
- `REGISTRY_USERNAME`
- The username for the container registry; for more information on setting this secret, please see the [`azure/container-actions/docker-login`](https://github.com/Azure/container-actions) action
- `REGISTRY_PASSWORD`
- The password for the container registry; for more information on setting this secret, please see the [`azure/container-actions/docker-login`](https://github.com/Azure/container-actions) action
25 changes: 25 additions & 0 deletions actions/oryx-dockerfile/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: 'Oryx Dockerfile Action'
description: 'Generate a Dockerfile that builds and runs an Azure Web App on Linux.'
branding:
icon: 'login.svg'
color: 'blue'
inputs:
source-directory:
description: 'Relative path (within the repository) to the source directory of the project you want to create a Dockerfile for; if no value is provided for this, the root of the repository ("GITHUB_WORKSPACE" environment variable) will be used.'
required: false
platform:
description: 'Programming platform of the web app; if no value is provided, Oryx will determine the platform to build with. The supported values are "dotnet", "nodejs", "php" and "python".'
required: false
platform-version:
description: 'Version of the programming platform; if no value is provided, Oryx will determine the version needed to build the repository.'
required: false
outputs:
dockerfile-path:
description: 'The path of the Dockerfile that was generated by Oryx, used to build and run a web app.'
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.source-directory }}
- ${{ inputs.platform }}
- ${{ inputs.platform-version }}
60 changes: 60 additions & 0 deletions actions/oryx-dockerfile/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/sh -l

sourceDirectory=$1
platform=$2
platformVersion=$3
dockerfilePath="Dockerfile.oryx"


echo

if [ -n "${sourceDirectory}" ]
then
dockerfilePath="$sourceDirectory/$dockerfilePath"
sourceDirectory="$PWD/$sourceDirectory"
echo "Relative path to source directory provided -- the following directory will be built: '${sourceDirectory}'"
else
sourceDirectory=$PWD
echo "No source directory provided -- the root of the repository ('GITHUB_WORKSPACE' environment variable) will be built: '${sourceDirectory}'"
fi

oryxCommand="oryx dockerfile ${sourceDirectory} --output ${dockerfilePath}"

echo
echo "Dockerfile will be written to the following file: '${dockerfilePath}'"
echo

if [ -n "${platform}" ]
then
echo "Platform provided: '${platform}'"
oryxCommand="${oryxCommand} --platform ${platform}"
else
echo "No platform provided -- Oryx will enumerate the source directory to determine the platform."
fi

echo

if [ -n "${platformVersion}" ]
then
echo "Platform version provided: '${platformVersion}'"
oryxCommand="${oryxCommand} --platform-version ${platformVersion}"
else
echo "No platform version provided -- Oryx will determine the version."
fi

echo
echo "Running command '${oryxCommand}'"
echo
eval $oryxCommand

if [ -f "$dockerfilePath" ];
then
echo "Dockerfile generation succeeded; the following is the content of the Dockerfile:"
cat $dockerfilePath
else
echo "Dockerfile generation failed."
exit 1
fi

echo
echo ::set-output name=dockerfile-path::$dockerfilePath

0 comments on commit 9e0468d

Please sign in to comment.