KalexWonder / github-actions-for-packages

https://lab.github.com/githubtraining/github-actions:-publish-to-github-packages
MIT License
0 stars 0 forks source link

Docker Workflow #2

Closed github-learning-lab[bot] closed 4 years ago

github-learning-lab[bot] commented 4 years ago

Docker 🐳

What is Docker?

Docker is an engine that allows you to run containers. Containers have many advantages, including:


Docker vs Virtual Machines

visualization comparing containers to virtual machines

Container Virtual Machine
Application layer abstraction Physical layer abstraction
Consume less space than VMs Include a full copy of the operating system
Fast to startup Boot up slowly
Shared OS kernel allows many containers to run on a single host Multiple virtual machines run on one server at a greater resource cost per guest machine

Dockerfiles, Images, and Container

Before moving forward with the workflow file, let's spend some time on these concepts. There are important differences between Dockerfiles, Images, and Containers.

Dockerfile Docker Image Docker Container
Text document that contains all the commands and instructions necessary to build a Docker Image. Executable packages comprised of code, dependancies, libraries, a runtime, environment variable, and configuration files. Very similar to a virtual machine snapshot. A runtime instance of a Docker Image. This is what the image becomes when executed in memory.

What about our workflow?

Our repository contains a Dockerfile, source code, and tests for the Tic Tac Toe application.

Our CI Workflow allows us to make code changes. Those changes will trigger an automated build and automated test. But, the automation does not create a deployable artifact.

We will place our application into a Docker container. Then, we will have a deployable package. A deployable package enables CD.

Because a Dockerfile is a text file, we are able to version it as source code. This configuration as code allowing us a single point of truth for our application.

As you learned above, we need turn that Dockerfile into a Docker image if we want to create a runtime instance. We are going to store that image in GitHub Packages.


📖Read More about Docker.

github-learning-lab[bot] commented 4 years ago

Edit the workflow file

Step 2: Edit the workflow file

We are going to edit the current workflow file in our repository by adding adding a job that turns our Dockerfile into a Docker Image.

:keyboard: Activity: Edit the workflow file and turn the Dockerfile into a Docker Image

  1. Edit the current workflow file in our repository
  2. Rename ci-workflow.yml to cd-workflow.yml:
  3. On line 1, change the name from Node CI to Docker CD
        name: Docker CD
  4. Add the following job to your workflow file:
  Build-and-Push-Docker-Image:
    runs-on: ubuntu-latest
    needs: test
    name: Docker Build, Tag, Push

    steps:
    - name: Checkout
      uses: actions/checkout@v1
    - name: Download built artifact
      uses: actions/download-artifact@master
      with:
        name: webpack artifacts
        path: public
    - name: Build, Tag, Push
      uses: mattdavis0351/actions/docker-gpr@v1.3.0
      with:
        repo-token: ${{ secrets.GITHUB_TOKEN }}
        image-name: tic-tac-toe
  1. Commit the newly edited workflow file to your repository
Complete cd-workflow.yml file... ```yaml name: Docker CD on: push: # branches-ignore: # - "ci-workflow" # - "docker-workflow" paths: - "**Dockerfile**" jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - name: npm install and build webpack run: | npm install npm run build - uses: actions/upload-artifact@master with: name: webpack artifacts path: public/ test: runs-on: ubuntu-latest needs: build strategy: matrix: os: [ubuntu-lastest, windows-2016] node-version: [10, 12] steps: - uses: actions/checkout@v1 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - uses: actions/download-artifact@master with: name: webpack artifacts path: public - name: npm install, and test run: | npm install npm test env: CI: true Build-and-Push-Docker-Image: runs-on: ubuntu-latest needs: test name: Docker Build, Tag, Push steps: - name: Checkout uses: actions/checkout@v1 - name: Download built artifact uses: actions/download-artifact@master with: name: webpack artifacts path: public - name: Build, Tag, Push uses: mattdavis0351/actions/docker-gpr@v1.3.0 with: repo-token: ${{ secrets.GITHUB_TOKEN }} image-name: tic-tac-toe ```
github-learning-lab[bot] commented 4 years ago

Great Job 👍

I have merged this pull request for you, and opened a new one for you to start working on the CD segment of our workflow.

Navigate to the next pull request to continue this course.