Skip to content

Commit 141cd5f

Browse files
authored
Merge pull request #3 from jonashackt/migrate-from-PAT-to-GITHUB_TOKEN
Migrate from pat to GitHub token & docker/login-action
2 parents f3e7dc2 + 05e8b58 commit 141cd5f

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

.github/workflows/publish.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ jobs:
99
steps:
1010
- uses: actions/checkout@v2
1111

12+
- name: Login to GitHub Container Registry
13+
uses: docker/login-action@v1
14+
with:
15+
registry: ghcr.io
16+
username: ${{ github.actor }}
17+
password: ${{ secrets.GITHUB_TOKEN }}
18+
1219
- name: Build the hello-world Docker image
1320
run: |
14-
echo $CR_PAT | docker login ghcr.io -u jonashackt --password-stdin
1521
docker build . --tag ghcr.io/jonashackt/hello-world:latest
1622
docker run ghcr.io/jonashackt/hello-world:latest
1723
docker push ghcr.io/jonashackt/hello-world:latest
18-
env:
19-
CR_PAT: ${{ secrets.CR_PAT }}
2024

README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,11 @@ First we need to activate the Container Registry beta feature in our account: ht
112112
![github-improved-container-support](screenshots/github-improved-container-support.png)
113113

114114

115-
#### Authenticate and login to GitHub Container Registry using a PAT
115+
#### Authenticate and login to GitHub Container Registry using GITHUB_TOKEN
116116

117-
Right now (in beta) [using the `GITHUB_TOKEN` to authenticate to the GHCR isn't possible](https://docs.github.com/en/packages/guides/pushing-and-pulling-docker-images#authenticating-to-github-container-registry). So we need to create a personal access token (PAT). But mind what the docs say:
117+
From March 2021 on we should be able to use our `GITHUB_TOKEN` to authenticate against the GitHub Container Registry instead of using a separate PAT (see https://github.blog/changelog/2021-03-24-packages-container-registry-now-supports-github_token/)!
118118

119-
> PATs can grant broad access to your account. We recommend selecting only the necessary read, write, or delete package scope when creating a PAT to authenticate to the container registry. Avoid including the repo scope in a PAT used by a GitHub Actions workflow because it gives unnecessary additional access.
120-
121-
Here's the guide on how to create a PAT in Settings/Developer settings/Personal access tokens: https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token You need to select `read:packages`, `write:packages` and `delete:packages` scopes like this:
122-
123-
![github-create-pat](screenshots/github-create-pat.png)
124-
125-
Using the token we should now create a new repository secret inside our repo settings:
126-
127-
![github-pat-repository-secret](screenshots/github-pat-repository-secret.png)
128-
129-
With all that set up we can now use the secret inside our GHA workflow file [publish.yml](.github/workflows/publish.yml):
119+
So our GHA workflow file [publish.yml](.github/workflows/publish.yml) should look like this:
130120

131121
```yaml
132122
name: publish
@@ -142,11 +132,22 @@ jobs:
142132

143133
- name: Build the hello-world Docker image
144134
run: |
145-
echo $CR_PAT | docker login ghcr.io -u jonashackt --password-stdin
146-
env:
147-
CR_PAT: ${{ secrets.CR_PAT }}
135+
echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin
136+
148137
```
149138
139+
or Alternatively we can also use the [docker/login-action](https://github.com/docker/login-action) to to the login:
140+
141+
```yaml
142+
- name: Login to GitHub Container Registry
143+
uses: docker/login-action@v1
144+
with:
145+
registry: ghcr.io
146+
username: ${{ github.actor }}
147+
password: ${{ secrets.GITHUB_TOKEN }}
148+
```
149+
150+
150151
#### Publish (Push) Container image to GHCR
151152
152153
The final step now is to push our container image to the GitHub Container Registry. Therefore we need to tag our image correctly while building it using `ghcr.io/OWNER/IMAGE_NAME:latest`. After that we can push it:

screenshots/github-create-pat.png

-368 KB
Binary file not shown.
-417 KB
Binary file not shown.

0 commit comments

Comments
 (0)