From 975a73bf893c8803e63efebf5f3fd9476b24c6ad Mon Sep 17 00:00:00 2001 From: Nicholas Cote <131394540+NicholasCote@users.noreply.github.com> Date: Fri, 2 Aug 2024 16:52:28 -0600 Subject: [PATCH] Add workflow --- .github/workflows/webapp-build-test.yaml | 35 +++++++++++ August-08-2024/walkthrough.ipynb | 78 +++++++++++++++++++++--- 2 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/webapp-build-test.yaml diff --git a/.github/workflows/webapp-build-test.yaml b/.github/workflows/webapp-build-test.yaml new file mode 100644 index 0000000..7338ae0 --- /dev/null +++ b/.github/workflows/webapp-build-test.yaml @@ -0,0 +1,35 @@ +# Name the Action +name: Automation to build a container image +# Define when the Action is run. This example is run when there is a push to the flask-app/ directory on the app branch. +on: + push: + paths: + - August-08-2024/** + branches: + - main +# Define the jobs to run. A job can have multiple steps and an Action can contain multiple jobs. +jobs: + build-image: + # Use the latest ubuntu image to run the jobs + runs-on: ubuntu-latest + steps: + # Step 1 is to checkout the github repo used to build the Dockerfile + - name: Check out the repo + uses: actions/checkout@v4 + # Get the date and send to GITHUB_OUTPUT to apply as image tag + - name: Get current date + id: date + run: echo "date=$(date +'%Y-%m-%d.%H')" >> $GITHUB_OUTPUT + # Build the docker image + - name: Build Docker image + uses: docker/build-push-action@v4 + with: + # Provide the current directory as build context + context: August-08-2024/. + # Specify where the Dockerfile is located in relation to the repo base path + file: Containerfile + # Enable the push to docker hub + push: false + # Provide the tags to apply to the image, this example uses the latest image tag + tags: | + ncote/workshop-webapp:${{ steps.date.outputs.date }} \ No newline at end of file diff --git a/August-08-2024/walkthrough.ipynb b/August-08-2024/walkthrough.ipynb index eae9fe8..9a16363 100644 --- a/August-08-2024/walkthrough.ipynb +++ b/August-08-2024/walkthrough.ipynb @@ -116,30 +116,92 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "A [GitHub Actions](https://docs.github.com/en/actions) workflow can be created to build a container when changes are made to a repository. This job can be run on GitHub provided machines, called runners, or self-hosted runners that can be run on a users machine or a central compute system like the CISL On-premise Cloud Pilot. YAML files define each workflow and are stored at the base of the repository in a directory named `.github/workflows/`." + "A [GitHub Actions](https://docs.github.com/en/actions) workflow is defined as a YAML file and can be created to build a container when changes are made to a repository. This job can be run on GitHub-provided machines, called runners, or self-hosted runners that can be run on a users machine or a central compute system like the CISL On-premise Cloud Pilot. YAML files define each workflow and are stored at the repository's base in a directory named `.github/workflows/`. On the left side bar in github.dev add a new folder named .github to the repository base by either right-clicking and selecting New Folder or clicking the new folder icon at the top of the sidebar. Inside the new .github folder add a new folder named workflows. This new location will be where we define how to run specific GitHub Actions. " ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "!mkdir -p .github/workflows/" + "#### GitHub provided runner example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### GitHub provided runner example" + "##### Build a container image" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Sometimes it is advantageous to build an image, but not push it anywhere. For instance, you may want to test that the changes made don't affect the image build and it's still able to build completely without errors. We are going to start with this example before moving on to a workflow that pushes an image. Inside the `.github/workflows/` directory create a new file where we can define the GitHub Action. Use a descriptive name like `webapp-build-test.yaml` when creating the new file. The contents to place inside the file can be found below with explanations of each line, or group of lines, included as comments:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```yaml\n", + "# Name the Action\n", + "name: Automation to build a container image\n", + "# Define when the Action is run. This example is run when there is a push to the flask-app/ directory on the app branch.\n", + "on:\n", + " push:\n", + " paths:\n", + " - August-08-2024/**\n", + " branches:\n", + " - main\n", + "# Define the jobs to run. A job can have multiple steps and an Action can contain multiple jobs. \n", + "jobs:\n", + " build-image:\n", + " # Use the latest ubuntu image to run the jobs\n", + " runs-on: ubuntu-latest\n", + " steps:\n", + " # Step 1 is to checkout the github repo used to build the Dockerfile\n", + " - name: Check out the repo\n", + " uses: actions/checkout@v4\n", + " # Get the date and send to GITHUB_OUTPUT to apply as image tag\n", + " - name: Get current date\n", + " id: date\n", + " run: echo \"date=$(date +'%Y-%m-%d.%H')\" >> $GITHUB_OUTPUT\n", + " # Build the docker image\n", + " - name: Build Docker image\n", + " uses: docker/build-push-action@v4\n", + " with:\n", + " # Provide the current directory as build context \n", + " context: August-08-2024/.\n", + " # Specify where the Dockerfile is located in relation to the repo base path\n", + " file: Containerfile\n", + " # Enable the push to docker hub\n", + " push: false\n", + " # Provide the tags to apply to the image, this example uses the latest image tag \n", + " tags: |\n", + " ncote/workshop-webapp:${{ steps.date.outputs.date }}\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Push the changes, the new workflow action file, by using the branch on the left side of your browser window. It should have a circle with a 1 inside it to show we have 1 change that can be committed and pushed. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### Build and push a container image" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "The following example uses a GitHub provided machine running Ubuntu with predefined Actions built by Docker to run certain jobs. The image created is pushed to [Docker Hub](https://hub.docker.com/) and uses credentials stored as repository [Actions secrets](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions) to login. Explanations of each line, or group of lines, are provided as comments in the example seen below:" + "Once we know the image is building correctly we can go ahead and push that image to a container registry so it can be shared with others or reused. The image created in the example is pushed to [Docker Hub](https://hub.docker.com/) and uses credentials stored as repository [Actions secrets](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions) to log in. If we go back to the code repository in our browser we can open up the repository settings by using the far right option on the top navigation bar. Once in the repo security settings we can look at the left navigation menu that is opened and under Security there is a dropdown for Secrets and variables that contains a link to Actions. Once we have the Actions repository secrets open we can add our Docker Hub information based on the variables used in the workflow file, DOCKERHUB_USERNAME & DOCKERHUB_TOKEN. Copy and paste the contents below into our existing GitHub Actions workflow file and overwrite everything that was in there before. " ] }, { @@ -153,7 +215,7 @@ "on:\n", " push:\n", " paths:\n", - " - web-app/**\n", + " - August-08-2024/**\n", " branches:\n", " - main\n", "# Define the jobs to run. A job can have multiple steps and an Action can contain multiple jobs. \n",