Skip to content

Commit 1c766bc

Browse files
authored
Initial commit
0 parents  commit 1c766bc

33 files changed

+28228
-0
lines changed

.github/dependabot.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "github-actions"
4+
directory: "/"
5+
schedule:
6+
interval: "monthly"
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
# Make sure this file is executable
3+
# chmod a+x .github/script/initialize-repository.sh
4+
5+
# USAGE: This should only be run once upon initial creation of the
6+
# learner's repository from the template repository.
7+
# Does a dry run by default, --dry-run=false to run live.
8+
9+
# PURPOSE: This script establishes an initial related history for
10+
# all branches. It merges main into all other branches in this repository
11+
# while auto-resolving conflicts in favor of main.
12+
13+
# BACKGROUND: This operation is required because when a repository is
14+
# created from a template repository with 'Include all branches', each
15+
# of the branches starts with only one initial commit and no related history.
16+
#
17+
# That state makes it impossible to create pull requests from the
18+
# step-specific branches into main as the learner progresses
19+
# through the course.
20+
21+
# Setup committer identity
22+
git config user.name github-actions[bot]
23+
git config user.email github-actions[bot]@users.noreply.github.com
24+
25+
# Fetch all remote branches
26+
git pull --all
27+
28+
# Create list of all remote branches
29+
branches=$(git branch -r | grep -v main | sed -r 's/origin\///g' | paste -s -d ' ' -)
30+
31+
# Merge main into each branch
32+
echo -e "Merge main into each branch\n---"
33+
for branch in $branches
34+
do
35+
# Dry run by default
36+
if [[ $1 = '--dry-run=false' ]]
37+
then
38+
git checkout "$branch"
39+
git pull origin main --no-rebase -X theirs --allow-unrelated-histories --no-edit
40+
git push origin "$branch"
41+
echo "---"
42+
else
43+
echo "plan: merge main into $branch"
44+
fi
45+
done

.github/steps/-step.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0

.github/steps/0-welcome.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- readme -->
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!--
2+
<<< Author notes: Step 1 >>>
3+
Choose 3-5 steps for your course.
4+
The first step is always the hardest, so pick something easy!
5+
Link to docs.github.com for further explanations.
6+
Encourage users to open new tabs for steps!
7+
-->
8+
9+
## Step 1: Trigger a job based on labels
10+
11+
_Welcome to the course :tada:_
12+
13+
![Screen Shot 2022-06-07 at 4 01 43 PM](https://user-images.githubusercontent.com/6351798/172490466-00f27580-8906-471f-ae83-ef3b6370df7d.png)
14+
15+
A lot of things go into delivering "continuously". These things can range from culture and behavior to specific automation. In this exercise, we're going to focus on the deployment part of our automation.
16+
17+
In a GitHub Actions workflow, the `on` step defines what causes the workflow to run. In this case, we want the workflow to run different tasks when specific labels are applied to a pull request.
18+
19+
We'll use labels as triggers for multiple tasks:
20+
21+
- When someone applies a "spin up environment" label to a pull request, that'll tell GitHub Actions that we'd like to set up our resources on an Azure environment.
22+
- When someone applies a "stage" label to a pull request, that'll be our indicator that we'd like to deploy our application to a staging environment.
23+
- When someone applies a "destroy environment" label to a pull request, we'll tear down any resources that are running on our Azure account.
24+
25+
### :keyboard: Activity 1: Configure `GITHUB_TOKEN` permissions
26+
27+
At the start of each workflow run, GitHub automatically creates a unique `GITHUB_TOKEN` secret to use in your workflow. We need to make sure this token has the permissions required for this course.
28+
29+
1. Open a new browser tab, and work on the steps in your second tab while you read the instructions in this tab.
30+
1. Go to Settings > Actions > General. Ensure that the `GITHUB_TOKEN` also has **Read and write permissions** enabled under **Workflow permissions**. This is required for your workflow to be able to upload your image to the container registry.
31+
32+
### :keyboard: Activity 2: Configure a trigger based on labels
33+
34+
For now, we'll focus on staging. We'll spin up and destroy our environment in a later step.
35+
36+
1. Go to the **Actions** tab.
37+
1. Click **New workflow**
38+
1. Search for "simple workflow" and click **Configure**
39+
1. Name your workflow `deploy-staging.yml`
40+
1. Edit the contents of this file and remove all triggers and jobs.
41+
1. Edit the contents of the file to add a conditional that filters the `build` job when there is a label present called **stage**. Your resulting file should look like this:
42+
```yaml
43+
name: Stage the app
44+
45+
on:
46+
pull_request:
47+
types: [labeled]
48+
49+
jobs:
50+
build:
51+
runs-on: ubuntu-latest
52+
53+
if: contains(github.event.pull_request.labels.*.name, 'stage')
54+
```
55+
1. Click **Start commit**, and choose to make a new branch named `staging-workflow`.
56+
1. Click **Propose changes**.
57+
1. Click **Create pull request**.
58+
1. Wait about 20 seconds then refresh this page (the one you're following instructions from). [GitHub Actions](https://docs.github.com/en/actions) will automatically update to the next step.
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<!--
2+
<<< Author notes: Step 2 >>>
3+
Start this step by acknowledging the previous step.
4+
Define terms and link to docs.github.com.
5+
-->
6+
7+
## Step 2: Set up an Azure environment
8+
9+
_Good job getting started :gear:_
10+
11+
### Nice work triggering a job on specific labels
12+
13+
We won't be going into detail on the steps of this workflow, but it would be a good idea to become familiar with the actions we're using. They are:
14+
15+
- [`actions/checkout`](https://github.com/actions/checkout)
16+
- [`actions/upload-artifact`](https://github.com/actions/upload-artifact)
17+
- [`actions/download-artifact`](https://github.com/actions/download-artifact)
18+
- [`docker/login-action`](https://github.com/docker/login-action)
19+
- [`docker/build-push-action`](https://github.com/docker/build-push-action)
20+
- [`azure/login`](https://github.com/Azure/login)
21+
- [`azure/webapps-deploy`](https://github.com/Azure/webapps-deploy)
22+
23+
### :keyboard: Activity 1: Store your credentials in GitHub secrets and finish setting up your workflow
24+
25+
1. In a new tab, [create an Azure account](https://azure.microsoft.com/en-us/free/) if you don't already have one. If your Azure account is created through work, you may encounter issues accessing the necessary resources -- we recommend creating a new account for personal use and for this course.
26+
> **Note**: You may need a credit card to create an Azure account. If you're a student, you may also be able to take advantage of the [Student Developer Pack](https://education.github.com/pack) for access to Azure. If you'd like to continue with the course without an Azure account, Skills will still respond, but none of the deployments will work.
27+
1. Create a [new subscription](https://docs.microsoft.com/en-us/azure/cost-management-billing/manage/create-subscription) in the Azure Portal.
28+
> **Note**: your subscription must be configured "Pay as you go" which will require you to enter billing information. This course will only use a few minutes from your free plan, but Azure requires the billing information.
29+
1. Install [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest) on your machine.
30+
1. In your terminal, run:
31+
```shell
32+
az login
33+
```
34+
1. Select the subscription you just selected from the interactive authentication prompt. Copy the value of the subscription ID to a safe place. We'll call this `AZURE_SUBSCRIPTION_ID`. Here's an example of what it looks like:
35+
```shell
36+
No Subscription name Subscription ID Tenant
37+
----- ------------------- ------------------------------------ -----------------
38+
[1] * some-subscription f****a09-****-4d1c-98**-f**********c Default Directory
39+
```
40+
1. In your terminal, run the command below.
41+
42+
```shell
43+
az ad sp create-for-rbac --name "GitHub-Actions" --role contributor \
44+
--scopes /subscriptions/{subscription-id} \
45+
--sdk-auth
46+
47+
# Replace {subscription-id} with the same id stored in AZURE_SUBSCRIPTION_ID.
48+
```
49+
50+
> **Note**: The `\` character works as a line break on Unix based systems. If you are on a Windows based system the `\` character will cause this command to fail. Place this command on a single line if you are using Windows.
51+
52+
1. Copy the entire contents of the command's response, we'll call this `AZURE_CREDENTIALS`. Here's an example of what it looks like:
53+
```shell
54+
{
55+
"clientId": "<GUID>",
56+
"clientSecret": "<GUID>",
57+
"subscriptionId": "<GUID>",
58+
"tenantId": "<GUID>",
59+
(...)
60+
}
61+
```
62+
1. Back on GitHub, click on this repository's **Secrets and variables > Actions** in the Settings tab.
63+
1. Click **New repository secret**
64+
1. Name your new secret **AZURE_SUBSCRIPTION_ID** and paste the value from the `id:` field in the first command.
65+
1. Click **Add secret**.
66+
1. Click **New repository secret** again.
67+
1. Name the second secret **AZURE_CREDENTIALS** and paste the entire contents from the second terminal command you entered.
68+
1. Click **Add secret**
69+
1. Go back to the Pull requests tab and in your pull request go to the **Files Changed** tab. Find and then edit the `.github/workflows/deploy-staging.yml` file to use some new actions. The full workflow file, should look like this:
70+
```yaml
71+
name: Deploy to staging
72+
73+
on:
74+
pull_request:
75+
types: [labeled]
76+
77+
env:
78+
IMAGE_REGISTRY_URL: ghcr.io
79+
###############################################
80+
### Replace <username> with GitHub username ###
81+
###############################################
82+
DOCKER_IMAGE_NAME: <username>-azure-ttt
83+
AZURE_WEBAPP_NAME: <username>-ttt-app
84+
###############################################
85+
86+
jobs:
87+
build:
88+
if: contains(github.event.pull_request.labels.*.name, 'stage')
89+
90+
runs-on: ubuntu-latest
91+
92+
steps:
93+
- uses: actions/checkout@v4
94+
- uses: actions/setup-node@v4
95+
with:
96+
node-version: 16
97+
- name: npm install and build webpack
98+
run: |
99+
npm install
100+
npm run build
101+
- uses: actions/upload-artifact@v4
102+
with:
103+
name: webpack artifacts
104+
path: public/
105+
106+
Build-Docker-Image:
107+
runs-on: ubuntu-latest
108+
needs: build
109+
name: Build image and store in GitHub Container Registry
110+
steps:
111+
- name: Checkout
112+
uses: actions/checkout@v4
113+
114+
- name: Download built artifact
115+
uses: actions/download-artifact@v4
116+
with:
117+
name: webpack artifacts
118+
path: public
119+
120+
- name: Log in to GHCR
121+
uses: docker/login-action@v3
122+
with:
123+
registry: ${{ env.IMAGE_REGISTRY_URL }}
124+
username: ${{ github.actor }}
125+
password: ${{ secrets.CR_PAT }}
126+
127+
- name: Extract metadata (tags, labels) for Docker
128+
id: meta
129+
uses: docker/metadata-action@v5
130+
with:
131+
images: ${{env.IMAGE_REGISTRY_URL}}/${{ github.repository }}/${{env.DOCKER_IMAGE_NAME}}
132+
tags: |
133+
type=sha,format=long,prefix=
134+
135+
- name: Build and push Docker image
136+
uses: docker/build-push-action@v5
137+
with:
138+
context: .
139+
push: true
140+
tags: ${{ steps.meta.outputs.tags }}
141+
labels: ${{ steps.meta.outputs.labels }}
142+
143+
Deploy-to-Azure:
144+
runs-on: ubuntu-latest
145+
needs: Build-Docker-Image
146+
name: Deploy app container to Azure
147+
steps:
148+
- name: "Login via Azure CLI"
149+
uses: azure/login@v2
150+
with:
151+
creds: ${{ secrets.AZURE_CREDENTIALS }}
152+
153+
- uses: azure/docker-login@v1
154+
with:
155+
login-server: ${{env.IMAGE_REGISTRY_URL}}
156+
username: ${{ github.actor }}
157+
password: ${{ secrets.CR_PAT }}
158+
159+
- name: Deploy web app container
160+
uses: azure/webapps-deploy@v3
161+
with:
162+
app-name: ${{env.AZURE_WEBAPP_NAME}}
163+
images: ${{env.IMAGE_REGISTRY_URL}}/${{ github.repository }}/${{env.DOCKER_IMAGE_NAME}}:${{ github.sha }}
164+
165+
- name: Azure logout via Azure CLI
166+
uses: azure/CLI@v2
167+
with:
168+
inlineScript: |
169+
az logout
170+
az cache purge
171+
az account clear
172+
```
173+
1. After you've edited the file, click **Commit changes...** and commit to the `staging-workflow` branch.
174+
1. Wait about 20 seconds then refresh this page (the one you're following instructions from). [GitHub Actions](https://docs.github.com/en/actions) will automatically update to the next step.

0 commit comments

Comments
 (0)