marcotterra / github-actions-for-packages

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

Docker Workflow #2

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

github-learning-lab[bot] commented 3 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 variables, 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 to 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 3 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 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 yaml 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@main
      with:
        name: webpack artifacts
        path: public
    - name: Build container image
      uses: docker/build-push-action@v1
      with:
        username: ${{github.actor}}
        password: ${{secrets.GITHUB_TOKEN}}
        registry: docker.pkg.github.com
        repository: marcotterra/github-actions-for-packages/tic-tac-toe
        tag_with_sha: true
  1. Commit the newly edited workflow file to your repository
Complete cd-workflow.yml file... ```yaml name: Docker CD on: push: 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@main with: name: webpack artifacts path: public/ test: runs-on: ubuntu-latest needs: build strategy: matrix: os: [ubuntu-lastest, windows-2016] node-version: [12.x, 14.x] 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@main 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@main with: name: webpack artifacts path: public - name: Build container image uses: docker/build-push-action@v1 with: username: ${{github.actor}} password: ${{secrets.GITHUB_TOKEN}} registry: docker.pkg.github.com repository: marcotterra/github-actions-for-packages/tic-tac-toe tag_with_sha: true ```
github-learning-lab[bot] commented 3 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.